button will not display

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 10
Reputation: vortex24 is an unknown quantity at this point 
Solved Threads: 2
vortex24 vortex24 is offline Offline
Newbie Poster

button will not display

 
0
  #1
Nov 4th, 2009
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.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class ComboBoxFrame extends JFrame {
  6. /***/ private static final long serialVersionUID = 1L;
  7.  
  8. private JComboBox b;
  9. private String s;
  10. private int x = 0;
  11. private JButton g[] = new JButton[10];
  12.  
  13. private String courses[] =
  14. { "Advanced Functions", "Calculus", "Chemistry", "Physics" };
  15.  
  16. public ComboBoxFrame() {
  17. super("Pick your courses!");
  18. setLayout(new GridLayout(3, 3));
  19.  
  20. b = new JComboBox(courses);
  21.  
  22. b.addItemListener( new ItemListener() {
  23. public void itemStateChanged(ItemEvent event) {
  24. if(event.getStateChange() == ItemEvent.SELECTED) {
  25. s = (String) b.getSelectedItem();
  26. g[x] = new JButton(s);
  27. add(g[x]);
  28. //g[x].addActionListener(this);
  29. x++;
  30. } } } );
  31.  
  32. add(b);
  33. }
  34.  
  35. /** public void actionPerformed(ActionEvent e) {
  36. String s;
  37. for(int x = 0; x < 10; x++) {
  38. s = courses[x];
  39. if(e.getSource() == s) remove(g[x]);
  40. }
  41. }*/
  42.  
  43. public static void main(String args[]) {
  44. ComboBoxFrame comboBoxFrame = new ComboBoxFrame();
  45. comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  46. comboBoxFrame.setSize( 350, 150 );
  47. comboBoxFrame.setVisible( true );
  48. }
  49. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,619
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
1
  #2
Nov 4th, 2009
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class ComboBoxFrame extends JFrame {
  6. /***/ private static final long serialVersionUID = 1L;
  7.  
  8. private JComboBox b;
  9. private String s;
  10. private int x = 0;
  11. private JButton g[] = new JButton[10];
  12. private ComboBoxFrame me;
  13. private String courses[] =
  14. { "Advanced Functions", "Calculus", "Chemistry", "Physics" };
  15.  
  16. public ComboBoxFrame() {
  17. super("Pick your courses!");
  18. setLayout(new GridLayout(3, 3));
  19. me = this;
  20. b = new JComboBox(courses);
  21.  
  22. b.addItemListener( new ItemListener() {
  23. public void itemStateChanged(ItemEvent event) {
  24. if(event.getStateChange() == ItemEvent.SELECTED) {
  25. s = (String) b.getSelectedItem();
  26. System.out.println(s);
  27. g[x] = new JButton(s);
  28. add(g[x]);
  29. me.validate();
  30. //g[x].addActionListener(this);
  31. x++;
  32. } } } );
  33.  
  34. add(b);
  35. }
  36.  
  37. /** public void actionPerformed(ActionEvent e) {
  38. String s;
  39. for(int x = 0; x < 10; x++) {
  40. s = courses[x];
  41. if(e.getSource() == s) remove(g[x]);
  42. }
  43. }*/
  44.  
  45. public static void main(String args[]) {
  46. ComboBoxFrame comboBoxFrame = new ComboBoxFrame();
  47. comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  48. comboBoxFrame.setSize( 350, 150 );
  49. comboBoxFrame.setVisible( true );
  50. }
  51. }


Now try.
Out.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: vortex24 is an unknown quantity at this point 
Solved Threads: 2
vortex24 vortex24 is offline Offline
Newbie Poster
 
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!

  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5.  
  6. public class ComboBoxFrame extends JFrame {
  7. /***/ private static final long serialVersionUID = 1L;
  8.  
  9. private JComboBox b;
  10. private String s;
  11. private TextFieldHandler handler = new TextFieldHandler();
  12. private int x = 0;
  13. private JButton g[] = new JButton[10];
  14. private ComboBoxFrame me;
  15. private String courses[] =
  16. { "Advanced Functions", "Calculus", "Chemistry", "Physics", "English",
  17. "History", "Law", "Politics", "Phys. Ed.", "Accounting" };
  18.  
  19. public ComboBoxFrame() {
  20. super("Pick your courses!");
  21. setLayout(new GridLayout(3, 3));
  22. me = this;
  23. b = new JComboBox(courses);
  24.  
  25. b.addItemListener( new ItemListener() {
  26. public void itemStateChanged(ItemEvent event) {
  27. if(event.getStateChange() == ItemEvent.SELECTED) {
  28. s = (String) b.getSelectedItem();
  29. System.out.println(s);
  30. g[x] = new JButton(s);
  31. g[x].setBorder(null);
  32. add(g[x]);
  33. me.validate();
  34. g[x].addActionListener(handler);
  35. x++;
  36. } } } );
  37.  
  38. add(b);
  39. }
  40.  
  41. private class TextFieldHandler implements ActionListener {
  42. public void actionPerformed(ActionEvent e) {
  43. String s = "";
  44. for(int x = 0; x < 10; x++) {
  45. s = courses[x];
  46. if(e.getSource() == s) remove(g[x]);
  47. }
  48. me.repaint();
  49. }
  50. }
  51.  
  52. public static void main(String args[]) {
  53. ComboBoxFrame comboBoxFrame = new ComboBoxFrame();
  54. comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  55. comboBoxFrame.setSize( 350, 150 );
  56. comboBoxFrame.setVisible( true );
  57. }
  58. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,619
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #4
34 Days Ago
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.
Out.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC