Is it possible to update the JTable Entire row at once , rather than column wise ??
This is my program below
Currently i could able to modify column wise only
using
model.setValueAt("1111", 0, 0)
Could i use
model.setValueAt(new Object[]{"1111","Pavan","Developer"}, 0, 0);
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class Preethi {
protected void initUI() {
final DefaultTableModel model = new DefaultTableModel(new String[] {
"Id", "Name", "Desg" }, 0);
final JTable table = new JTable(model);
// table.setFillsViewportHeight(true);
JFrame frame = new JFrame(Preethi.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollpane = new JScrollPane(table);
frame.add(scrollpane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
Vector vec = new Vector();
vec.add("1122");
vec.add("Iraneee");
vec.add("Dev");
model.insertRow(0, vec);
model.setValueAt("1111", 0, 0);
//model.setValueAt(new Object[]{"1111","Pavan","Developer"}, 0, 0);
}
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Preethi().initUI();
}
});
}
}
As the docs indicate,
setValueAtsets a single cell value.However, you could overload the method to handle an
Objectarray:Using this model you would then be able to set the values for a given row:
Slightly Shorter Version: