Search code examples
javajframejlabel

Update JLabel text


I'm working on a simple GUI. On Button press i want to increase/decrease a variable and update the corresponding JLabel.

class JFrameSetUp

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


public class JFrameSetUp extends JFrame implements ActionListener {

    private int RecHeight = 0;
    private int RecWidth = 0;

    //Here Buttons

    JButton HeightIncrease = new JButton("+");
    JButton HeightDecrease = new JButton("-");

    JLabel height = new JLabel(Integer.toString(RecHeight));
    JLabel width = new JLabel(Integer.toString(RecWidth));

    GridLayout gridLayout = new GridLayout(2, 4);

    public JFrameSetUp(){

    }

    public void addComponentsToPane(final Container pane){

        //Create GridPanel and set Layout
        JPanel grid = new JPanel();
        grid.setLayout(gridLayout);

        //Create buttondrawPanel and set Layout
        JPanel buttondraw = new JPanel();
        buttondraw.setLayout(new GridLayout(2, 0));

        //Adding Components to GridPanel

        //Adding Layouts to pane

        pane.add(grid, BorderLayout.NORTH);
        pane.add(new JSeparator(), BorderLayout.CENTER);
        pane.add(buttondraw, BorderLayout.SOUTH);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        //Setting up ActionListener to Buttons

        if (e.getSource() == this.HeightDecrease) {

            RecHeight -= 1;
            height.setText(Integer.toString(RecHeight));

        } else if (e.getSource() == this.HeightIncrease) {

            RecHeight += 1;
            height.setText(Integer.toString(RecHeight));
        }

    }

}

Class with MainMethod

import javax.swing.JFrame;


public class Program {

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();

            }
        });
    }



    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrameSetUp frame = new JFrameSetUp();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set up the content pane.
        frame.addComponentsToPane(frame.getContentPane());
        //Display the window.
        frame.pack();
        frame.setVisible(true);

    }

}

I'm aware, that's kind a newbish question. I think I'm wrong with my Code Structure. Any help is appreciated.

Thanks in advance.


Solution

  • You never register any ActionListeners to the buttons...

    HeightIncrease.addActionListener(this);
    HeightDecrease.addActionListener(this);
    

    You also never add the buttons to the GUI

    buttondraw.add(HeightIncrease);
    buttondraw.add(HeightDecrease);
    

    You also never add the labels to the GUI either...

    grid.add(height);
    grid.add(width);
    

    I reworked the code, because your example was messing with my mind, hope you don't mind...

    It's conceptually the same idea, just done slightly more efficently

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    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.JSeparator;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private int recHeight = 0;
            private int recWidth = 0;
    
            //Here Buttons
            JButton heightIncrease = new JButton("+");
            JButton heightDecrease = new JButton("-");
    
            JLabel height = new JLabel(Integer.toString(recHeight));
            JLabel width = new JLabel(Integer.toString(recWidth));
    
            GridLayout gridLayout = new GridLayout(2, 4);
    
            public TestPane() {
                setLayout(new BorderLayout());
                //Create GridPanel and set Layout
                JPanel grid = new JPanel();
                grid.setLayout(gridLayout);
    
                grid.add(height);
                grid.add(width);
    
                //Create buttondrawPanel and set Layout
                JPanel buttondraw = new JPanel();
                buttondraw.setLayout(new GridLayout(2, 0));
    
                heightIncrease.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        recHeight += 1;
                        height.setText(Integer.toString(recHeight));
                    }
                });
                heightDecrease.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        recHeight -= 1;
                        height.setText(Integer.toString(recHeight));
                    }
                });
    
                buttondraw.add(heightIncrease);
                buttondraw.add(heightDecrease);
    
                //Adding Components to GridPanel
                //Adding Layouts to pane
                add(grid, BorderLayout.NORTH);
                add(new JSeparator(), BorderLayout.CENTER);
                add(buttondraw, BorderLayout.SOUTH);
            }
    
        }
    }
    

    I would encourage you to spend some time having a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners for more details