Is it possible to clear text in JTable with type?

Can I make my textField in JTable act like a cell in Excel ? Clear text as you type, but you can edit it when you enter a cell.

I think that these 2 operations will go into the same event. I'm wrong?

I am trying to use keyPressed but nothing works. TT-TT

Here is my code

 private JTable getTblMaster() { if (tblMasterData == null) { tblMasterData = new JTable() { private static final long serialVersionUID = 1L; public TableCellEditor getCellEditor(int row, int column) { TableColumn tableColumn = getColumnModel() .getColumn(column); TableCellEditor editor = tableColumn.getCellEditor(); try { if (editor == null) { final JTextField text = new JTextField(); /* text.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(KeyEvent e){ } }); SwingUtilities.invokeLater(new Runnable(){ public void run(){ } }); */ editor = new DefaultCellEditor(text); ; return editor; } } catch (Exception e) { LogWriter.error(e); } return editor; } }; } return tblMasterData; } 

Any suggestion?

+4
source share
3 answers

best select all text in JTable cell

 text.setText(text.getText()) text.selectAll 

by porting to invokeLater()

great workaround Table Select all Editor @camickr

+4
source

OU! trashgod. Your example saved my life: D

All I need is the code below and it worked. How easy! Thank you very much.

 private JTable getTblMaster() { if (tblMasterData == null) { tblMasterData = new JTable() { public boolean editCellAt(int row, int column, EventObject e){ boolean result = super.editCellAt(row, column, e); final Component editor = getEditorComponent(); if (editor == null || !(editor instanceof JTextComponent)) { return result; } if (e instanceof KeyEvent) { ((JTextComponent) editor).selectAll(); } return result; } .... 
+3
source

I did it like that. First I use the keyReleased event, and then I get the row and column number I'm working on, and then set the value in that row. The code is as follows.

 private void purchases_TBLKeyReleased(java.awt.event.KeyEvent evt) { int rowWorking = purchases_TBL.getSelectedRow(); int columnWorking = purchases_TBL.getSelectedColumn(); if(columnWorking==3){ model.setValueAt(null, rowWorking, columnWorking); } } 

This makes the third column of the table null as soon as I focus on using the keyboard.

Note The same code fragment can be placed in the MouseClicked event.

0
source

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


All Articles