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

voiddecorate(JDialog dialog, int type)
Sets the look of the given dialog.
try {
    Class windowType = Class.forName("java.awt.Window$Type");
    Method setType = Window.class.getDeclaredMethod("setType", windowType);
    switch (type) {
    case POPUP:
        setType.invoke(dialog,
                Enum.valueOf((Class<Enum>) windowType.getDeclaredField("POPUP").getType(), "POPUP"));
        break;
...
JDialogdisplayInDialog(JComponent comp, JDialog result)
display In Dialog
Dimension preferredSize = comp.getPreferredSize();
result.setContentPane(comp);
result.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
result.setLocation(screenSize.width / 2 - preferredSize.width / 2,
        screenSize.height / 2 - preferredSize.height / 2);
result.setVisible(true);
return result;
...
javax.swing.JDialogdisposeOnClose(javax.swing.JDialog d)
Arranges for clearing the content pane of the dialog when it is closed.
d.setDefaultCloseOperation(javax.swing.JDialog.DISPOSE_ON_CLOSE);
d.addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent e) {
        javax.swing.JDialog d2 = (javax.swing.JDialog) e.getSource();
        d2.removeWindowListener(this);
        d2.setContentPane(new javax.swing.JPanel()); 
});
return d;
voidenableAllComponents(final boolean enable, final JDialog parent)
Enables/Disables all the components of a Frame based on the input.
if (null != parent) {
    Component[] components = parent.getRootPane().getComponents();
    if (null == components) {
        return;
    if (components.length <= 0) {
        return;
    for (Component component : components) {
        if (null != component) {
            component.setEnabled(enable);
voidenableAllComponentsExcept(final boolean enable, final JDialog parent, final Component... components)
Enables/Disables all the components of a Frame except the specified components based on the input.
if (null != parent) {
    Component[] allComponents = parent.getRootPane().getComponents();
    if (null == allComponents) {
        return;
    if (allComponents.length <= 0) {
        return;
    for (Component component : allComponents) {
        if (null != component && components != null) {
            if (contains(component, components)) {
                continue;
            component.setEnabled(enable);
voidendTask(javax.swing.JDialog jp)
end Task
javax.swing.JButton button = new javax.swing.JButton();
button.setText("Done");
button.setSize(100, 50);
button.setLocation(50, 25);
final javax.swing.JDialog dlg = jp;
dlg.add(button);
button.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
...
JDialogfindJDialog(Component c)
Locates the JDialog for the given component
if (c == null) {
    return null;
} else if (c instanceof JDialog) {
    return (JDialog) c;
} else {
    return findJDialog(c.getParent());
voidfit(JDialog d)
fit
try {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int hd = d.getY() + d.getHeight() - (dim.height - 80);
    if (hd > 0) {
        d.setSize(d.getWidth(), Math.max(d.getHeight() - hd, 150));
} catch (Throwable t) {
JDialoggetJDialog(JComponent c)
get J Dialog
Container p = c.getParent();
while (p != null && !(p instanceof JDialog)) {
    p = p.getParent();
return (JDialog) p;
ComponentgetRootJDialog(Component component)
Gets the root JDialog from the given Component Object.
while (null != component.getParent()) {
    component = component.getParent();
    if (component instanceof JDialog) {
        break;
return component;