为了便于快速了解,请参阅给定的快照
我有一个Jdialog SampleTestDialog ,其中定义了一些组件
以及setTraversalOrder()
方法中定义的焦点遍历顺序。此对话框还使用在 SeparatePanel 类中定义的单独面板,其中包含一些组件,并且还定义了在定义为名为setTraversalOrder()
的方法中的组件定义的init的焦点顺序。类。
问题:
按TAB键时,焦点遍历SampleTestDialog中定义的所有组件 它不会进入SeparatePanel组件
我为SeparatePanel设置了this.setFocusTraversalPolicy(focusPolicy);
this.setFocusTraversalPolicyProvider(true);
。
以下是使用过的两个容器的SSCCE。
SampleTestDialog.java
public class SampleTestDialog extends JDialog {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JPanel jPanel = null;
private JButton jButton = null;
private JButton jButton1 = null;
private SeparatePanel seperatePanel = null;
private CustomTraversalPolicyHandler focusPolicy = null;
public SampleTestDialog(Frame owner) {
super(owner);
initialize();
setFocusTraversalPolicy(focusPolicy);
}
private void initialize() {
this.setSize(392, 320);
this.setContentPane(getJContentPane());
setFocusTraversalOrder();
}
private void setFocusTraversalOrder() {
Vector<Component> order = new Vector<Component>();
order.add(getJButton1());
order.add(getJButton());
order.add(getJTextField1());
order.add(getJTextField());
focusPolicy = new CustomTraversalPolicyHandler(order);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
gridBagConstraints31.gridx = 0;
gridBagConstraints31.gridwidth = 2;
gridBagConstraints31.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints31.anchor = GridBagConstraints.WEST;
gridBagConstraints31.insets = new Insets(10, 10, 5, 10);
gridBagConstraints31.gridy = 3;
GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
gridBagConstraints21.gridx = 0;
gridBagConstraints21.anchor = GridBagConstraints.WEST;
gridBagConstraints21.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints21.gridwidth = 2;
gridBagConstraints21.gridy = 2;
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
gridBagConstraints3.gridy = 1;
gridBagConstraints3.weightx = 1.0;
gridBagConstraints3.anchor = GridBagConstraints.WEST;
gridBagConstraints3.insets = new Insets(5, 10, 0, 0);
gridBagConstraints3.gridx = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.fill = GridBagConstraints.NONE;
gridBagConstraints2.gridy = 0;
gridBagConstraints2.weightx = 1.0;
gridBagConstraints2.anchor = GridBagConstraints.WEST;
gridBagConstraints2.insets = new Insets(5, 10, 0, 0);
gridBagConstraints2.gridx = 1;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 1;
jLabel1 = new JLabel();
jLabel1.setText("Text 2");
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
jLabel = new JLabel();
jLabel.setText("Text 1:");
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.add(jLabel, gridBagConstraints);
jContentPane.add(jLabel1, gridBagConstraints1);
jContentPane.add(getJTextField(), gridBagConstraints2);
jContentPane.add(getJTextField1(), gridBagConstraints3);
jContentPane.add(getJPanel(), gridBagConstraints21);
jContentPane.add(getSeperatePanel(), gridBagConstraints31);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setColumns(10);
}
return jTextField;
}
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setColumns(10);
}
return jTextField1;
}
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.insets = new Insets(10, 10, 10, 10);
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.gridx = 1;
gridBagConstraints4.insets = new Insets(10, 10, 10, 10);
gridBagConstraints4.gridy = 0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(getJButton(), gridBagConstraints5);
jPanel.add(getJButton1(), gridBagConstraints4);
}
return jPanel;
}
public SeparatePanel getSeperatePanel() {
if(seperatePanel == null)
{
seperatePanel = new SeparatePanel();
seperatePanel.setBorder(BorderFactory.createTitledBorder(null, "SeparatePanel", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
seperatePanel.setFocusCycleRoot(true);
seperatePanel.setFocusTraversalPolicyProvider(true);
}
return seperatePanel;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Button 1");
}
return jButton;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("Button 2");
}
return jButton1;
}
public static void main(String[] args) {
SampleTestDialog dialog = new SampleTestDialog(null);
dialog.setVisible(true);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
SeparatePannel.java
public class SeparatePanel extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel jLabel = null;
private JTextField nameTextField = null;
private JLabel jLabel1 = null;
private JTextField addressTextField = null;
private JLabel jLabel2 = null;
private JTextField contactTextField2 = null;
private JPanel jPanel = null;
private JButton sendButton = null;
private JButton cancleButton = null;
private CustomTraversalPolicyHandler focusPolicy = null;
public SeparatePanel() {
super();
initialize();
this.setFocusTraversalPolicy(focusPolicy);
this.setFocusTraversalPolicyProvider(true);
}
private void initialize() {
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.gridx = 1;
gridBagConstraints11.fill = GridBagConstraints.BOTH;
gridBagConstraints11.gridy = 3;
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
gridBagConstraints5.gridy = 2;
gridBagConstraints5.weightx = 1.0;
gridBagConstraints5.anchor = GridBagConstraints.WEST;
gridBagConstraints5.insets = new Insets(5, 10, 0, 0);
gridBagConstraints5.weighty = 0.0;
gridBagConstraints5.gridx = 1;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.gridx = 0;
gridBagConstraints4.insets = new Insets(0, 5, 0, 0);
gridBagConstraints4.gridy = 2;
jLabel2 = new JLabel();
jLabel2.setText("Contact #:");
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
gridBagConstraints3.gridy = 1;
gridBagConstraints3.weightx = 1.0;
gridBagConstraints3.anchor = GridBagConstraints.WEST;
gridBagConstraints3.insets = new Insets(5, 10, 0, 0);
gridBagConstraints3.gridx = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 0;
gridBagConstraints2.anchor = GridBagConstraints.EAST;
gridBagConstraints2.gridy = 1;
jLabel1 = new JLabel();
jLabel1.setText("Address:");
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.insets = new Insets(5, 10, 0, 0);
gridBagConstraints1.gridx = 1;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.anchor = GridBagConstraints.EAST;
gridBagConstraints.gridy = 0;
jLabel = new JLabel();
jLabel.setText("Name:");
this.setSize(300, 200);
this.setLayout(new GridBagLayout());
this.add(jLabel, gridBagConstraints);
this.add(getNameTextField(), gridBagConstraints1);
this.add(jLabel1, gridBagConstraints2);
this.add(getAddressTextField(), gridBagConstraints3);
this.add(jLabel2, gridBagConstraints4);
this.add(getContactTextField(), gridBagConstraints5);
this.add(getJPanel(), gridBagConstraints11);
setFocusTraversalOrder();
}
private void setFocusTraversalOrder() {
Vector<Component> order = new Vector<Component>();
order.add(getCancleButton());
order.add(getSendButton());
order.add(getContactTextField());
order.add(getAddressTextField());
order.add(getNameTextField());
focusPolicy = new CustomTraversalPolicyHandler(order);
}
private JTextField getNameTextField() {
if (nameTextField == null) {
nameTextField = new JTextField();
nameTextField.setColumns(10);
}
return nameTextField;
}
private JTextField getAddressTextField() {
if (addressTextField == null) {
addressTextField = new JTextField();
addressTextField.setColumns(10);
}
return addressTextField;
}
private JTextField getContactTextField() {
if (contactTextField2 == null) {
contactTextField2 = new JTextField();
contactTextField2.setColumns(10);
}
return contactTextField2;
}
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
gridBagConstraints8.gridx = 0;
gridBagConstraints8.gridy = 0;
GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
gridBagConstraints7.insets = new Insets(10, 10, 10, 10);
GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
gridBagConstraints6.gridx = 1;
gridBagConstraints6.insets = new Insets(10, 10, 10, 10);
gridBagConstraints6.gridy = 0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(getSendButton(), gridBagConstraints7);
jPanel.add(getCancleButton(), gridBagConstraints6);
jPanel.add(getSendButton(), gridBagConstraints8);
}
return jPanel;
}
private JButton getSendButton() {
if (sendButton == null) {
sendButton = new JButton();
sendButton.setText("Send");
}
return sendButton;
}
private JButton getCancleButton() {
if (cancleButton == null) {
cancleButton = new JButton();
cancleButton.setText("Cancel");
}
return cancleButton;
}
}
要完成自定义焦点遍历,会使用 CustomTraversalPolicyHandler 类扩展 FocusTraversalPolicy
public class CustomTraversalPolicyHandler extends FocusTraversalPolicy {
Vector<Component> order;
public CustomTraversalPolicyHandler(Vector<Component> order) {
this.order = order;
}
@Override
public Component getComponentAfter(Container focusCycleRoot,
Component aComponent) {
Component nextComponent;
int idx = (order.indexOf(aComponent) + 1) % order.size();
nextComponent = order.get(idx);
if (!nextComponent.isEnabled()
|| !nextComponent.isVisible()
|| !nextComponent.getParent().isVisible()
|| !nextComponent.getParent().isEnabled()){
nextComponent = getComponentAfter(focusCycleRoot, nextComponent);
}
return nextComponent;
}
@Override
public Component getComponentBefore(Container focusCycleRoot,
Component aComponent) {
Component previousComponent;
int idx = order.indexOf(aComponent) - 1;
if (idx < 0) {
idx = order.size() - 1;
}
previousComponent = order.get(idx);
if (!previousComponent.isEnabled()
|| !previousComponent.isVisible()
|| !previousComponent.getParent().isVisible()
|| !previousComponent.getParent().isEnabled()){
previousComponent = getComponentBefore(focusCycleRoot,
previousComponent);
}
return previousComponent;
}
@Override
public Component getFirstComponent(Container focusCycleRoot) {
return order.firstElement();
}
@Override
public Component getLastComponent(Container aContainer) {
return order.lastElement();
}
@Override
public Component getDefaultComponent(Container aContainer) {
return order.firstElement();
}
}