Results 1 to 8 of 8
- 10-29-2009, 06:11 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
Returning focus to a frame after hiding another frame
Here's some sample code so you can see the problem I'm running into.
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.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(); } }
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).
-
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.
- 10-29-2009, 02:27 PM #3
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
See How to Use the Focus Subsystem.
Usually it suffices to request the focus for the place you want to have focus:
I would write this after setting the visibility.Java Code:mainframe.requestFocusInWindow();
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
- 10-29-2009, 04:19 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
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.
- 10-29-2009, 05:49 PM #5
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
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.
-
- 10-29-2009, 06:41 PM #7
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
Another useful approach is a JTabbedPane.
- 11-02-2009, 06:31 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Help frame is not displaying.
By gammaman in forum New To JavaReplies: 24Last Post: 07-22-2009, 01:00 PM -
GUI new frame
By billbo123 in forum New To JavaReplies: 15Last Post: 03-02-2009, 04:24 AM -
frame
By arunkumarinfo in forum NetBeansReplies: 0Last Post: 02-07-2009, 10:26 AM -
Frame to other Frame
By Aswq in forum New To JavaReplies: 2Last Post: 07-19-2008, 04:27 PM -
Hiding the frame’s title bar
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 08:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks