Press TAB to next Java in Vertical axis component

无人久伴 提交于 2019-12-24 16:51:28

问题


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


回答1:


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.




回答2:


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);
    }
}


来源:https://stackoverflow.com/questions/29581463/press-tab-to-next-java-in-vertical-axis-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!