Niketa
Niketa

Reputation: 563

Adding rows to Jtable at run time in java

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

Answers (2)

SomeJavaGuy
SomeJavaGuy

Reputation: 7347

Solution:

addRow(new Object[]{"",""})

but Google would have been faster...

Upvotes: 1

Deepu
Deepu

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

Related Questions