Reputation: 563
Table has to be created first with an empty rows and then rows should added at run time in java using JTable.
How to add rows at runtime?
Upvotes: 0
Views: 4985
Reputation: 7347
Solution:
addRow(new Object[]{"",""})
but Google would have been faster...
Upvotes: 1
Reputation: 2616
You can use addRow( ... ) method -
DefaultTableModel model;
JTable table;
JScrollPane sc1;
model = new DefaultTableModel();
table = new JTable(model);
model.addColumn("RollNo");
model.addColumn("Name");
model.addRow(new Object[]{"",""});
table.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
model.addRow(new Object[]{"", ""}); } }
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { } } );
sc1 = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
But you should implement it in your own way.
Upvotes: 0