How do I make the buttons on this GUI work?

Joined
Jul 19, 2017
Messages
13
Reaction score
2
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:

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:
Joined
Jul 19, 2017
Messages
13
Reaction score
2
What did you mean by GUI itself doesn't work ? Can you specific the error?

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.
 
Joined
Apr 25, 2017
Messages
259
Reaction score
36
Modify your code to this
Code:
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:
Joined
Jul 19, 2017
Messages
13
Reaction score
2
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?
 
Joined
Apr 25, 2017
Messages
259
Reaction score
36
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?
Why would you need to create two static class for upButtonListener and downButtonListener ? For me I will do everything just in one class.
 
Joined
Jul 19, 2017
Messages
13
Reaction score
2
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.
 
Joined
Jul 19, 2017
Messages
13
Reaction score
2
How do I make it so that it continues to perform the action when I click and hold the button?
 
Joined
Apr 25, 2017
Messages
259
Reaction score
36
How do I make it so that it continues to perform the action when I click and hold the button?
For me I will write like this. Use ActionListener for the two buttons.

Code:
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();
    }
}
 
Joined
Jul 19, 2017
Messages
13
Reaction score
2
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)".
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top