0

I am making a small Java project in which there are 3 frames connected together, say f1, f2, f3 & f4. f1 is having a button and after clicking it it calls f2 and f2 also has button which calls f3. The problem is if I opened all the form one by one then when I close form f2 or f3 then the main form(f1) gets closed.

I want that even I close form f2 and f3 my main form should not be close until I close it personally.

package mnm;

public class NewJFrame extends javax.swing.JFrame {


    public NewJFrame() {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        NewJFrame1 nb=new NewJFrame1();
        nb.setVisible(true);
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame().setVisible(true);
                NewJFrame gn=new NewJFrame();
                gn.setDefaultCloseOperation(NewJFrame.DISPOSE_ON_CLOSE);
            }
        });
    }

    private javax.swing.JButton jButton1;

}
user2096592
  • 53
  • 1
  • 2
  • 8
  • 1
    Maybe you could show us some of your code, if frame 1 gets closed when you close another frame, there must be a reference of your main frame in the others that you missed. OR maybe an issue when instantiate the frames, you can use dialogs if you want multiple windows or another option is to use JInternalFrames. – Marcelo Tataje Feb 22 '13 at 13:34
  • Use upper case where appropriate (e.g. start of every sentence, proper names like Java, the word I). This makes it easier for the reader. You would not try to make it *harder* for us to help, would you? – Andrew Thompson Feb 22 '13 at 13:34
  • In the event of button i make the object of the other fram that i have to open and suppose f2 is the form. then f2 bb=new f2(); bb.setVisible(true); Thats the only codes i used in all the form. – user2096592 Feb 22 '13 at 13:41

1 Answers1

5

..3 frames connected together

Don't do it! See The Use of Multiple JFrames, Good/Bad Practice? for details. Two of those frames should be either modal dialogs or a JOptionPane.


I want that even I close form f2 and f3 my main form should not be close until I close it personally.

Now that I've warned you against it, I'll add..

The behavior requested can be achieved by setting a default close operation of DISPOSE_ON_CLOSE as seen in this answer. Any of those frames can be closed without affecting the others.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks thompson. In that link that guy said about jInternalFrame. Can u give me any link to use it in my Java Project so that i will not use multiple window. :) – user2096592 Feb 22 '13 at 13:49
  • Here is a tip. Search on 'classname+java+tutorial' and see what hits turn up. E.G. for this it would be 'jinternalframe+java+tutorial'. It has good instructions and usually includes examples with code. – Andrew Thompson Feb 22 '13 at 13:59
  • Again thanks for the codes in that answer. Was that your code? i mean how you know these codes.? I also want to know different techniques in Java. – user2096592 Feb 22 '13 at 14:00
  • Yes, I see the code. What about it? As to the code on the link I posted. Yes, that (very short code) was written by me. – Andrew Thompson Feb 22 '13 at 14:19
  • i use that DIspose function in other form too. But it still getting close if i close any of the Frame. May be i put that code in wrong place. Please see the code again. – user2096592 Feb 22 '13 at 14:31
  • The code you posted does not compile because `NewJFrame1` is not included. I suspect the problem is that `NewJFrame1` also sets `EXIT_ON_CLOSE` just like the unaltered `NewJFrame` does. – Andrew Thompson Feb 22 '13 at 14:37
  • NewJFrame1 is the second frame that i made in the same package in Netbeans. I didnot put its code bcz the code is very similar to it. And the code is compiling. – user2096592 Feb 22 '13 at 14:41
  • For better help sooner, post an [SSCCE](http://sscce.org/). Or in other words, it needs to be ***compilable code.*** *"the code is very similar to it."* It is a **one line difference** between working as I described, & failing as you've seen. – Andrew Thompson Feb 22 '13 at 14:44
  • That is the auto generated code. and your code was made by you. The problem is i dont know where to put NewJFrame gn=new NewJFrame(); gn.setDefaultCloseOperation(NewJFrame.DISPOSE_ON_CLOSE); in the above auto generated code. – user2096592 Feb 22 '13 at 14:48
  • *"The problem is.."* ..unless you post an SSCCE, I'm not investing (or rather 'wasting' given the guessing involved) any more time trying to help you. – Andrew Thompson Feb 22 '13 at 14:52
  • That code might be short, ***but it is not an SSCCE!*** Again, it does not compile here.. `I:\projects\NewJFrame.java:7: cannot find symbol symbol : method initComponents() location: class mnm.NewJFrame initComponents(); ^ I:\projects\NewJFrame.java:12: cannot find symbol symbol : class NewJFrame1 location: class mnm.NewJFrame NewJFrame1 nb=new NewJFrame1(); ^ I:\projects\NewJFrame.java:12: cannot find symbol symbol : class NewJFrame1 location: class mnm.NewJFrame NewJFrame1 nb=new NewJFrame1(); ^ 3 errors` – Andrew Thompson Feb 22 '13 at 15:15
  • Its a Netbeans project not a notepad one. And i cant see any option here to send you the zip file of my project. – user2096592 Feb 22 '13 at 15:23
  • *"Its a Netbeans project not a notepad one"* I do most of my SSCCEs in Netbeans, but don't offer any help with using it (there are far better Netbeans experts around). At the moment, you are the IDEs 'bitch'. It owns you, controls you and limits you. I suggest you stop that nonsense by not using Netbeans until you are comfortable with writing Java code in a simpler editor like [TextPad](http://www.textpad.com/), I reach for it every time I could not be bothered trying to figure out how to do something using Netbeans. And no, I don't want 'a zip file' of your entire codes. – Andrew Thompson Feb 22 '13 at 15:30
  • 1
    you are right. i should not use netbeans. Actually i do drag and drop and thats limits me. i will try my best :) – user2096592 Feb 22 '13 at 15:34