| | |
updating text fields in GUIs
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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
RoachPopulationViewer.java
RoachPopulation.java
Java Syntax (Toggle Plain Text)
public class RoachPopulation { public RoachPopulation(int initialPopulation) { pop = initialPopulation; } public void doublePopulation() { pop = pop * 2; } public int getPopulation() { return pop; } private int pop; }
RoachPopulationViewer.java
Java Syntax (Toggle Plain Text)
import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /** This program displays the growth of a roach population. */ public class RoachPopulationViewer { public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Double roach population!"); final RoachPopulation roachPopulation = new RoachPopulation(2); // The label for displaying the results JLabel label = new JLabel( "population: " + roachPopulation.getPopulation()); // The panel that holds the user interface components JPanel panel = new JPanel(); panel.add(button); panel.add(label); frame.add(panel); //your code goes here class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { roachPopulation.doublePopulation(); } } ActionListener listener = new ButtonListener(); button.addActionListener(listener); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private static final int FRAME_WIDTH = 400; private static final int FRAME_HEIGHT = 100; }
I think is something like:
Java Syntax (Toggle Plain Text)
roachPopulation.doublePopulation(); lable.setText( roachPopulation.getPopulation() );
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Other Threads in the Java Forum
- Previous Thread: Memory Leaks in Java
- Next Thread: how to get the pdf properties using java
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






