| | |
button will not display
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 2
Hi, I am working on code to have a drop down menu from which you can pick items and add a button with that course. Unfortunetally, I can't get the button to display. And help is greatly appreciated.
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxFrame extends JFrame { /***/ private static final long serialVersionUID = 1L; private JComboBox b; private String s; private int x = 0; private JButton g[] = new JButton[10]; private String courses[] = { "Advanced Functions", "Calculus", "Chemistry", "Physics" }; public ComboBoxFrame() { super("Pick your courses!"); setLayout(new GridLayout(3, 3)); b = new JComboBox(courses); b.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent event) { if(event.getStateChange() == ItemEvent.SELECTED) { s = (String) b.getSelectedItem(); g[x] = new JButton(s); add(g[x]); //g[x].addActionListener(this); x++; } } } ); add(b); } /** public void actionPerformed(ActionEvent e) { String s; for(int x = 0; x < 10; x++) { s = courses[x]; if(e.getSource() == s) remove(g[x]); } }*/ public static void main(String args[]) { ComboBoxFrame comboBoxFrame = new ComboBoxFrame(); comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); comboBoxFrame.setSize( 350, 150 ); comboBoxFrame.setVisible( true ); } }
•
•
Join Date: Sep 2008
Posts: 1,629
Reputation:
Solved Threads: 206
1
#2 Nov 4th, 2009
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxFrame extends JFrame { /***/ private static final long serialVersionUID = 1L; private JComboBox b; private String s; private int x = 0; private JButton g[] = new JButton[10]; private ComboBoxFrame me; private String courses[] = { "Advanced Functions", "Calculus", "Chemistry", "Physics" }; public ComboBoxFrame() { super("Pick your courses!"); setLayout(new GridLayout(3, 3)); me = this; b = new JComboBox(courses); b.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent event) { if(event.getStateChange() == ItemEvent.SELECTED) { s = (String) b.getSelectedItem(); System.out.println(s); g[x] = new JButton(s); add(g[x]); me.validate(); //g[x].addActionListener(this); x++; } } } ); add(b); } /** public void actionPerformed(ActionEvent e) { String s; for(int x = 0; x < 10; x++) { s = courses[x]; if(e.getSource() == s) remove(g[x]); } }*/ public static void main(String args[]) { ComboBoxFrame comboBoxFrame = new ComboBoxFrame(); comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); comboBoxFrame.setSize( 350, 150 ); comboBoxFrame.setVisible( true ); } }
Now try.
Out.
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 2
0
#3 Nov 4th, 2009
Thanks! One more question; I am trying to get the button to remove itself when clicked, but I can't get it working. Here i tried to call the .remove() method and .repaint(), but I can't get it to remove itself. Some insight to the problem would be great, thanks in advance! 

Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxFrame extends JFrame { /***/ private static final long serialVersionUID = 1L; private JComboBox b; private String s; private TextFieldHandler handler = new TextFieldHandler(); private int x = 0; private JButton g[] = new JButton[10]; private ComboBoxFrame me; private String courses[] = { "Advanced Functions", "Calculus", "Chemistry", "Physics", "English", "History", "Law", "Politics", "Phys. Ed.", "Accounting" }; public ComboBoxFrame() { super("Pick your courses!"); setLayout(new GridLayout(3, 3)); me = this; b = new JComboBox(courses); b.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent event) { if(event.getStateChange() == ItemEvent.SELECTED) { s = (String) b.getSelectedItem(); System.out.println(s); g[x] = new JButton(s); g[x].setBorder(null); add(g[x]); me.validate(); g[x].addActionListener(handler); x++; } } } ); add(b); } private class TextFieldHandler implements ActionListener { public void actionPerformed(ActionEvent e) { String s = ""; for(int x = 0; x < 10; x++) { s = courses[x]; if(e.getSource() == s) remove(g[x]); } me.repaint(); } } public static void main(String args[]) { ComboBoxFrame comboBoxFrame = new ComboBoxFrame(); comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); comboBoxFrame.setSize( 350, 150 ); comboBoxFrame.setVisible( true ); } }
•
•
Join Date: Sep 2008
Posts: 1,629
Reputation:
Solved Threads: 206
0
#4 Nov 5th, 2009
You need to register an ActionListener with the buttons as they are created, similarly to how you declared the ItemListener (with b.addItemListener). You can find examples of ActionListeners being used online, here is one similar to the ItemListener you used. http://www.javaprogrammingforums.com...ava-swing.html
In your actionPerformed method, you'd just use JFrame's remove() method on the button. So you'd use me.remove((JButton)event.getSource()) where event is the argument to the actionPerformed method.
In your actionPerformed method, you'd just use JFrame's remove() method on the button. So you'd use me.remove((JButton)event.getSource()) where event is the argument to the actionPerformed method.
Out.
![]() |
Similar Threads
- How to display searched result in word (VB.NET)
- radio button to display (VB.NET)
- Random Numbers on button (Java)
- How to put procedures in a dynamic input button? (ASP.NET)
- Desktop background not changeable -- display properties inaccessible (Viruses, Spyware and other Nasties)
- how i want to make the button display when first click and hide when second click? (VB.NET)
- Display user input in new window (VB.NET)
- Display Autonumber (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Merging two files [editted]
- Next Thread: J2ME:http post from a midlet to servlet
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor






