0

I am new to NetBeans and GUI development. I was wondering how I can design a main form that has menus that open up other forms. I know how to design a form with a button and an event listener (actionperformed), like so:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       new SeconfFrame().setVisible(true);        // TODO add your handling code here:
}

This will call the second frame when button is pushed, but when I exit the second frame the first one goes away too. Not cool. How can I get the first frame (the main one) to still stay up?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1456559
  • 917
  • 1
  • 7
  • 9

3 Answers3

2

This will call the second frame when button is pushed, but when I exit the second frame the first one goes away too. Not cool. How can I get the first frame (the main one) to still stay up?

you have to change setDefaultCloseOperations(EXIT_ON_ClOSE) to HIDE_ON_CLOSE

was wondering how I can design a main form that has menus that open up other forms.

use CardLayout rather than bothering with Top-Level Containers

mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

Recommendations:

  • If you must have one window open another, have the dependent window be a dialog window such as a JDialog or JOptionPane. Do this, and you won't have to worry about the JVM exiting.
  • And yes, a JDialog can hold as complicated a GUI as any JFrame.
  • Having said that, you don't want to have an application with lots of windows bouncing in and out of view. If you do, re-think your design, including use of CardLayout as mKorbel recommended.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

On the 'properties' tab of the JFrame in question goto the Tab: Set 'defaultCloseOperation' (top of the list) and change this from 'EXIT_ON_CLOSE' to 'DISPOSE' see: DISPOSE_ON_CLOSE

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • what exactly do you mean by the jFrame in question? Do you mean the one i want to stay active or the second frame? – user1456559 Jun 14 '12 at 21:06
  • the 2nd frame the one you want to close without exiting the entire application – David Kroukamp Jun 14 '12 at 21:10
  • nope. It sitll doesnt work. When i close the second frame the first one also closes. I set the defaultCloseOperation to Dispose and the first one to exit on close but when i exit out of the second one the first one exits too – user1456559 Jun 14 '12 at 21:14
  • hmm it should work? see this:http://netbeans-org.1045718.n5.nabble.com/GUI-Builder-Changing-default-close-operation-td5710571.html – David Kroukamp Jun 14 '12 at 21:17
  • Are you sure it never worked? Is your main frame defaultCloseOperation: EXIT_ON_CLOSE. and is your second frames defaultCloseOperation set to DISPOSE, thus closing the second frame shouldnt close the first one, to do this use System.exit(0); because it worked on mine? – David Kroukamp Jun 14 '12 at 21:24
  • Not the right strategy here. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jun 15 '12 at 04:37