java JButton setEnabled isn't working properly

78 views Asked by At

I have a JButton that is setEnabled(false) as default, and then if a condition from another class is true, that button will be enabled, but the button isn't being enabled at all. All 3 classes are from the same package.

Main.java(main class)

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        OtherClass c1 = new OtherClass();
        Panel p = new Panel();
        frame.add(p);
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        //p.setButtonEnabled();
    }
    
}

Panel.java

import javax.swing.JButton;
import javax.swing.JPanel;

public class Panel extends JPanel{
    JButton b1 = new JButton("OK"); ;
    
    public Panel(){
        b1.setSize(50, 50);
        b1.setEnabled(false);
        add(b1);
    }
    
    public void setButtonEnabled(){
        b1.setEnabled(true);
    }
}

OtherClass.java

public class OtherClass {
    Panel p1 = new Panel();
    
    //some method
    public void method(){ 
        if (true){
            p1.setButtonEnabled();
        }
    }
}

But when I called the setButtonEnabled() method from the main method, it somehow worked, can someone explain why and how to fix it, I just learned Java for about a month and this is my first time using Swing, thanks a lot.

3

There are 3 answers

4
Adarsh Singh On BEST ANSWER

The button's state remains unaffected because setButtonEnabled() is invoked on a separate instance of Panel, which doesn't reflect the instance added to the frame in the main method.

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Panel p = new Panel();
        OtherClass c1 = new OtherClass(p); // Pass the Panel instance to OtherClass
        frame.add(p);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

public class OtherClass {
    private Panel p1;

    public OtherClass(Panel p1) {
        this.p1 = p1;
    }
    
    public void method(){ 
        if (true){
            p1.setButtonEnabled();
        }
    }
}
6
Adarsh Singh On

Referring your comment

Modify Your Panel class and add an ActionListener and on tap you can call method from the instance of Other class Passed to Panel class

import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Panel extends JPanel {
    JButton b1 = new JButton("OK");
    OtherClass otherClass; // Reference to OtherClass

    public Panel(OtherClass otherClass) {
        this.otherClass = otherClass; // Store the reference to OtherClass instance
        b1.setSize(50, 50);
        b1.setEnabled(false);
        add(b1);

        // Add ActionListener to the button
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Call a method from OtherClass instance when the button is clicked
                otherClass.executeMethod();
            }
        });
    }

    public void setButtonEnabled() {
        b1.setEnabled(true);
    }
}
1
Adarsh Singh On
public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        OtherClass c1 = new OtherClass();
        Panel p = new Panel(c1);
        frame.add(p);
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    
}

public class Panel extends JPanel {
    private JButton b1 = new JButton("OK");
    private OtherClass otherClassInstance; 
    
    public Panel(OtherClass otherClassInstance) {
        this.otherClassInstance = otherClassInstance; 
        b1.setSize(50, 50);
        b1.setEnabled(false);
        add(b1);
    }
    
    public void setButtonEnabled() {
        b1.setEnabled(true);
    }
   
    public void executeMethodFromOtherClass() {
        otherClassInstance.method(); 
    }
}

public class OtherClass {
    
    public void method() { 
    }
}