user12316287
user12316287

Reputation:

Can we change a JLabel from another class in java?

I am new to java and I am trying to use a JLabel and a JButton. My problem is that I can't change the JLabel in the actionPerformed() method. It cannot find the variable nbr. Please help :(

package com.test;

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame implements ActionListener{

    int x = 0;

    public static void main(String[] args) {
        new Main().setVisible(true);
    }

    public Main(){
        super("Test");
        setSize(640, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout());
        JButton button1 = new JButton("Button1");
        button1.addActionListener(this);
        add(button1);
        JLabel nbr = new JLabel(x);
        add(nbr);
    }

    public void actionPerformed(ActionEvent e){
        x++;
        nbr.setText(x);
    }
}

Upvotes: 1

Views: 75

Answers (3)

Dave The Dane
Dave The Dane

Reputation: 869

You could also try something like this:

public class Main extends JFrame {

    public static void main(final String[] args) {
        new Main().setVisible(true);
    }

    public Main(){
        super("Test");
        setSize(640, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout());

        final JButton button1 = new JButton("Button1");
        final JLabel  nbr     = new JLabel("0");
        add(button1);
        add(nbr);

        final AtomicInteger x = new AtomicInteger();

        button1.addActionListener(e -> {
            nbr.setText("" + x.incrementAndGet());
        });
    }
}

Upvotes: 0

Abra
Abra

Reputation: 20914

If you prefer not to make nbr a member of class Main but keep it as a local variable in the class constructor, you can still use the Swing API to update the JLabel text from the actionPerformed() method. Sample code below. Note that both the JLabel constructor and its setText() method require a String argument and not a int argument, hence the code you posted does not compile. I suggest you read How to create a Minimal, Reproducible Example. In any case, I have fixed that error in the below code.

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Main extends JFrame implements ActionListener {
    int x = 0;

    public static void main(String[] args) {
        new Main().setVisible(true);
    }

    public Main(){
        super("Test");
        setSize(640, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout());
        JButton button1 = new JButton("Button1");
        button1.addActionListener(this);
        add(button1);
        JLabel nbr = new JLabel(String.valueOf(x));
        add(nbr);
    }

    public void actionPerformed(ActionEvent e){
        x++;
        Container contentPane = getContentPane();
        Component[] cmpts = contentPane.getComponents();
        for (Component cmpt : cmpts) {
            if (cmpt instanceof JLabel) {
                JLabel nbr = (JLabel) cmpt;
                nbr.setText(String.valueOf(x));
            }
        }
    }
}

Upvotes: 0

Saud
Saud

Reputation: 878

That is because you are accessing a variable that is in different and limited scope. In your case your nbr is defined in your Main() function and is limited only to that scope. If you want to access it you have to make it global, means you have to declare it in your class.

Example:

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame implements ActionListener{

    int x = 0;
    private JLabel nbr; // declare a global variable

    public static void main(String[] args) {
        new Main().setVisible(true);
    }

    public Main(){
        super("Test");
        nbr = new JLabel(); // initialize here...

        setSize(640, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout());
        JButton button1 = new JButton("Button1");
        button1.addActionListener(this);
        add(button1);
        nbr.setText(x); // set text to that variable here...
        add(nbr);
    }

    public void actionPerformed(ActionEvent e){
        x++;
        nbr.setText(x);
    }
}

P.s: Check for any syntax errors, I wrote this without testing. :)

Upvotes: 2

Related Questions