Java Utililty Methods JDialog

List of utility methods to do JDialog

Description

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

Method

javax.swing.JDialoggetRootJDialog(java.awt.Component c)
get Root J Dialog
java.awt.Component root = javax.swing.SwingUtilities.getRoot(c);
if (root instanceof javax.swing.JDialog) {
    return (javax.swing.JDialog) root;
} else {
    return null;
voidinitialiseFrame(JDialog frame, JScrollPane viewport)
initialise Frame
frame.add(viewport);
frame.setSize(1100, 800);
frame.setLocation(new Point(200, 100));
frame.setModal(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setVisible(true);
booleanisInsideScreen(JDialog component)
Checks if component is inside the screen bounds
boolean inside;
Rectangle virtualBounds = getVirtualBounds();
inside = virtualBounds.contains(component.getBounds());
return inside;
voidjointButton(JDialog frame, final JButton button)
Allows to relate a dialog to a button
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        button.setEnabled(true);
});
voidmakeJDialogCancellable(final Window w, final Action cancelAction, final boolean disposeOnCancel)
Arrange for an existing JDialog or JFrame to close nicely when the ESC key is pressed.
JComponent c;
if (w instanceof JFrame) {
    c = (JComponent) ((JFrame) w).getRootPane();
} else if (w instanceof JDialog) {
    c = (JComponent) ((JDialog) w).getRootPane();
} else {
    throw new IllegalArgumentException("The window argument has to be either a JFrame or JDialog." + 
            "  You provided a " + (w == null ? null : w.getClass().getName())); 
...
voidmessageDialog(String string, JDialog parentDialog)
message Dialog
JOptionPane.showMessageDialog(parentDialog, string);
voidopenDialog(final JDialog dialog)
Opens Swing dialog
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        dialog.setVisible(true);
});
voidposeInsideScreen(JDialog component)
Poses component inside the screen bounds
Rectangle componentBounds = component.getBounds();
Rectangle screenBounds = getVirtualBounds();
Point newLocation;
int newLocationX;
int newLocationY;
if (!isInsideScreen(component)) {
    if (componentBounds.getX() < 0)
        newLocationX = 0;
...
voidpositionDialogInContainer(JDialog dialog, Container frame, int horizontal, int vertical)
position Dialog In Container
Dimension prefSize = dialog.getPreferredSize();
java.awt.Point parentLocation = frame.getLocationOnScreen();
Dimension parentSize = frame.getSize();
int x = parentLocation.x + (parentSize.width - prefSize.width) / 2;
int y = parentLocation.y + (parentSize.height - prefSize.height) / 2;
switch (horizontal) {
case SwingConstants.WEST:
    x = parentLocation.x;
...
voidrenderDialog(JDialog thedialog, String szMessage, int noffsetx, int noffsety)
Calls renderDialog with Help in the title window for a dialog
renderDialog(thedialog, szMessage, noffsetx, noffsety, "Help");