Java Utililty Methods JFrame

List of utility methods to do JFrame

Description

The list of methods to do JFrame are organized into topic(s).

Method

ActionListenernewExitActionListener(final JFrame jFrame)
new Exit Action Listener
return e -> {
    switch (JOptionPane.showConfirmDialog(jFrame, "Are you sure you want to quit?")) {
    case JOptionPane.OK_OPTION:
        System.exit(0);
        break;
    default:
        break;
};
JFramenewJFrame(Component c, String title, int w, int h)
new J Frame
JFrame f = new JFrame(title);
f.getContentPane().add(c, BorderLayout.CENTER);
f.setSize(w, h);
f.pack();
return f;
JDialognewModalDialog(JFrame owner, String title, JPanel content, JButton... buttons)
Builds a standard modal input dialog, with content and buttons to accept or cancel that content.
java.awt.GridBagConstraints gridBagConstraints;
int i = 0;
for (JButton b : buttons) {
    i++;
    b.setPreferredSize(new java.awt.Dimension(100, 24));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = (i + 2);
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
...
intoptionDialog(String title, String text, String option1, String option2, int defaultOption, JFrame parentFrame)
option Dialog
if (defaultOption < 1)
    defaultOption = 1;
if (defaultOption > 2)
    defaultOption = 2;
Object[] options = { option1, option2 };
int n = JOptionPane.showOptionDialog(parentFrame, text, title, JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE, null, options, options[defaultOption - 1]);
n = n + 1;
...
voidpackFrame(JFrame frame)
pack Frame
frame.pack();
voidpackJFrameWindow(final JComponent comp)
i seperated it in different method to avoid affecting other components.
if (comp.getRootPane() != null) {
    final Container cont = comp.getRootPane().getParent();
    if (cont instanceof JFrame) {
        ((JFrame) cont).pack();
        ((JFrame) cont).setLocationRelativeTo(null);
voidPOPUP(JFrame frame, String message)
POPUP
JOptionPane.showMessageDialog(frame, message);
voidpopupError(JFrame parent, String title, String text)
Popup an Error message dialog
JOptionPane.showMessageDialog(parent, text, title, JOptionPane.ERROR_MESSAGE);
voidposition(JFrame f)
Centers a JFrame so that its position is in the center of the window.
f.setLocationRelativeTo(frame);
voidputOnWidestScreen(JFrame frame)
put On Widest Screen
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
int widestGD = -1;
int widestGC = -1;
double widestSize = 0;
for (int j = 0; j < gs.length; j++) {
    GraphicsDevice gd = gs[j];
    GraphicsConfiguration[] gcs = gd.getConfigurations();
...