Search code examples
javaswingjbuttonactionlistenerjtextfield

Having Trouble Using JButton to Increment/Decrement value of JTextField


Im having trouble using JButtons to increment/decrement an already set JTextField.

I know an actionlistener is necessary but I'm not sure how to set it up. I also have to set this up for multiple JButtons which further complicates this process.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class App1 extends JFrame implements ActionListener, MouseListener
{
    JPanel upper, lower, jp1, jp2, jp3;
    JButton jb1, jb2, jb3, jb4, jb5, jb6;
    JLabel jl1, jl2, jl3;
    JTextField jtf1, jtf2, jtf3;
    int count = 128;

    public static void main(String[] args) {
        App1 a1 = new App1();
    }

    public App1() {

        JFrame myFrame = new JFrame("Color Selector");
        myFrame.setLocationRelativeTo(null);
        myFrame.setSize(new Dimension(600,400));

        GridLayout layout = new GridLayout(2,1);
        myFrame.setLayout(layout);

        JPanel upper = new JPanel();
        upper.setBackground(Color.GRAY);

        JPanel lower = new JPanel(); add(lower);
        lower.setLayout(new BoxLayout(lower, BoxLayout.X_AXIS));
        lower.setBackground(Color.GRAY);
        JPanel jp1 = new JPanel(); lower.add(jp1);
        JPanel jp2 = new JPanel(); lower.add(jp2);
        JPanel jp3 = new JPanel(); lower.add(jp3);
        lower.addMouseListener(this);

        JLabel jl1 = new JLabel("Red"); jp1.add(jl1);
        JLabel jl2 = new JLabel("Green"); jp2.add(jl2);
        JLabel jl3 = new JLabel("Blue"); jp3.add(jl3);

        jp1.setBackground(Color.RED);
        jp2.setBackground(Color.GREEN);
        jp3.setBackground(Color.BLUE);

        JButton jb1 = new JButton("R-"); jp1.add(jb1);
        JTextField jtf1 = new JTextField(6);jp1.add(jtf1);
        jtf1.setText("128");
        JButton jb2 = new JButton("R+"); jp1.add(jb2);
        jb1.addActionListener(this);
        jb2.addActionListener(this);

        JButton jb3 = new JButton("G-"); jp2.add(jb3);
        JTextField jtf2 = new JTextField(6);jp2.add(jtf2);
        jtf2.setText("128");
        JButton jb4 = new JButton("G+"); jp2.add(jb4);
        jb3.addActionListener(this);
        jb4.addActionListener(this);

        JButton jb5 = new JButton("B-"); jp3.add(jb5);
        JTextField jtf3 = new JTextField(6);jp3.add(jtf3);
        jtf3.setText("128");
        JButton jb6 = new JButton("B+"); jp3.add(jb6);
        jb5.addActionListener(this);
        jb6.addActionListener(this);

        myFrame.add(upper);
        myFrame.add(lower);

        myFrame.setVisible(true);
    }

    private void start() {
        // TODO Auto-generated method stub
    }

    private static String setEditable(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    private static String setText(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    private static void add(JPanel lower) {
        // TODO Auto-generated method stub
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
    }

    private void rebuild() {
        // TODO Auto-generated method stub
    }
}   

I expect JButton to increment/decrement already set JTextField "128" by +1 or -1 with use of JButtons.


Solution

  • In this case it would be simpler to have a seperate Action Listener for each button. For example:
    Instead of

    jb1.addActionListener(this);
    jb2.addActionListener(this);
    

    Do

        jb1.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())-1));
            }
        });
    
        jb2.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())+1));
            }
        });
    

    (So App1 does not need to implement ActionListener)

    Note: using lambda expressions makes the above code look like:

    jb1.addActionListener(e -> jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())-1)));
    jb2.addActionListener(e -> jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())+1)));
    

    Side note: You are declaring variables twice.
    For example you have a field JTextField jtf1 and later JTextField jtf1 = new JTextField(6);. The field remains null.