I've been looking at various questions about JInternalFrames and have tried to implement the code as suggested, but I appear to be doing something wrong. The code that I'm including here will demonstrate my problem. The three frames are created, but as you click and move them around, they will not come to the top as expected and will often times look like some sort of Escher print.
public class IntFrmTest extends JFrame {
private JPanel contentPane;
private JDesktopPane desktop;
private int formHeight=675;
private int formWidth=950;
public IntFrmTest() {
this.setTitle("Desktop");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(10, 10, formWidth, formHeight);
this.setMinimumSize(new Dimension(640,480));
this.setResizable(true);
desktop = new JDesktopPane();
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
this.add(BorderLayout.CENTER, desktop);
this.setVisible(true);
this.setLocationRelativeTo(null);
frmTesting frmTest1=new frmTesting(1);
frmTest1.setVisible(true);
contentPane.add(frmTest1);
frmTest1.setLocation(10,10);
frmTest1.addFocusListener(focusListener);
frmTesting frmTest2=new frmTesting(2);
frmTest2.setVisible(true);
contentPane.add(frmTest2);
frmTest2.setLocation(40,40);
frmTest2.addFocusListener(focusListener);
frmTesting frmTest3=new frmTesting(3);
frmTest3.setVisible(true);
contentPane.add(frmTest3);
frmTest3.setLocation(80,80);
frmTest3.addFocusListener(focusListener);
}
FocusListener focusListener=(new FocusListener(){
@Override
public void focusGained(FocusEvent arg0) {
JInternalFrame f = (JInternalFrame) arg0.getSource();
try {
f.setSelected(true);
f.moveToFront();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
System.out.println("Got Focus "+f.getName());
}
@Override
public void focusLost(FocusEvent arg0) {
JInternalFrame f = (JInternalFrame) arg0.getSource();
try {
f.moveToBack();
f.setSelected(false);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
System.out.println("Lost Focus "+f.getName());
}
});
public class frmTesting extends JInternalFrame {
private int formHeight=375;
private int formWidth=450;
public frmTesting(int frameNo) {
this.setTitle("Internal Frame "+frameNo);
this.setName("frmTest"+frameNo);
this.setClosable(true);
this.setResizable(false);
this.setMaximizable(false);
this.setIconifiable(false);
this.setFocusable(true);
this.setSize(formWidth,formHeight);
JPanel pnlDisplay = new JPanel();
this.setContentPane(pnlDisplay);
pnlDisplay.setLayout(null);
this.setVisible(true);
}
}
public static void main(String[] args) {
IntFrmTest ift=new IntFrmTest();
}
}
I am expecting the windows to simply come to the front as they are selected but that does not happen most of the time. When clicked, they may not show as selected and often times do not come to the top.
Here's an example of how it can look: Visually Incorrect Example
Any help explaining what I'm doing wrong would be greatly appreciated.
As usual, I spend all day trying to figure out a problem and find the answer after I've asked for help...
Perhaps someone can fill in more details, but I've been creating single forms with a desktop/contentpane combination and it's been working as expected. What I did not suss out of the docs for JInternalFrames is that it appears a JInternalFrame resides on the desktop and there is no contentpane on the main window.
When I was putting the internal frames onto the desktop/contentpane, it would not allow the frames to come to the top when selected. This led me to believe I needed to control the internal frame manually and thus the FocusListener was used.
Removing the contentpane from the mix fixes the issue and everything works as expected, which is a relief as I could not understand why it would need to be done manually. The corrected code is as follows.
I stumbled onto the answer when I started researching the rootpane.