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

voidadaptSize(JDialog frame, int preferredWidth, int preferredHeight)
Set the size of the dialog to the given size, or the size of the screen if it the screen is too small.
java.awt.Toolkit toolkit = frame.getToolkit();
Dimension sSize = toolkit.getScreenSize();
if (preferredWidth > 0 && preferredHeight > 0) {
    frame.setSize(Math.min(preferredWidth, sSize.width - 30), Math.min(preferredHeight, sSize.height - 30));
} else {
    frame.pack();
frame.setLocationRelativeTo(null);
...
voidaddFileOnAction(AbstractButton button, final JTextField textField, final JDialog parent)
Add an action listener which presents a common file dialog.
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        File old = new File(textField.getText());
        chooser.setSelectedFile(old);
        FileNameExtensionFilter tiff = new FileNameExtensionFilter("TIFF (large images)", "tif", "tiff");
        FileNameExtensionFilter png = new FileNameExtensionFilter("PNG & JPG (regular images)", "png",
...
voidalignDialogInMiddleOfScreen(JDialog dlg)
align Dialog In Middle Of Screen
Rectangle frameBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
int x = frameBounds.x + ((frameBounds.width - dlg.getSize().width) / 2);
int y = frameBounds.y + ((frameBounds.height - dlg.getSize().height) / 2);
dlg.setLocation(x, y);
voidapplyDefaultProperties(final JDialog comp)
Sets default background and foreground color as well as a default font for the specified component.
if (comp == null) {
    return;
applyProperties(comp, "control", 
        "controlText", 
        null); 
PointcalculatePosition(Window parent, JDialog dialog)
calculate Position
int x = parent.getX() + (parent.getWidth() - dialog.getWidth()) / 2;
int y = parent.getY() + (parent.getHeight() - dialog.getHeight()) / 2;
Point p = new Point(x, y);
return p;
voidcascadeDialog(JDialog dialog, JDialog parent)
cascade Dialog
Point p = parent.getLocationOnScreen();
p.x += 20;
p.y += 20;
dialog.setLocation(p);
FilechooseFile(final JDialog dialog, final String title)
choose File
return chooseFile(dialog, title, null);
voidcloseDialog(final JDialog dialog)
Send a WindowEvent#WINDOW_CLOSING event to the given dialog.
final WindowEvent ev = new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING);
dialog.dispatchEvent(ev);
FilecommonFileDialog(JDialog parent, boolean save)
common File Dialog
JFileChooser chooser = new JFileChooser();
if (commonFileDialogDir != null)
    chooser.setCurrentDirectory(commonFileDialogDir);
int returnVal;
if (save)
    returnVal = chooser.showSaveDialog(parent);
else
    returnVal = chooser.showOpenDialog(parent);
...
voidconfigureCancelForDialog(final JDialog dialog, JButton cancelButton)
This method configures a standard Cancel action, bound to the ESC key, to dispose of the dialog, and sets the buttons action to be this action, and adds the action to the dialog's rootPane action map
String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY";
int noModifiers = 0;
KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, noModifiers, false);
InputMap inputMap = dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(escapeKey, CANCEL_ACTION_KEY);
Action closeAction = new AbstractAction("Cancel") {
    public void actionPerformed(ActionEvent arg0) {
        dialog.dispose();
...