how to detect enter pressed while jtable cell is being edited?

allegroBegin

I have a keylistener on jtable so that when someone presses enter some calculations happen. However, this only happens if the person is not editing. I would like to apply this action when a person finishes editing a cell and is pressing enter to finish and close the editing.

I cannot figure this out, anyone ever did this or know how to?

Basically, now for the action to be done, people must press enter twice, one to end the editing and another for the action that I want to happen, I would like to make it needed only once, while editing.

Thank you

tenorsax

You can override JTable.editingStopped, which is invoked when editing is finished and apply your actions in that method.

EDIT:

JTable.editingStopped was not designed for application extension. To avoid complications, in particular platform dependent ones, a better approach is to override model's setValueAt or register a TableModelListener. Here is an example:

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

public class DemoTable3 {
    private static void createAndShowUI() {
        JFrame frame = new JFrame("DemoTable");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Object[][] rows = { { "Column 1", "Column 2" },
                { "Column 1", "Column 2" } };
        Object[] columns = { "Column 1", "Column 2" };

        DefaultTableModel model = new DefaultTableModel(rows, columns);
        model.addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                System.out.println("apply additional action");
            }
        });

        JTable table = new JTable(model);
        frame.add(new JScrollPane(table));
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

Another alternative is to add CellEditorListener to catch editingStopped events. For example:

import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;

public class DemoTable2 {

    private static void createAndShowUI() {
        JFrame frame = new JFrame("DemoTable");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Object[][] rows = { { "Column 1", "Column 2" },
                { "Column 1", "Column 2" } };
        Object[] columns = { "Column 1", "Column 2" };

        final JTable table = new JTable(new DefaultTableModel(rows, columns));

        table.getDefaultEditor(String.class).addCellEditorListener(
                new CellEditorListener() {
                    public void editingCanceled(ChangeEvent e) {
                        System.out.println("editingCanceled");
                    }

                    public void editingStopped(ChangeEvent e) {
                        System.out.println("editingStopped: apply additional action");
                    }
                });

        frame.add(new JScrollPane(table));
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

Also look at a Table Cell Listener by @camickr which offers custom processing of the edits.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check if a particular cell in JTable is being edited?

From Dev

How to check if an Excel document cell is being edited

From Dev

How to capture ESC and Enter keys being pressed?

From Dev

How to detect if Enter was pressed in txtMessage_TextChanged() ?

From Dev

How to update edited JTable cell's records into Database

From Dev

How to know whether QTableWidget's cell is (not) being edited?

From Dev

How to manually modify a cell that is being edited in a worksheet_change event?

From Dev

Save edited cell/s from JTable to database

From Dev

Update mysql when cell value is edited in JTable

From Dev

How to detect in touchesEnded which nodes are still being pressed

From Dev

QTableView: dataChanged event clears cell being edited

From Dev

How to detect mouse movement over node while button is pressed?

From Dev

How to check if Being edited on CMSDESK

From Dev

How to detect if a PNG was edited with PhotoShop?

From Dev

How to detect if a PNG was edited with PhotoShop?

From Dev

bashscript to detect right arrow key being pressed

From Dev

How do I format UITextField text in real time, while it's being edited?

From Dev

JTable Cell Isn't Updated On Enter Click

From Dev

How to Detect When the Enter Key was Pressed using F sharp (F#)

From Dev

Stopping a particular ngGrid cell (not column) from being edited during ngGridEventStartCellEdit

From Dev

Stop JTable being editable only when focussed and key pressed

From Dev

Change view *while* another is being pressed

From Dev

How to detect if a specific key was pressed?

From Dev

How to detect key pressed in TypeScript?

From Dev

How to detect if a key was pressed in Java?

From Dev

How to detect key pressed in TypeScript?

From Dev

How to check if any item is being edited in the QTreeView or not?

From Dev

How to prevent a directory from being edited

From Dev

How to break a loop when "enter" is pressed in C++ while taking input in an integer array

Related Related

  1. 1

    How to check if a particular cell in JTable is being edited?

  2. 2

    How to check if an Excel document cell is being edited

  3. 3

    How to capture ESC and Enter keys being pressed?

  4. 4

    How to detect if Enter was pressed in txtMessage_TextChanged() ?

  5. 5

    How to update edited JTable cell's records into Database

  6. 6

    How to know whether QTableWidget's cell is (not) being edited?

  7. 7

    How to manually modify a cell that is being edited in a worksheet_change event?

  8. 8

    Save edited cell/s from JTable to database

  9. 9

    Update mysql when cell value is edited in JTable

  10. 10

    How to detect in touchesEnded which nodes are still being pressed

  11. 11

    QTableView: dataChanged event clears cell being edited

  12. 12

    How to detect mouse movement over node while button is pressed?

  13. 13

    How to check if Being edited on CMSDESK

  14. 14

    How to detect if a PNG was edited with PhotoShop?

  15. 15

    How to detect if a PNG was edited with PhotoShop?

  16. 16

    bashscript to detect right arrow key being pressed

  17. 17

    How do I format UITextField text in real time, while it's being edited?

  18. 18

    JTable Cell Isn't Updated On Enter Click

  19. 19

    How to Detect When the Enter Key was Pressed using F sharp (F#)

  20. 20

    Stopping a particular ngGrid cell (not column) from being edited during ngGridEventStartCellEdit

  21. 21

    Stop JTable being editable only when focussed and key pressed

  22. 22

    Change view *while* another is being pressed

  23. 23

    How to detect if a specific key was pressed?

  24. 24

    How to detect key pressed in TypeScript?

  25. 25

    How to detect if a key was pressed in Java?

  26. 26

    How to detect key pressed in TypeScript?

  27. 27

    How to check if any item is being edited in the QTreeView or not?

  28. 28

    How to prevent a directory from being edited

  29. 29

    How to break a loop when "enter" is pressed in C++ while taking input in an integer array

HotTag

Archive