0

I have a panel null layout and have the following code

int k=130;
    int h=10;
    for (int i=0; i<22; ++i) {
        jTextFieldArray[i] = new JTextField();
        jTextFieldArray[i].setBounds(k, h, 120, 25);
        String s = Integer.toString(i+1);
        jTextFieldArray[i].setText(s);
        h+=30;
        panel.add(jTextFieldArray[i]);  
        if (i==10) k=430; 
        if (i==10) h=10;
    }

When I press TAB, the cursor will move to the next horizontal Textfield. How can I make it moving or pointing to the next horizontal Textfield

Best Regards, Wesam

Wesam
  • 23
  • 3
  • possible duplicate of [Press TAB to next Java in Vertical axis component](http://stackoverflow.com/questions/29581463/press-tab-to-next-java-in-vertical-axis-component) – Freek de Bruijn Apr 11 '15 at 21:43

1 Answers1

0

This question seems to be a duplicate of Press TAB to next Java in Vertical axis component.

It looks like repeating your question can be beneficial... ;-) For convenience, I have repeated my answer here as well - again using the Customizing Focus Traversal topic in How to Use the Focus Subsystem tutorial that was already mentioned (in the answer to your other question) by Aqua:

private void tryCustomFocusTraversal() {
    final JFrame frame = new JFrame("Stack Overflow: vertical tab order");
    frame.setBounds(100, 100, 800, 600);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    final JPanel panel = new JPanel(null);
    final JTextField[] jTextFieldArray = new JTextField[22];

    int k = 130;
    int h = 10;
    for (int i = 0; i < jTextFieldArray.length; ++i) {
        jTextFieldArray[i] = new JTextField();
        jTextFieldArray[i].setBounds(k, h, 120, 25);
        String s = Integer.toString(i + 1);
        jTextFieldArray[i].setText(s);
        h += 30;
        panel.add(jTextFieldArray[i]);
        if (i == 10) k = 430;
        if (i == 10) h = 10;
    }

    frame.getContentPane().add(panel);
    frame.setFocusTraversalPolicy(new CustomFocusTraversalPolicy(Arrays.asList(jTextFieldArray)));
    frame.setVisible(true);
}

Which uses the CustomFocusTraversalPolicy class:

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.util.ArrayList;
import java.util.List;

public class CustomFocusTraversalPolicy extends FocusTraversalPolicy {
    private final List<Component> componentOrder = new ArrayList<>();

    public CustomFocusTraversalPolicy(final List<Component> componentOrder) {
        this.componentOrder.addAll(componentOrder);
    }

    public Component getComponentAfter(final Container focusCycleRoot, final Component aComponent) {
        return componentOrder.get((componentOrder.indexOf(aComponent) + 1) % componentOrder.size());
    }

    public Component getComponentBefore(final Container focusCycleRoot, final Component aComponent) {
        final int currentIndex = componentOrder.indexOf(aComponent);
        return componentOrder.get(currentIndex > 0 ? currentIndex - 1 : componentOrder.size() - 1);
    }

    public Component getFirstComponent(final Container focusCycleRoot) {
        return componentOrder.get(0);
    }

    public Component getLastComponent(final Container focusCycleRoot) {
        return componentOrder.get(componentOrder.size() - 1);
    }

    public Component getDefaultComponent(final Container focusCycleRoot) {
        return getFirstComponent(focusCycleRoot);
    }
}
Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28