Results 1 to 8 of 8
  1. #1
    fletcher is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default Returning focus to a frame after hiding another frame

    Here's some sample code so you can see the problem I'm running into.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class TestProgram {
    
    	private JFrame mainFrame = new JFrame();
    	private JFrame secondFrame = new JFrame();
    	private JFrame thirdFrame = new JFrame();
    	private JButton openSecondButton = new JButton("Open");
    	private JButton openThirdButton = new JButton("Open");
    	private JButton closeButton = new JButton("Close");
    	
    	public TestProgram() {
    		mainFrame.setTitle("Main");
    		secondFrame.setTitle("Second");
    		thirdFrame.setTitle("Third");
    		mainFrame.setSize(200,100);
    		secondFrame.setSize(200,100);
    		thirdFrame.setSize(200,100);
    		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		secondFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    		thirdFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    		
    		openSecondButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				secondFrame.setVisible(true);
    			}
    			
    		});
    		openThirdButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				thirdFrame.setVisible(true);
    			}
    		});
    		closeButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				thirdFrame.setVisible(false);
    				secondFrame.setVisible(false);
    				
    //				secondFrame.setVisible(false);
    //				thirdFrame.setVisible(false);
    			}
    		});
    		
    		mainFrame.add(openSecondButton);
    		secondFrame.add(openThirdButton);
    		thirdFrame.add(closeButton);
    		
    		mainFrame.setVisible(true);
    	}
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		new TestProgram();
    	}
    
    }
    This may look confusing. Each frame has a button. The first frame opens the second. The second frame opens the third. The third frame closes the second and third frame in one action.

    If you use the little X buttons in the window corners, the focus never leaves the program. However, if you use the close button in the third frame, the focus does not return to the remaining frame.

    For some reason, this is fixed merely by changing the order of the setVisible() methods. But in my actual program, I cannot adjust this order (since the analogous "third" frame is a JOptionPane).

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

    Default

    Sorry, I'm confused. Exactly how does this posted code not work? Oh, and I would tend to use only one JFrame in my app, and the rest dialogs such as JDialog.

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    See How to Use the Focus Subsystem.

    Usually it suffices to request the focus for the place you want to have focus:
    Java Code:
    mainframe.requestFocusInWindow();
    I would write this after setting the visibility.

    And: Yes, I agree about redesigning the GUI.
    Dialog boxes are not user-friendly,
    but flashing multiple windows are obnoxious.
    Last edited by zweibieren; 10-29-2009 at 02:30 PM. Reason: concur with redesign suggestion

  4. #4
    fletcher is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    It is a spaced-repetition flash card program. The main window needs to be minimalist so the user isn't distracted while flashing cards. Editing/searching the database of flashcards opens a new larger window. You can also edit/add cards from the main window or database window, which opens yet another window for input. The edit/add dialog displays a popup alert on complete and then doesn't always return focus to the program. I have considered a single window design but it would be like trying to use flash cards surrounded by the clutter of your typical IDE.

    I can't very easily use requestFocusInWindow(), even if I track the window which opened the add/edit dialog. This is because I want to allow functionality whereby a user can open add card from the main window, realize he needs to check which cards already exist, open the database, then return to the add/edit dialog. On closing the add/edit dialog, focus should return to whatever the topmost window is, not the window which called the dialog, but I also don't want to give focus to a window if the user has minimized them for some reason.

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    The original problem was to direct the input focus.
    But just reading your description made me lose focus.
    When I regained consciousness I felt it necessary to step back.
    Look at the whole application.

    The fact that you can't reasonably use requestFocusInWindow
    is a hint that the design is flawed. If you can't figure out what to do,
    how is the user supposed to cope?

    Have you tried explaining the user interface to a prospective user?
    Use HTML with FORMs to produce the various window designs
    and show how it behaves. See the Gee demo for an example.

    You will do your users a great service by discarding
    everything you think you know about the design of this user interface.
    Start again.

    Possibly a CardLayout will help.

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

    Default

    Quote Originally Posted by zweibieren View Post
    The original problem was to direct the input focus.
    But just reading your description made me lose focus.
    Phew! I thought it was just me!

    I second all your recs: Re-design from the ground up and look at CardLayout.

    I'd rep you, but it won't let me re-rep you this soon.

  7. #7
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Another useful approach is a JTabbedPane.

  8. #8
    arulmozs is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Help frame is not displaying.
    By gammaman in forum New To Java
    Replies: 24
    Last Post: 07-22-2009, 01:00 PM
  2. GUI new frame
    By billbo123 in forum New To Java
    Replies: 15
    Last Post: 03-02-2009, 04:24 AM
  3. frame
    By arunkumarinfo in forum NetBeans
    Replies: 0
    Last Post: 02-07-2009, 10:26 AM
  4. Frame to other Frame
    By Aswq in forum New To Java
    Replies: 2
    Last Post: 07-19-2008, 04:27 PM
  5. Hiding the frames title bar
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:41 AM

Posting Permissions

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