asabo
asabo

Reputation:

how to control focus in JTable

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?

Upvotes: 3

Views: 27167

Answers (2)

Markus Lausberg
Markus Lausberg

Reputation: 12267

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.

Write your own FocusTraversalPolicy to set it to the Table

table.setFocusTraversalPolicy(policy)

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

FocusManager.getCurrentManager().focusNextComponent();

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

Upvotes: 0

Peter Lang
Peter Lang

Reputation: 55624

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.

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

Upvotes: 5

Related Questions