How to bring jinternalframe to front from all opened jinternalframes?

淺唱寂寞╮ 提交于 2019-12-13 05:46:35

问题


This is my source code. I can't get my JInternalframe to the front. I have tried many codes out there, but nothing worked.

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    boolean b = true;
    JInternalFrame[] j = jDesktopPane1.getAllFrames();
    no = new NewOrder();

    for (JInternalFrame Ji : j) {
        if (Ji.isShowing()) {
            b = false;
            break;
        }
    }

    if (!b) {
      no.requestFocus(true);
    } else {
        dm = jDesktopPane1.getDesktopManager();
        jDesktopPane1.add(no);
        dm.setBoundsForFrame(no, (jDesktopPane1.getWidth() - no.getWidth()) / 2, (jDesktopPane1.getHeight() - no.getHeight()) / 2, no.getWidth(), no.getHeight());
        dm.openFrame(no);
        no.setVisible(true);
    }


} 

and

NewOrder no = new NewOrder();

if (no.isShowing()) {
    no.toFront();
} else {
    lo.LoadInterfaces(no, jDesktopPane1);
}

Can any body please explain me why this was happened? Thank you!


回答1:


Use the setSelected() method of JInternalFrame; a complete example is cited here. For convenience, invoke the method from an Action, as shown here.

Addendum: A typical implementation might look like this.

class MyFrame extends JInternalFrame {

    private Action action;

    MyFrame(…) {
        …
        action = new AbstractAction(name) {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    MyFrame.this.setSelected(true);
                } catch (PropertyVetoException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    public Action getAction() {
        return action;
    }
}



回答2:


I found it..

    NewOrder no = new NewOrder();

    if (no.isShowing()) {
        no.toFront();
    } else {
        lo.LoadInterfaces(no, jDesktopPane1);
    }

It was just remove the initialization.. And do initialization and declaration at once. Can any body please explain me why this was happened. Please.



来源:https://stackoverflow.com/questions/26699125/how-to-bring-jinternalframe-to-front-from-all-opened-jinternalframes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!