Results 1 to 5 of 5
  1. #1
    Shellback3 is offline Member
    Join Date
    Jan 2011
    Location
    Riveside, CA
    Posts
    16
    Rep Power
    0

    Question how to create a "child" frame.

    Standard disclaimer: I'm new to Java especially to AWT/SWING.

    In developing my project I realized that I couldn't create a GUI until I had read data from a file. Since I had only created one Java Gui I created one as a class that implemented a file chooser. It had one button: "Browse". When this worked as I wished I started on step two.

    For initial development I created a class with a main and used a fixed pathname to facilitate debugging. I used the pathname to access the file and used the data found there to construct from 1 though n JLables and JTextFields.

    When this worked I invoked this class from its public method from the class with the browse button and continued development. It all works now *almost* correctly! The only bug is that when I close the second GUI the first closes as well which I don't want.

    My question is: how do I build and display a "child" GUI from a "parent" GUI correctly?

    Code similar to this appears in both GUIs.
    Java Code:
    	private static void createAndShowGUI() {
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		frame.addWindowListener(new WindowAdapter() {
    			@Override
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm not surpised that this behavior is occurring since you're telling the JVM to exit the program if the window closes in your WindowListener above. So if you don't want this to happen, don't do it.

    Sometimes it's better to avoid closing one GUI and opening another but instead swapping views such as can be done with a CardLayout. Otherwise if you absolutely must display two JFrames, one after the other, you'll want to make sure to set the JFrame's default close operation correctly to JFrame.DISPOSE_ON_CLOSE. Again this is done by calling the setDefaultCloseOperation(int i) method of JFrame.

  3. #3
    Shellback3 is offline Member
    Join Date
    Jan 2011
    Location
    Riveside, CA
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm not surpised that this behavior is occurring since you're telling the JVM to exit the program if the window closes in your WindowListener above. So if you don't want this to happen, don't do it.

    Sometimes it's better to avoid closing one GUI and opening another but instead swapping views such as can be done with a CardLayout. Otherwise if you absolutely must display two JFrames, one after the other, you'll want to make sure to set the JFrame's default close operation correctly to JFrame.DISPOSE_ON_CLOSE. Again this is done by calling the setDefaultCloseOperation(int i) method of JFrame.
    Thanks, that's just what I needed. I didn't know enough Java and Swing to do it any other way but I would like to get rid of one of the frames and include all the code in just one. My frame has three panes but the contents of the middle one are not known until a file is read at runtime.

    Can I display the frame so that I have a "browse" button available, and then populate the middle frame and cause it to re-display? Would that be a re-paint?

    If I can do that I guess that when the user uses the "browse" button to choose a new file I could remove all the components on the pane and proceed as before, is that true?

    I FIGURED OUT HOW TO DO THIS.
    Last edited by Shellback3; 02-01-2011 at 10:58 PM. Reason: Found solution

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Shellback3 View Post
    Can I display the frame so that I have a "browse" button available, and then populate the middle frame and cause it to re-display?
    Yes, absolutely.


    Would that be a re-paint?
    Probably. You also might have to revalidate the container (usually a JPanel) that holds your components if any of the components are added or removed.


    If I can do that I guess that when the user uses the "browse" button to choose a new file I could remove all the components on the pane and proceed as before, is that true?
    That may be one way. Another is again to use card layout.


    I FIGURED OUT HOW TO DO THIS.
    I just saw this. Nevermind I guess.

  5. #5
    Shellback3 is offline Member
    Join Date
    Jan 2011
    Location
    Riveside, CA
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Get "Frame"(?) as a methode
    By Mattedatten in forum New To Java
    Replies: 3
    Last Post: 11-10-2009, 12:31 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Using for loops to create a "bridge" made out of hyphens
    By carlodelmundo in forum New To Java
    Replies: 7
    Last Post: 09-21-2008, 11:20 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •