Search code examples
javaswingfocusjinternalframejdesktoppane

My internal frames don't earn focus after opening... (Java)


I have a menu with items that open internal frames, but every time I need to click twice in the frame. One time to give focus to the Int.frame and the second time to actually do something (give focus to a textfield).

So, here is my question: It's possible to automatic give focus to the Int.Frame?


Code of my main screen:

public final class principal extends javax.swing.JFrame {

viewCity city = new viewCity();

public principal() {
    initComponents();
    myListeners();
    setLocationRelativeTo(null);
}

public void myListeners() {
    menuCity.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            menuCityClicked(e);
        }
    });
}

public void central(JInternalFrame window1) {

    int lDesk = panelPrincipal.getWidth();
    int aDesk = panelPrincipal.getHeight();
    int lIFrame = window1.getWidth();
    int aIFrame = window1.getHeight();

    window1.setLocation(lDesk / 2 - lIFrame / 2, aDesk / 2 - aIFrame / 2);
}

private void menuCityClicked(MouseEvent e) {
    if (!city.isVisible()) {
        panelPrincipal.add(city);
        central(city);
        city.requestFocus(); // Nothing
        city.requestFocusInWindow(); // Nothing
        city.setVisible(true);
        city.requestFocus(); // Nothing
        city.requestFocusInWindow(); // Nothing

    }
}}

No matter what, the menu will always keep the focus. For example, click in your browser's menu, and you will keep the focus, by moving the cursor you will open other menus without need to click.


By putting the properties "selection model" to null works, but give me nullpointerexception.


Solution

  • Ok, the problem is with the jMenu, but with jMenuItem Works fine, so... I'm using