How do I make the buttons on this GUI work?

Discussion in 'Java' started by Alioune, Jul 19, 2017.

  1. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    I'm trying to make a GUI that will increase a number when you click one button and decrease that number when you click another button. This is my code:

    Code (Java):

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class PushCounter
    {
       static JTextField textArea;
       static int count = 0;
     
       public static void main(String[] args)
       {
         
           JFrame frame = new JFrame("PushCounter");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           JPanel panel = new JPanel();
           JButton upButton = new JButton("Up");
           JButton downButton = new JButton("Down");
           JTextField textArea = new JTextField(15);
           JLabel label = new JLabel("Count");
         
         
           upButton.addActionListener(new upButtonListener());
           downButton.addActionListener(new downButtonListener());
         
           panel.add(label);
           panel.add(textArea);
           panel.add(downButton);
           panel.add(upButton);
         
         
           frame.getContentPane().add(panel);
           frame.setVisible(true);
           frame.pack();
         
       }
     
       private static class upButtonListener implements ActionListener
       {
         
           public void actionPerformed(ActionEvent event)
           {
               ++count;
               textArea.setText(Integer.toString(count));
           }
       }
     
       private static class downButtonListener implements ActionListener
       {
         
           public void actionPerformed(ActionEvent event)
           {
               --count;
               textArea.setText(Integer.toString(count));
           }
       }
     
    }
     
    It compiles, but the GUI itself doesn't work. What's wrong with it? How would you create something like this and why?
     
    Last edited by a moderator: Jul 19, 2017
    Alioune, Jul 19, 2017
    #1
    1. Advertisements

  2. Alioune

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    98
    Likes Received:
    14
    Location:
    Malaysia
    What did you mean by GUI itself doesn't work ? Can you specific the error?
     
    John Joe, Jul 20, 2017
    #2
    1. Advertisements

  3. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    If you run the code, you'll find that a GUI appears. If you click either of the buttons the program crashes. I have no idea why.
     
    Alioune, Jul 20, 2017
    #3
  4. Alioune

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    98
    Likes Received:
    14
    Location:
    Malaysia
    Modify your code to this
    Code (Text):
    public class PushCounter {

        static JTextField textArea;
        static int count = 0;

        public static void main(String[] args) {

            JFrame frame = new JFrame("PushCounter");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            JButton upButton = new JButton("Up");
            JButton downButton = new JButton("Down");
            textArea = new JTextField(15);  // use one object enough
            JLabel label = new JLabel("Count");

            ActionListener showComponentAction = new upButtonListener(upButton);
            upButton.addActionListener(showComponentAction);

            ActionListener showComponentAction1 = new downButtonListener(downButton);
            downButton.addActionListener(showComponentAction1);

            panel.add(label);
            panel.add(textArea);
            panel.add(downButton);
            panel.add(upButton);

            frame.getContentPane().add(panel);
            frame.setVisible(true);
            frame.pack();

        }

        private static class upButtonListener implements ActionListener {

            private JButton btn;

            public upButtonListener(JButton btn) {
                this.btn = btn;
            }

            public void actionPerformed(ActionEvent event) {
                ++count;
                System.out.println(count);
                textArea.setText(Integer.toString(count));
            }
        }

        private static class downButtonListener implements ActionListener {

            private JButton downBtn;

            public downButtonListener(JButton downBtn) {
                this.downBtn = downBtn;
            }

            public void actionPerformed(ActionEvent event) {
                --count;
                textArea.setText(Integer.toString(count));
            }
        }

    }
    I also noticed that you are creating two JTextField objects. Create one had enough.

    Refer to this https://stackoverflow.com/a/26166672/5156075 to get clear explanation.
     
    Last edited: Jul 20, 2017
    John Joe, Jul 20, 2017
    #4
  5. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    Thanks, that solved the problem. How would you make a project like this? Other than what you said, is there anything about the structure that could be done better or does it not matter?
     
    Alioune, Jul 20, 2017
    #5
    John Joe likes this.
  6. Alioune

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    98
    Likes Received:
    14
    Location:
    Malaysia
    Why would you need to create two static class for upButtonListener and downButtonListener ? For me I will do everything just in one class.
     
    John Joe, Jul 20, 2017
    #6
  7. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    I thought I had to create a private class containing the actionPerformed class. I guess I could have done it in the same class. I'm a novice at this language.
     
    Alioune, Jul 20, 2017
    #7
  8. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    How do I make it so that it continues to perform the action when I click and hold the button?
     
    Alioune, Jul 20, 2017
    #8
  9. Alioune

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    98
    Likes Received:
    14
    Location:
    Malaysia
    For me I will write like this. Use ActionListener for the two buttons.

    Code (Text):
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class PushCounter extends JPanel {

        JTextField textArea;
        int count = 0;

        public PushCounter() {
            JPanel panel = new JPanel();
            JButton upButton = new JButton("Up");
            JButton downButton = new JButton("Down");
            textArea = new JTextField(15); // use one object enough
            JLabel label = new JLabel("Count");

            upButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ++count;
                    textArea.setText(Integer.toString(count));
                }
            });

            downButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    --count;
                    textArea.setText(Integer.toString(count));
                }
            });

            panel.add(label);
            panel.add(textArea);
            panel.add(downButton);
            panel.add(upButton);
            add(panel);
        }

        public static void main(String[] args) {
            PushCounter pc = new PushCounter();
            JFrame frame = new JFrame("PushCounter");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(pc);
            frame.setVisible(true);
            frame.pack();
        }
    }
     
    John Joe, Jul 22, 2017
    #9
  10. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    There are problems with the following lines:

    add(panel);
    frame.add(pc);

    I'm using the Eclipse ide. It says "method add(JPanel) is undefined for the type PushCounter" and "the method add(Component) in the type Container is not applicable for the arguments (PushCounter)".
     
    Alioune, Aug 4, 2017
    #10
  11. Alioune

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    98
    Likes Received:
    14
    Location:
    Malaysia
    It works fine in my PC
     
    John Joe, Aug 4, 2017
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.