| | |
button will not display
![]() |
•
•
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: 2,226
Reputation:
Solved Threads: 273
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.
•
•
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: 2,226
Reputation:
Solved Threads: 273
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.
![]() |
Similar Threads
- Random Numbers on button (Java)
- How to display searched result in word (VB.NET)
- radio button to display (VB.NET)
- 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
Views: 218 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
add android applet arguments array arrays binary bluetooth c# chat class classes client code compile compiler component convert converter coordinates data database db design detection draw eclipse error event exception fast file forloop fractal givemetehcodez graphics gridlayout gui helpwithhomework homeworkassignment html ide image input j2me java javafx jframe jmf jni jpanel key lazy linked linked-list list loop main method methods mobile netbeans newbie number object objects oracle os parameter pattern phone pixel print printing problem program programming read recursion remote remove robot scanner server set socket sort sql string swing system test text thread transfer translate tree user web windows






