I am developing a Java application that uses a JTable. I want to allow the user to enter data in a JTable that can later be printed or saved. The issues is: Suppose the user has entered some data and the cursor is on the last cell of the row in a table (I have uploaded an image-suppose the cursor is on the highlighted cell).How do I make it in such a way that when a user presses the Enter Button(Keyboard button) the application will add/append a new row that is empty to the table so that the user can fill in other data. Attachment
Asked
Active
Viewed 2,236 times
0
-
Possible duplicate of [How to add row dynamically in JTable](http://stackoverflow.com/questions/22371720/how-to-add-row-dynamically-in-jtable) – Arun Xavier Jan 09 '17 at 06:23
2 Answers
0
Try this...
JTable table = new JTable();
DefaultTableModel tblModel = new DefautlTableModel(0,0);
table.setModel(tblModel);
//an action or click then you should run this code
tblModel.addRow(new Object[]{""});

Murat Yağan
- 1
- 1
-
Thanks very much for your help. I did what you suggested and it worked perfect. – Sharma Hussein Jan 09 '17 at 07:19
-
I have another problem. How can i get row index in JTable from KeyEvent . – Sharma Hussein Jan 09 '17 at 08:05
-
public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_CTRL){// which key is pressed. for example control key JTable.getSelectedRow(); } } – Murat Yağan Jan 09 '17 at 10:03
0
DefaultTableModel tbm = (DefaultTableModel) jTable1.getModel();
Vector rowData = null;
tbm.addRow(rowData);

Arpit Porwal
- 273
- 3
- 14