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

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

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

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

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

JOptionPane – check user input and prevent from closing until conditions are met

You can create your own Custom JDialog to check user input etc before closing or moving on. See this link: Stopping Automatic Dialog Closing By default, when the user clicks a JOptionPane-created button, the dialog closes. But what if you want to check the user’s answer before closing the dialog? In this case, you must … Read more

Multiple input in JOptionPane.showInputDialog

Yes. You know that you can put any Object into the Object parameter of most JOptionPane.showXXX methods, and often that Object happens to be a JPanel. In your situation, perhaps you could use a JPanel that has several JTextFields in it: import javax.swing.*; public class JOptionPaneMultiInput { public static void main(String[] args) { JTextField xField … Read more

Why JScrollPane in JOptionPane not showing all its content?

As shown in this related example, you can override the viewport’s preferred size. import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.GroupLayout; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.SwingConstants; /** * @see https://stackoverflow.com/a/14858272/230513 * @see https://stackoverflow.com/a/8504753/230513 * @see https://stackoverflow.com/a/14011536/230513 */ public class DynamicGroupLayout { private static final int NUM = 30; private JTextField[] … Read more

Text wrap in JOptionPane?

A JOptionPane will use a JLabel to display text by default. A label will format HTML. Set the maximum width in CSS. JOptionPane.showMessageDialog( this, “<html><body><p style=”width: 200px;”>”+exp.getMessage()+”</p></body></html>”, “Error”, JOptionPane.ERROR_MESSAGE); More generally, see How to Use HTML in Swing Components, as well as this simple example of using HTML in JLabel.