开发者

set focus for all fields

开发者 https://www.devze.com 2023-02-10 05:29 出处:网络
I noticed I can use getName() as part of the trick. What is java.awt.Component.getName() and setName() used for?

I noticed I can use getName() as part of the trick. What is java.awt.Component.getName() and setName() used for? But I don't really have a clue where to start. What type of listener should I use (assuming the textfield / or box is currently blinking / selected)


This is my previous question, and thank you for the help guys. How do I use requestFocus in a Java JFrame GUI?

I reali开发者_运维百科ze that for each component (Textfield) that I am creating, I have to insert a statement like requestFocus (or using transferFocus). Is it possible to apply this policy to all the fields???

I have several textfields and ComboBox. The problem I hit is that I don't want to write methods for every single field / box.

For example, I write a method like this

private JTextField getFirstNameEntry() {
.... do something
}

because my instructor writes his program like this

private JPanel getJContentPane() {
        jContentPane = new JPanel();
        jContentPane.setLayout(new java.awt.FlowLayout(FlowLayout.LEADING));


        jContentPane.add(makeLabel(" First Name *", 100, 20));
        jContentPane.add(getFirstNameEntry(), null);

        jContentPane.add(makeLabel(" Middle Initial", 100, 20));
        jContentPane.add(getMiddleInitialEntry(), null);
// etc

        return jContentPane;

However, to save redundancy (that was my motive at first), say I have a box, I can simply add the following code inside the method above: getJContentPane()

titleBox = new JComboBox(new String[]{"Mr.","Mrs.","Ms.","Dr.","Prof.","Rev."});
jContentPane.add(titleBox);

But doing this, I still need to create a method to do addItemListener

  private void setComboBoxFocus() {
      titleBox.addItemListener(
            new ItemListener(){
                public void itemStateChanged(ItemEvent e){
                    if(e.getStateChange() == ItemEvent.SELECTED)
                    {
                        String titleSelected = titleBox.getSelectedItem().toString();
                        System.out.println(titleSelected);
                        titleBox.transferFocus();
                    }
                }
            });

      }

However, this doesn't really save redundancy at all. If I have more than one ComboBox to be added, I would have to write another similar method. In fact, even in the case with one ComboBox (titleBox), I would still end up with writing a method for titleBox.

So my question is: is there a way to write a general method that can call focus to all (maybe one for ComboBox type)?

Thank you and sorry for the long post.


Why not take a JComboBox argument to your setComboBoxFocus() method, which allows you to set that listener to any JComboBox you may have? Like so:

private void setComboBoxFocus(JComboBox box) {
    box.addItemListener(
          new ItemListener(){
              public void itemStateChanged(ItemEvent e){
                  if(e.getStateChange() == ItemEvent.SELECTED)
                  {
                      String titleSelected = box.getSelectedItem().toString();
                      System.out.println(titleSelected);
                      box.transferFocus();
                  }
              }
          });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号