Trong bài viết này mình sử dụng lớp RequestFocusListener kế thừa từ lớp AncestorListener của Java để thiết lập Focus đầu tiên cho một JTextFiled thuộc JOptionPane. |
Để sử dụng bạn chỉ việc add thuộc tính AncestorListener là lớp RequestFocusListener cho JTextField bạn muốn set Focus như ví dụng bên dưới.
JPanel panel = new JPanel();
JPasswordField pass = new JPasswordField();
pass.addAncestorListener(new RequestFocusListener(false));
int option = JOptionPane.showOptionDialog(this, panel, "Passowrd Unzip Setting", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
JPasswordField pass = new JPasswordField();
pass.addAncestorListener(new RequestFocusListener(false));
int option = JOptionPane.showOptionDialog(this, panel, "Passowrd Unzip Setting", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
RequestFocusListener.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package citi_support_tools;
import javax.swing.JComponent;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
/**
*
* @author bnson
*/
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) {
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package citi_support_tools;
import javax.swing.JComponent;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
/**
*
* @author bnson
*/
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) {
}
}
No comments:
Post a Comment