1

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Wesam
  • 23
  • 3
  • 1
    Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). *"How can I make it moving or pointing to the next horizontal Textfield"* What? You want focus to move to the next text field when the mouse points to it, or ..what? – Andrew Thompson Apr 11 '15 at 18:28

2 Answers2

2

If my interpretation of the question is correct you would like to customize the focus traversal. You can provide your own focus traversal policy to override default focus cycling order. Take a look at Customizing Focus Traversal topic in How to Use the Focus Subsystem tutorial. It has an example that illustrates how to install a custom policy.

On a side note, as already mentioned in comments, absolute positioning (null layout) has many drawbacks and should be considered with extra care. Null layouts should/can be avoided in most cases. As stated in Doing Without a Layout Manager (Absolute Positioning):

Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales. Layout managers also can be reused easily by other containers, as well as other programs.

Take a look at A Visual Guide to Layout Managers to get familiar with Swing layouts.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
0

As an example - using the Customizing Focus Traversal topic in How to Use the Focus Subsystem tutorial that was already mentioned 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