通用化几个文本字段,以便在获得焦点时选择整个文本

时间:2014-06-21 18:34:22

标签: java swing jtextfield focuslistener

我希望面板中的每个文本字段都有中心对齐,并且要有一个焦点监听器,它将在获得焦点时选择整个字段。

txtSelection = new JTextField("", 9);
txtInclusion = new JTextField("", 9);
txtExclusion = new JTextField("", 9);
...

我知道如何逐一做到这一点:

txtSelection.setHorizontalAlignment(JTextField.CENTER);

txtSelection.addFocusListener(new FocusAdapter() {

  public void focusGained(FocusEvent e) {

    txtSelection.select(0, 9);
  }
});

但是我宁愿不为所有文本字段重复该结构

我试过这个(和几个品种),但代码似乎什么都不做

pnlConditions = new JPanel();
pnlConditions.setLayout(new GridBagLayout());

for(final Component c : pnlConditions.getComponents())  // compiler insisted on 'final'
{
  ((JTextField)c).setHorizontalAlignment(JTextField.CENTER);

  if(c instanceof JTextField)
  {
    c.addFocusListener
      (new FocusAdapter()
        {
          @Override public void focusGained(FocusEvent e) 
          {
            ((JTextField)c).select(0,9);   // compiler suggested cast to JTextComponent; no difference
          }
        }
      );
  }
}

未选择整个字段;保留对齐(默认)。我想我的第一个线索是它不起作用的是编译器坚持c必须final for并建议演员应该在JTextComponent的陈述中去过select;但它没有在setHorizontal...的声明中提出这个建议。

我确信这可以做到;但如何


以下是包含上述代码段的整个方法:

  static void makePnlConditions(){
    JLabel lblSelections = new JLabel("Select ONLY combos with ALL of:");
    JLabel lblInclusions = new JLabel("DE-select combo NOT containing one or more of:");
    JLabel lblExclusions = new JLabel("DE-select combo containing ANY of:");

    txtSelection = new JTextField("", 9);
    txtInclusion = new JTextField("", 9);
    txtExclusion = new JTextField("", 9);

    pnlConditions = new JPanel();
    pnlConditions.setLayout(new GridBagLayout());

    for(final Component c : pnlConditions.getComponents())
    {
      ((JTextField)c).setHorizontalAlignment(JTextField.CENTER);

      if(c instanceof JTextField)
      {
        c.addFocusListener
          (new FocusAdapter()
            {
              @Override public void focusGained(FocusEvent e) 
              {
                ((JTextField)c).select(0,9);
              }
            }
          );
      }
    }

    pnlConditions.add(lblSelections);
    pnlConditions.add(txtSelection);
    pnlConditions.add(lblInclusions);
    pnlConditions.add(txtInclusion);
    pnlConditions.add(lblExclusions);
    pnlConditions.add(txtExclusion);
  }

1 个答案:

答案 0 :(得分:2)

首先将组件添加到JPanel中,否则pnlConditions.getComponents()将返回一个空数组,并且不会为JTextField添加任何侦听器。

如果您想选择整个文字,请使用JTextComponent#selectAll()方法。

示例代码:

JPanel pnlConditions = new JPanel();
pnlConditions.setLayout(new GridBagLayout());

pnlConditions.add(lblSelections);
pnlConditions.add(txtSelection);
pnlConditions.add(lblInclusions);
pnlConditions.add(txtInclusion);
pnlConditions.add(lblExclusions);
pnlConditions.add(txtExclusion);

for (final Component c : pnlConditions.getComponents()) {      
    if (c instanceof JTextField) {
        ((JTextField) c).setHorizontalAlignment(JTextField.CENTER);
        ...
    }
}

  

我希望面板中的每个文本字段都有中心对齐,并且要有一个焦点监听器,它将在获得焦点时选择整个字段。

创建一个扩展JTextField的自定义类,提供所有默认实现,并在应用程序的任何位置使用它以使其集中化。

示例代码:

class MyJTextField extends JTextField {

    // Initialization block that is called for all the constructors
    {
        this.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                selectAll();
            }
        });
        this.setHorizontalAlignment(JTextField.CENTER);
    }

    public MyJTextField() {}
    public MyJTextField(String text) {super(text);}
    public MyJTextField(int columns) {super(columns);}
    public MyJTextField(String text, int columns) {super(text, columns);}
}