(Beginner) Simple Application & Graphics Help
Hi all, I have a few questions regarding a program meant to output some simple information. I created an application with a frame, panel, JButtons, and JLabels that all involve displaying an integer that increments or decrements by 1 when pressed by either button, respectively. My problem is that everything is displayed correctly, but the JLabel displaying the integer doesn't update when either button is pressed. Here's my code:
Code java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AddSubtractGUI
{
private int WIDTH = 300;
private int HEIGHT = 75;
private int pushTotal = 50; //integer that is incremented/decremented
private JFrame frame;
private JPanel panel;
private JLabel totalDisplay, pushTotalDisplay; //displays the push total
private JButton addPush, subPush; //addition and subtraction buttons
public AddSubtractGUI()
{
frame = new JFrame ("Add/Subtract"); //title of application
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //closes application
totalDisplay = new JLabel ("Total : ");
pushTotalDisplay = new JLabel ("50");
addPush = new JButton ("Add!");
subPush = new JButton ("Subtract!");
panel = new JPanel();
panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
panel.setBackground (Color.cyan);
panel.add (totalDisplay);
panel.add (pushTotalDisplay);
panel.add (addPush);
panel.add (subPush);
frame.getContentPane().add (panel); //package to display all components of app
}
public void display() //enables frame
{
frame.pack();
frame.setVisible(true);
}
private class AddListener implements ActionListener //addition button upon press
{
public void actionPerformed (ActionEvent event)
{
pushTotal++;
pushTotalDisplay.setText(Integer.toString (pushTotal) );
}
}
private class SubListener implements ActionListener //subtraction button upon press
{
public void actionPerformed (ActionEvent event)
{
pushTotal--;
pushTotalDisplay.setText(Integer.toString (pushTotal) );
}
}
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
public class AddSubtractDriver //driver program
{
public static void main (String[] args)
{
AddSubtractGUI converter = new AddSubtractGUI();
converter.display();
}
}
I'm trying to get my JButtons to update properly so that the push total updates when they are pressed, and corresponds with the operation that they are labeled as.
I run my code through my driver as an application, rather than as an applet. I apologize for any ambiguity, this is my first post. Thanks!
Re: (Beginner) Simple Application & Graphics Help
Are the listener methods being called? Add some println statements to the method that print out the changed values of pushTotal to see if they are called and what the value of pushTotal is changed to.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: (Beginner) Simple Application & Graphics Help
When you mention calling my listener methods, do you mean in the driver? I'm not 100% how to do that, since I'm building an application. It doesn't seem like they were called when I added some println's, thanks for the reply :)
Re: (Beginner) Simple Application & Graphics Help
There is nothing in the code that will cause the listener methods to be called. See the tutorial for how to have them called:
How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
Re: (Beginner) Simple Application & Graphics Help
Thanks Norm, it works now, I had to have listener objects that I could reference each update. Thanks!!
Re: (Beginner) Simple Application & Graphics Help