Password Fields and Option Dialogs

Site Admin · 11 Sep 2026 · 2 views

JPasswordField

Hides typed characters (default echo is a dot):

JPasswordField pass = new JPasswordField(12);
char[] pwd = pass.getPassword();   // char[] not String
String entered = new String(pwd);

Swing JPasswordField demo

JOptionPane Dialogs

Standard message, confirm and input dialogs in one call:

JOptionPane.showMessageDialog(this, "Saved successfully!");
int r = JOptionPane.showConfirmDialog(this, "Delete this row?",
        "Confirm", JOptionPane.YES_NO_OPTION);
String n = JOptionPane.showInputDialog(this, "Enter name:");

Swing JOptionPane demo

JColorChooser

Pick colors with a complete chooser dialog:

Color c = JColorChooser.showDialog(this, "Pick a color", Color.RED);
if (c != null) panel.setBackground(c);

Swing JColorChooser demo

Key Points

  • getPassword() returns char[] so the password is not kept as an immutable String.
  • JOptionPane covers alerts, confirms and prompts without custom dialog classes.
  • JColorChooser is a ready-made color picker.
Share this post:

Comments (0)

Please login or register to comment.