2

I have two frames and I want to click the button in frame2 and disable the button in frame1. Is this possible? The program begins its execution from frame1 and opens frame2. It's from this frame2 where I want to disable that button in frame1 But it does NOT work.....How can it be done?

additional information: I have a similar problems like this when I work with panels as well. I just don't get it. Plz help!

Here is the coding for frame1 where the program begins execution:

public class Frame1 extends javax.swing.JFrame {

Frame2 frm2 = new Frame2();    

public Frame1() {
    initComponents();        
}    

public void buttonDisable(){
    Btn1.setEnabled(false);
}
private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    frm2.setVisible(true);
}                                    

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Frame1().setVisible(true);
        }
    });
}

public javax.swing.JButton Btn1;

}

Here is the coding for frame2 where I want to disable that button from:

public class Frame2 extends javax.swing.JFrame {

public Frame2() {
    initComponents();
}                       

private void Btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    Frame1 frm1 = new Frame1();
    frm1.buttonDisable();
}                                    

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Frame2().setVisible(true);
        }
    });
}

public javax.swing.JButton Btn2;

}
Anafam
  • 127
  • 1
  • 2
  • 12
  • 1
    An application should only have a single JFrame. See: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice. Make frame2 a model JDialog, then you don't have to worry about disabling the button. – camickr Apr 05 '13 at 19:51
  • Thanks, I wasn't too sure how Dialog's work but I looked into it a bit and now i'm gonna use them for such purposes :) – Anafam Apr 12 '13 at 12:41

1 Answers1

3

It's quite simple and your abstraction would work just fine. Here's a basic way to achieve what you want:

class FrameTwo extends JFrame {
    private FramOne firstFrame;

    public FrameTwo(FrameOne firstFrame) {
        this.firstFrame = firstFrame;
    };

    public void doSomething() {
        System.out.println(this.firstFrame.someMethod());
    };
};

Basically I am passing an instance of the FrameOne class to the FrameTwo constructor. There are a few options to achieve this, using static members, using both frames as instances of some class, using a View abstraction, etc. Your question is simple OOP, perhaps read more on that.

Also, note you can add import javax.swing.Frame to the top of your file and then you can just type class FrameOne extends Frame instead of class FrameOne extends javax.swing.Frame;

MOST IMPORTANT

It is extremely uncommon and strongly discouraged to use multiple frames for the same application. I would consider creating views that use a JPanel to nest elements and then you can switch panels and dialogs inside the same frame.

flavian
  • 28,161
  • 11
  • 65
  • 105
  • I'm not sure how FrameOne will look, can u plz add the FrameOne class to your code? I made this using Netbeans so it adds the imports on the fly. Yeah I'm sort of a new Java learner but not bad. This was just a simple coding to understand what i'm doing wrong. – Anafam Apr 05 '13 at 20:06
  • Yeah actually I am using panels for everything in my Applicaton but I just have this small Login screen which opens up in a separate JFrame. That Login screen is accessed using a file menu, which is part of a single Jframe which is used to display all those other panels :) – Anafam Apr 05 '13 at 20:11
  • While technically correct, it does have the side effect of exposing the first frame to the second frame. This allows the second frame the ability to change the firs frame, which you may not want. You could take a look at [this](http://stackoverflow.com/questions/15826951/how-to-change-setindeterminate-jprogressbar-in-other-frame/15827130#15827130) which discusses a simular problem – MadProgrammer Apr 06 '13 at 02:28