CodeHouse of Horrors

Archive for the ‘Uncategorized’ Category

Swing – hey, that dialog looks neat, can I do it with a Password field instead?

leave a comment »

No, it’s not as straightforward as one would hope, but after some searching around for different solutions, I settled on this one:

String user = JOptionPane.showInputDialog(null,
“Username:”,
“Enter Username”,
JOptionPane.PLAIN_MESSAGE);

 

Equivalent JPassword Dialog:

JPanel panel = new JPanel(new GridLayout(2, 1));
JLabel passLabel = new JLabel(“Password:”);
JPasswordField pass = new JPasswordField(20);
pass.addAncestorListener(new AncestorListener() {
public void ancestorAdded(AncestorEvent e) {
JComponent component = (JComponent) e.getComponent();
component.requestFocusInWindow();
component.removeAncestorListener(this);
}

public void ancestorMoved(AncestorEvent e) { }

public void ancestorRemoved(AncestorEvent e) { }
});
panel.add(passLabel);
panel.add(pass);
int action = JOptionPane.showConfirmDialog(null, panel, “Enter Password”, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (action == 0) {
passwd = new String(pass.getPassword());
}

Written by trumpetx

November 20, 2012 at 9:07 pm

Posted in Uncategorized

Eclipse Settings – Mockito/JUnit annoyances with importing static classes

leave a comment »

  1. Open Preferences (Window-> Preferences)
  2. Browse to Java -> Code Style -> Organize Imports
  3. Change ‘Number of static imports needed for .* to ‘1’
  4. Click Ok

OMG I can’t believe I had never found this before.  No more assertTrue & assertFalse annoyances with silly static classes.

 

Written by trumpetx

April 8, 2011 at 1:03 pm

Posted in Uncategorized