How to bring jinternalframe to front from all open

2019-08-20 01:56发布

问题:

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.