Tuesday, May 9, 2017

Programmtically start editing a JTable cell as if you have double clicked in it

There are times after you have finished something, you want the editing caret to flash in one of your JTable cell where you would like the user to pay attention to.

This is how you can programmtically make the editing caret flash in a JTable cell as if the user has double clicked in it.

JTable table = new JTable();
table.setModel(<table model>);

//programmatically edit the cell in row 2 and column 3
table.setSelectedRow(2);
table.setSelectedCol(3);
if (table.editCellAt(2, 3)){
      table.getEditorComponent().requestFocusInWindow();
}

If you would like any cell in a JTable always in editing mode whenever it gains focus, you can modify the changeSelection method of JTable.

JTable table = new JTable() {
      public void changeSelection(int row, int col, boolean toggle, boolean extend) {
            super.changeSelection(row, col, toggle, extend);

            if (table.editCellAt(row, col)){
                  table.getEditorComponent().requestFocusInWindow();
           }
      }
};

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment