How to control focus in JTable

What I want to do is when the user finishes editing the data in the table cell to move the focus to another cell depending on the user entered and turn this cell into edit mode so that the user immediately starts typing without any additional actions. Thus, the user can focus on his work, and the software will make a "reflection" on which cell should be edited further.

A simple task that does not look so simple in real life ... any idea?

+3
source share
2 answers

Try this example.

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

Hope this gives you an idea of ​​how to do this.

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);
    }
}
+5
source

You must add KeyListener to JTable to get all typed characters. After the user clicks Enter, you should check the word that the user typed.

Write your own FocusTraversalPolicyto set it to a table

table.setFocusTraversalPolicy(policy)

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

FocusManager.getCurrentManager().focusNextComponent();

EDIT: I have not tested this, it is just an idea.

0
source

All Articles