0

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?

2 Answers 2

1

Solution:

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

but Google would have been faster...

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.