updating text fields in GUIs

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

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

updating text fields in GUIs

 
0
  #1
Dec 18th, 2007
I'm stuck trying to figure out how to update the text field after I click on the button in the GUI. The number of roaches is supposed to be updated each time I click, but I can't seem to find the right tool for the job in the APIs.


RoachPopulation.java
  1. public class RoachPopulation
  2. {
  3. public RoachPopulation(int initialPopulation)
  4. {
  5. pop = initialPopulation;
  6. }
  7.  
  8. public void doublePopulation()
  9. {
  10. pop = pop * 2;
  11. }
  12.  
  13. public int getPopulation()
  14. {
  15. return pop;
  16. }
  17.  
  18. private int pop;
  19. }

RoachPopulationViewer.java
  1. import java.awt.event.ActionListener;
  2. import java.awt.event.ActionEvent;
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JPanel;
  7. import javax.swing.JTextField;
  8.  
  9. /**
  10.   This program displays the growth of a roach population.
  11. */
  12. public class RoachPopulationViewer
  13. {
  14. public static void main(String[] args)
  15. {
  16. JFrame frame = new JFrame();
  17.  
  18. JButton button = new JButton("Double roach population!");
  19.  
  20. final RoachPopulation roachPopulation = new RoachPopulation(2);
  21.  
  22. // The label for displaying the results
  23. JLabel label = new JLabel(
  24. "population: " + roachPopulation.getPopulation());
  25.  
  26. // The panel that holds the user interface components
  27. JPanel panel = new JPanel();
  28. panel.add(button);
  29. panel.add(label);
  30. frame.add(panel);
  31.  
  32. //your code goes here
  33. class ButtonListener implements ActionListener
  34. {
  35. public void actionPerformed(ActionEvent e)
  36. {
  37. roachPopulation.doublePopulation();
  38. }
  39. }
  40.  
  41. ActionListener listener = new ButtonListener();
  42. button.addActionListener(listener);
  43.  
  44. frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
  45. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. frame.setVisible(true);
  47. }
  48.  
  49. private static final int FRAME_WIDTH = 400;
  50. private static final int FRAME_HEIGHT = 100;
  51. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: updating text fields in GUIs

 
0
  #2
Dec 18th, 2007
I think is something like:
  1. roachPopulation.doublePopulation();
  2. lable.setText( roachPopulation.getPopulation() );
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC