0

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.

Nicke Manarin
  • 3,026
  • 4
  • 37
  • 79
  • 1
    Try using `city.requestFocusInWindow` instead – MadProgrammer Nov 22 '12 at 05:37
  • 1
    `city.requestFocus();` See [`JComponent.requestFocus()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocus%28%29). *"Note that the use of this method is discouraged because its behavior is platform dependent. Instead we recommend the use of **[`requestFocusInWindow()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocusInWindow%28%29)**.."* – Andrew Thompson Nov 22 '12 at 05:39
  • I already tried this, nothing happens too. – Nicke Manarin Nov 22 '12 at 06:43
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 22 '12 at 07:45
  • Okay. Give a look, perhaps my listener have something wrong. – Nicke Manarin Nov 22 '12 at 19:21
  • Take a look into these links. [oracle.com](http://docs.oracle.com/javase/7/docs/api/javax/swing/JInternalFrame.html) [coderanch.com](http://www.coderanch.com/t/334157/GUI/java/JInternalFrame-Focus) – Nidhish Krishnan Nov 22 '12 at 05:31

1 Answers1

0

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

Nicke Manarin
  • 3,026
  • 4
  • 37
  • 79