We had a request that some dialogs should always be on top based on some control. This must be switched on/off on a per dialog basis. Using the following code, and going through the following steps seems to me like a Java bug:
- Set dialog1 on top. Check it stays on top.
- Set dialog2 on top. Check both dialogs stay on top.
- Set dialog1 not on top.
Expected: dialog2 remains on top and dialog1 doesn't
Actual: none of the dialogs remain on top.
This has some connection with the owner of the dialogs. If you uncomment the block comments, everything works as expected but I don't think this is normal behaviour since Always on Top should not be connected to the parent. Is this normal or is it an issue with Swing? Unfortunately setting a new owner for each dialog introduces other issues.
public static void main(String[] args) {
final JFrame rahan = new JFrame("Rahan");
rahan.setSize(new Dimension(1000,1000));
rahan.setVisible(true);
JDialog d1 = getJDialog(/*new JFrame()*/);
JDialog d2 = getJDialog(/*new JFrame()*/);
}
private static JDialog getJDialog(/*JFrame owner*/) {
final JDialog jDialog = new JDialog(/*owner*/);
final JButton onTop = new JButton("OnTop");
onTop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jDialog.setAlwaysOnTop(!jDialog.isAlwaysOnTop());
}
});
jDialog.add(onTop);
jDialog.setVisible(true);
jDialog.pack();
return jDialog;
}