JTable with JComboBox editor: is it possible to change the value of a cell from the keyboard with a single click

I have a JTable containing JComboBox editors initialized somewhat as

JComboBox comboBox = ...; TableColumn tc = table.getColumnModel().getColumn(i); tc.setCellEditor(new DefaultCellEditor(comboBox)); 

This works fine, but I would like to be able to navigate in the table and update values ​​only using the keyboard. Now it is possible with the help of combo boxes, but if I want to update the value "1", I must first press the key to activate the combo box, and then press "1" to select the item.

So, I want me to be able to press "1" and the item will be selected with just one keystroke.

For text cells, I managed to do this using prepareEditor, as shown below ...

 @Override public Component prepareEditor(TableCellEditor editor, int row, int column) { Component c = super.prepareEditor(editor, row, column); if (c instanceof JTextComponent) { ((JTextComponent) c).selectAll(); } return c; } 

... but I was not able to figure out what to do with the combo box.

One possibility could be your own TableCellEditor, but if there is a simpler solution that would be nice =)

br, Toko

+4
source share
2 answers

If anyone is still interested, I am doing a simple modification of Tuco's code, and this works for me:

 public class CustomTable extends JTable { private static final long serialVersionUID = -8855616864660280561L; public CustomTable(TableModel tableModel) { super(tableModel); } @Override public Component prepareEditor(TableCellEditor editor, int row, int column) { final Component comp = super.prepareEditor(editor, row, column); // Text component should select all text when initiated for editing. if (comp instanceof JTextComponent) ((JTextComponent) comp).selectAll(); // Try to obtain focus for the editor component. SwingUtilities.invokeLater(new Runnable() { @Override public void run() { comp.requestFocusInWindow(); } }); return comp; } } 

Basically, I just request focus for the editor component at a later time using SwingUtilities.invokeLater . The reason for this approach is that the focus request will fail if the editor component is not yet visible.

Hope this helps anyone.

+2
source

You must add KeyListener to your code.

The best solution is to add it to the JTable component, where you put the JComboBox and implement the keyPressed(KeyEvent e) or keyReleased(KeyEvent e) method to find out what the key is and perform the necessary actions.

Here I will give you an example:

 JTable table = new JTable(); // Your necessary code (create combo box, cell editor...etc) table.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); switch(keyCode) { case KeyEvent.VK_1: // manage key 1 break; case KeyEvent.VK_A: // manage key A break; case KeyEvent.VK_F1: // manage key F1 break; case KeyEvent.VK_TAB: // manage key TAB break; default: // manage other keys } } }); 

You can also combine this solution with a dictionary that associates keyCode with an action interface.

This second solution needs the following code: Global attribute (dictionary):

 Map<Integer,MyAction> keyActions = new Hashmap<Integer,MyAction>(); 

Native action interface:

 public interface MyAction { public void doAction(); } 

And the KeyListener.keyPressed () function will be as follows:

 public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); MyAction ma = keyActions.get(keyCode); if (ma != null) { ma.doAction(); } else { // do default action for other keys } } 

Hope this helps you.

Hello!

0
source

Source: https://habr.com/ru/post/1299537/


All Articles