Java 如何在 JTable 中控制焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/423524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:26:10  来源:igfitidea点击:

how to control focus in JTable

javauser-interfaceswingfocusjtable

提问by

What I want to do is when user finish editing of data in table cell to move focus onto another cell depending of what user entered, and to turn that cell into editing mode so user can start typing immediately with no additional action. This way user can focus on his work and software will do the 'thinking' about which cell should be edited next.

我想要做的是,当用户完成表格单元格中数据的编辑时,根据用户输入的内容将焦点移到另一个单元格上,并将该单元格转换为编辑模式,以便用户可以立即开始输入而无需额外操作。这样用户可以专注于他的工作,软件将“思考”接下来应该编辑哪个单元格。

Simple task which does not look so simple in real life ... anyone some idea?

在现实生活中看起来并不那么简单的简单任务......有人有什么想法吗?

采纳答案by Peter Lang

Please try this example.

请试试这个例子。

It should let you navigate through the table by entering the values u, d, l, r for Up, Down, Left, Right.

它应该让您通过输入值 u、d、l、r 来浏览表格,分别代表 Up、Down、Left、Right。

Hope that this will give you an idea about how to do it.

希望这会给你一个关于如何做的想法。

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class Test extends JFrame {

    private JTable table;
    private TableModel tableModel;

    public Test() {
        tableModel = new DefaultTableModel(5, 5);

        table = new JTable(tableModel);
        table.setColumnSelectionAllowed(true);

        getContentPane().add(table);

        Action handleEnter = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                table.getCellEditor().stopCellEditing(); // store user input
                int row = table.getSelectedRow();
                int col = table.getSelectedColumn();
                String val = String.valueOf(table.getValueAt(row, col)).toLowerCase();
                if (val.equals("u"))
                    --row;
                else if (val.equals("d"))
                    ++row;
                else if (val.equals("l"))
                    --col;
                else if (val.equals("r"))
                    ++col;
                if (     row >= 0 && row < tableModel.getRowCount()
                      && col >= 0 && col < tableModel.getColumnCount()) {
                    table.changeSelection(row, col, false, false);
                    table.editCellAt(row, col);
                }
            }
        };
        // replace action for ENTER, since next row would be selected automatically
        table.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "handleEnter");
        table.getActionMap().put("handleEnter", handleEnter);
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.setSize(800, 600);
        test.setVisible(true);
    }
}

回答by Markus Lausberg

You should add a KeyListener to the JTable to get all typed Characters. After the user presses Enter, you should check the word the user has typed.

您应该向 JTable 添加一个 KeyListener 以获取所有键入的字符。用户按下 后Enter,您应该检查用户输入的单词。

Write your own FocusTraversalPolicyto set it to the Table

编写自己的FocusTraversalPolicy将其设置为表

table.setFocusTraversalPolicy(policy)

The FocusTraversalPolicydescribes which component gets the next focus. After this you can call

FocusTraversalPolicy描述了组件获得下一个焦点。在此之后你可以打电话

FocusManager.getCurrentManager().focusNextComponent();

EDIT: I did not test this, it is just an idea.

编辑:我没有测试这个,这只是一个想法。