Example usage for javax.swing JDialog pack

List of usage examples for javax.swing JDialog pack

Introduction

In this page you can find the example usage for javax.swing JDialog pack.

Prototype

@SuppressWarnings("deprecation")
public void pack() 

Source Link

Document

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();

    JDialog dialog = new JDialog(frame, "Dialog");
    dialog.pack();
    dialog.setVisible(true);//from   w  w  w .  j av a2s .c o  m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JOptionPane pane = new JOptionPane("your message", JOptionPane.ERROR_MESSAGE, JOptionPane.OK_OPTION);
    JDialog d = pane.createDialog(null, "title");
    d.pack();
    d.setModal(false);//from   w ww  .ja  va2 s  .  c  om
    d.setVisible(true);
    while (pane.getValue() == JOptionPane.UNINITIALIZED_VALUE) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
        }
    }
    System.exit(0);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame1 = new JFrame();
    frame1.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame1.setUndecorated(true);//from  w  w  w . j a v a2s  .c o  m
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setVisible(true);

    JDialog nonModalDialog = new JDialog(frame1, "Non-Modal Dialog", ModalityType.MODELESS);
    nonModalDialog.add(Box.createRigidArea(new Dimension(200, 200)));
    nonModalDialog.pack();
    nonModalDialog.setVisible(true);

    JDialog modalDialog = new JDialog(frame1, "Modal Dialog", ModalityType.APPLICATION_MODAL);
    modalDialog.add(Box.createRigidArea(new Dimension(200, 200)));
    modalDialog.pack();
    modalDialog.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Click me to open dialog");
    button.addActionListener(e -> {//from w  w w .  ja  v a2s . c  om
        Window parentWindow = SwingUtilities.windowForComponent(button);
        JDialog dialog = new JDialog(parentWindow);
        dialog.setLocationRelativeTo(button);
        dialog.setModal(true);
        dialog.add(new JLabel("A dialog"));
        dialog.pack();
        dialog.setVisible(true);
    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JDialog f = new JDialog();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*from w  w  w . j a  v a  2 s.  co m*/
        }
    });

    JButton btOK = new JButton("Press Enter to click me, I am the default.");
    btOK.setToolTipText("Save and exit");
    f.getRootPane().setDefaultButton(btOK);

    JPanel p = new JPanel();
    p.add(btOK);
    p.add(new JButton("I am NOT the default."));
    f.getContentPane().add(p);

    f.pack();
    f.setSize(new Dimension(300, 200));

    f.setVisible(true);

}

From source file:DefaultButton.java

public static void main(String[] a) {
    JDialog f = new JDialog();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*from  ww w  . j a  v  a 2  s .  c  om*/
        }
    });

    JButton btOK = new JButton("Press Enter to click me, I am the default.");
    btOK.setToolTipText("Save and exit");
    f.getRootPane().setDefaultButton(btOK);

    JPanel p = new JPanel();
    p.add(btOK);
    p.add(new JButton("I am NOT the default."));
    f.getContentPane().add(p);

    f.pack();
    f.setSize(new Dimension(300, 200));

    f.show();

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);/*from   www . j a va2  s  .c o  m*/

    JDialog dialog = new JDialog(frame, "Dialog", true);

    JPanel mainGui = new JPanel(new BorderLayout());
    mainGui.setBorder(new EmptyBorder(20, 20, 20, 20));
    mainGui.add(new JLabel("Contents go here"), BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel(new FlowLayout());
    mainGui.add(buttonPanel, BorderLayout.SOUTH);

    JButton close = new JButton("Close");
    close.addActionListener(e -> dialog.setVisible(false));

    buttonPanel.add(close);

    frame.setVisible(true);

    dialog.setContentPane(mainGui);
    dialog.pack();
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Frame");
    frame.add(Box.createRigidArea(new Dimension(400, 300)));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();//  w  w w. java2s  .co  m
    frame.setVisible(true);

    JDialog dialog = new JDialog(frame, "Dialog", true);

    int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
    InputMap inputMap = ((JPanel) dialog.getContentPane()).getInputMap(condition);
    ActionMap actionMap = ((JPanel) dialog.getContentPane()).getActionMap();
    String enter = "enter";
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
    actionMap.put(enter, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    });

    dialog.add(Box.createRigidArea(new Dimension(200, 200)));
    dialog.pack();
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);

}

From source file:Main.java

public static void main(final String[] args) {
    JFrame frame = new JFrame("Frame");
    JDialog dialog = new JDialog(frame, "Dialog");
    frame.add(new JLabel("Content"));
    frame.addMouseListener(new MouseAdapter() {
        @Override//from  w  w  w . jav  a  2  s. c  o  m
        public void mousePressed(MouseEvent arg0) {
            System.out.println("frame pressed");
            System.out.println("dialog focused " + dialog.isFocused());
            System.out.println("frame focused " + frame.isFocused());
            super.mousePressed(arg0);
        }
    });
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    dialog.add(new JLabel("Content"));
    dialog.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent arg0) {
            super.focusLost(arg0);
            dialog.requestFocus();
        }
    });
    dialog.pack();
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JDialog dialog;
    JList jlist;/*from  w ww .  j av  a  2  s. c o  m*/
    ActionListener otherListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("current");
        }
    };
    JButton okButton = new JButton("OK");
    okButton.addActionListener(e -> close(true));
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(e -> close(false));
    jlist = new JList(new String[] { "A", "B", "C", "D", "E", "F", "G" });
    jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jlist.setVisibleRowCount(5);
    JScrollPane scroll = new JScrollPane(jlist);
    JPanel buttonsPanel = new JPanel(new FlowLayout());
    buttonsPanel.add(okButton);
    buttonsPanel.add(cancelButton);
    JPanel content = new JPanel(new BorderLayout());
    content.add(scroll, BorderLayout.CENTER);
    content.add(buttonsPanel, BorderLayout.SOUTH);
    dialog = new JDialog((Frame) null, true);
    dialog.setContentPane(content);
    dialog.pack();
    dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    dialog.getRootPane().getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "doSomething");
    dialog.getRootPane().getActionMap().put("doSomething", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    dialog.setVisible(true);
}