Ok, ho risolto con una soluzione trovata su un forum inglese.
Usando un JOptionPane.showConfirmDialog, posso dare il focus all'area di testo facendole chiamare un'estensione del metodo .addAncestorListener
Per riferimenti futuri, metto a disposizione il codice del JOptionPane (qui) e quello della classe RequestFocusListener.java (in basso)
codice:
JPasswordField pf = new JPasswordField();
pf.addAncestorListener( new RequestFocusListener() ); // METTO IL FOCUS
Object[] obj = {"Enter your Password:", pf};
JOptionPane.showConfirmDialog(null, obj, "Enter Password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,null"));
codice:
import javax.swing.*;
import javax.swing.event.*;
public class RequestFocusListener implements AncestorListener
{
private boolean removeListener;
/*
* Convenience constructor. The listener is only used once and then it is
* removed from the component.
*/
public RequestFocusListener()
{
this(true);
}
/*
* Constructor that controls whether this listen can be used once or
* multiple times.
*
* @param removeListener when true this listener is only invoked once
* otherwise it can be invoked multiple times.
*/
public RequestFocusListener(boolean removeListener)
{
this.removeListener = removeListener;
}
@Override
public void ancestorAdded(AncestorEvent e)
{
JComponent component = e.getComponent();
component.requestFocusInWindow();
if (removeListener)
component.removeAncestorListener( this );
}
@Override
public void ancestorMoved(AncestorEvent e) {}
@Override
public void ancestorRemoved(AncestorEvent e) {}
}