JOptionPane to get password

Yes, it is possible using JOptionPane.showOptionDialog(). Something like this: JPanel panel = new JPanel(); JLabel label = new JLabel(“Enter a password:”); JPasswordField pass = new JPasswordField(10); panel.add(label); panel.add(pass); String[] options = new String[]{“OK”, “Cancel”}; int option = JOptionPane.showOptionDialog(null, panel, “The title”, JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]); if(option == 0) // pressing OK button { char[] … Read more

Customize JOptionPane Dialog

You can simply add your components to a JPanel and then add this JPanel to your JOptionPane, as shown in this small example : import java.awt.*; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.swing.*; import javax.imageio.ImageIO; public class JOptionPaneExample { private void displayGUI() { JOptionPane.showConfirmDialog(null, getPanel(), “JOptionPane Example : “, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); } private JPanel … Read more

JOptionPane Passing Custom Buttons

In the example I linked to you previous question, the buttons use the JOptionPane#setValue method to set the return value. This allows you to continue using the API as normal, while providing you with the customisation your after. final JButton okay = new JButton(“Ok”); okay.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane pane … Read more

Why does setSelected on JCheckBox lose effect?

It is a known bug as acknowledged by Oracle Bug ID:6924233 The JOptionPane apparently causes another event to be generated with a check box value = false. The recommended fix is to instantiate the JOptionPane using invokeLater. Submitted On 09-MAR-2010 The change is in the BasicButtonListener – Method focusLost() In 1.6.0_18 it is … ButtonModel … Read more

Closing a runnable JOptionPane

Yes, the trick would be to get the Timer started before you call setVisible… public class AutoClose02 { public static void main(String[] args) { new AutoClose02(); } private Timer timer; private JLabel label; private JFrame frame; public AutoClose02() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException … Read more

Disable ok button on JOptionPane.dialog until user gives an input

JOptionPane allows you to supply a component as the message pane and the controls/options that can be displayed on it. If you add the correct listeners to the message component, then you should be able to influence the controls that are used as options. Take a look at JOptionPane.showOptionDialog(Component parentComponent, Object message, String title, int … Read more

Closing A JOptionPane Programmatically

Technically, you can loop through all windows of the application, check is they are of type JDialog and have a child of type JOptionPane, and dispose the dialog if so: Action showOptionPane = new AbstractAction(“show me pane!”) { @Override public void actionPerformed(ActionEvent e) { createCloseTimer(3).start(); JOptionPane.showMessageDialog((Component) e.getSource(), “nothing to do!”); } private Timer createCloseTimer(int seconds) … Read more