Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: (Beginner) Simple Application & Graphics Help

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default (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:

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (Beginner) Simple Application & Graphics Help

    Thanks Norm, it works now, I had to have listener objects that I could reference each update. Thanks!!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: (Beginner) Simple Application & Graphics Help

    You're welcome.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 12
    Last Post: May 8th, 2012, 05:22 PM
  2. Replies: 1
    Last Post: November 11th, 2011, 07:46 PM
  3. Baffled by simple graphics
    By macantas in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 4th, 2011, 08:50 AM
  4. Games/Application (Video output, Frame, Graphics Components,Versatility)
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 7th, 2011, 08:06 AM
  5. Replies: 2
    Last Post: March 23rd, 2011, 08:51 AM

Tags for this Thread