Metal Wing
Metal Wing

Reputation: 545

Requesting Focus in Window

I have a simple (not?) problem with getting a focus on an JInternalFrame after disposing of JTable.

//I created a table, set it to visible, gave it focus.
list.dispose(); //I did what I wanted with the table and I dispose of it
screen.setVisible(true); //The screen is already visible. this isn't really needed
//After I closed table, I want the focus return to the screen (JInternalFrame)
screen.setFocusable(true); 
screen.requestFocus();
screen.requestFocusInWindow();
screen.requestFocus(true);
desktopPane.setSelectedFrame(screen);

So what's happening is this: The screen DOES get focus, in a sense that when I check for which component has focus - it does return me - screen. BUT the bar (not sure of proper terminology here) the bar on the top, that contains the icon, title, X thingy, is gray and not blue.

Any idea how I can make the JInternalFrame LOOK active, not just behave active?

Example: http://nexrem.com/projects/FocusDemo.rar I made this quickly in NetBeans and what you do is:

Click to open screen1. It opens and is focused. Click to open JTable. It opens and has focus. Screen1 lost focus. Hit Esc to close the JInternalFrame. It closes, but screen1 doesn't seem to have focus (visually at least). I am suspecting it may have something to do with focus cycling, since it is between 2 JInternalFrames...

Upvotes: 0

Views: 3260

Answers (1)

Guillaume Polet
Guillaume Polet

Reputation: 47608

You are looking for screen.setSelected(true); (I just tried it on your code and it works now).

As for @HFOE suggestion what he meant was to do something like this:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        screen.requestFocus();
        screen.requestFocusInWindow();
        screen.requestFocus(true);
    }
});

but I am not sure this will improve your situation.

Upvotes: 3

Related Questions