I'm collaborating on a mature desktop Swing application
A customer wants a dialog that:
- Has a maximize button in the corner (I assume, the other standard frame buttons too, minimize and close)
- Has a relationship with the "root frame". For example, the root frame shouldn't be minimizable while the dialog is still visible
I reimplemented the dialog, which used to be a JDialog
, to be a JFrame
subclass. It solved the first problem, but now it's totally oblivious to any root frames at all as JFrame
s don't have parents
They say adding a maximize button to a JDialog
is possible but not a good idea (though, I'm not sure why). The app is used in medical facilities, I don't want to take any chances
How do I meet the customer's expectations?
UPD: Abra suggested calling JDialog.setDefaultLookAndFeelDecorated(true)
. There are a number of problems with that approach:
- Still no maximize button
- The look-and-feel will not match the global settings
public class Main {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Main Frame");
JButton popupButton = new JButton("Open Popup");
JDialog.setDefaultLookAndFeelDecorated(true);
popupButton.addActionListener(e -> {
JDialog popupDialog = new JDialog(mainFrame, "Popup Dialog");
popupDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popupDialog.setSize(200, 200);
popupDialog.setLocationRelativeTo(null);
popupDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
popupDialog.setVisible(true);
});
mainFrame.add(popupButton);
mainFrame.pack();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
}
By the way, the "unsafe" option (see the link above) has the same problem
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Main Frame");
JButton popupButton = new JButton("Open Popup");
//JDialog.setDefaultLookAndFeelDecorated(true);
popupButton.addActionListener(e -> {
JDialog popupDialog = new JDialog(mainFrame, "Popup Dialog");
popupDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popupDialog.setSize(200, 200);
popupDialog.setLocationRelativeTo(null);
popupDialog.setUndecorated(true);
popupDialog.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
popupDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
popupDialog.setVisible(true);
});
mainFrame.add(popupButton);
mainFrame.pack();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
JFrame
can't be made modal (afaik), which is what you need – LuzernpopupDialog.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
that doesn't work because it seems like it should. How do you feel about hiding all of the decorations and re-drawing them? – Hylophagous