简体   繁体   中英

JAVA, Netbeans: how do I assign a tab index in a jtextfield?

I searched for it and didn't find the right answer. Some say that the arrangement of the components in the inspector tab makes an automatic tab index. But it didn't work. So is there really a tab property in netbeans?

just use this piece of code and it works, using setNextFocusableComponent ()

buttonA.setNextFocusableComponent(buttonB);
buttonB.setNextFocusableComponent(buttonC);

I was going to comment on Yogendra Singh's answer, but I've only just signed up & can't. Yogendra's answer is almost perfect, but doesn't take into account what happens if someone changes focus by clicking on another component. The focusNumber will still be set to the old component, and pressing tab will take you to the wrong component.

Modifying the answer slightly fixes this issue:

List<Component> elementList = new ArrayList<Component>();
elementList.Add(element1);
elementList.Add(element2);
...
elementList.Add(elementx);
setFocusTraversalPolicy(new CustomFocusTraversalPolicy());


private class CustomFocusTraversalPolicy extends FocusTraversalPolicy {   

    public Component getComponentAfter(Container focusCycleRoot,Component aComponent)    {   
        int currentPosition = elementList.indexOf(aComponent);
        currentPosition = (currentPosition + 1) % elementList.size();   
        return (Component)elementList.get(currentPosition);   
    }   
    public Component getComponentBefore(Container focusCycleRoot,Component aComponent)    { 
        int currentPosition = elementList.indexOf(aComponent);
        currentPosition = (elementList.size() + currentPosition - 1) % elementList.size();   
        return (Component)elementList.get(currentPosition);   
    }
    public Component getFirstComponent(Container cntnr) {
        return (Component)elementList.get(0);
    }
    public Component getLastComponent(Container cntnr) {
        return (Component)elementList.get(focusList.size() - 1);
    }
    public Component getDefaultComponent(Container cntnr) {
        return (Component)elementList.get(0);
    }
}

Define Component[] elementsList to hold your elements, extend FocusTraversalPolicy and set the elements in desired order using elementsList eg

 Component[] elementsList = new Component[]{elem1,elem2,elem3};
 setFocusTraversalPolicy(new MyFocusTraversalPolicy());


 public class CustomFocusTraversalPolicy extends  FocusTraversalPolicy    {   

  public Component getComponentAfter(Container focusCycleRoot,Component aComponent)    {   
   focusNumber = (focusNumber+1) % focusList.length;   
   return (JTextField)focusList[focusNumber];   
  }   
  public Component getComponentBefore(Container focusCycleRoot,Component aComponent)    { 
   focusNumber = (focusList.length+focusNumber-1) % focusList.length;   
   return (JTextField)focusList[focusNumber];   
 }
}

The GUI editor generates code in initComponents with:

jTabbedPane1.addTab("tab1", jTextField1);

This does not seem to be customizable, using an insertTab with index. (It would have been shown in the Properties in the Layout group.)

So the best is to generate the tab component entirely in code, with, insertTab or:

jTabbedPane1.add(jTextField1, index);

The answer of Mike Plazer NZ has a small mistake I guess.

public Component getLastComponent(Container cntnr) {
    return (Component)elementList.get(focusList.size() - 1);
}

should be

public Component getLastComponent(Container cntnr) {
    return (Component)elementList.get(elementList.size() - 1);
}

the "focusList.size()" was changed to "elementList.size()"

Otherwise eclipse shows an error. But a great answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM