Below is the code i used to generate table data using JTable
public class Scroller extends JFrame {
public Scroller() throws HeadlessException {
String columnNames[] = { "Location", "Lived In People" };
// Create some data
String dataValues[][] =
{
{ "100 Hamilton", "" },
{ "", "Balan" },
{ "", "Kris" },
{ "Parkwood place", "" },
{ "", "Kris" }
};
JTable table = new JTable(dataValues, columnNames);
table.setPreferredSize(new Dimension(250, 600));
final JScrollPane scroll = new JScrollPane(table);
scroll.getVerticalScrollBar().setUnitIncrement(50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
}
public static void main(final String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Scroller().setVisible(true);
}
});
}
}
The output looks like below
Now, i can able to click on each row. But when i double click any cell, it lets me edit the
data in the cell. I found i can use table.setEnabled(false)
to make this not to happen.
But i am trying to make the table so that, each row can be selected but the cells should not be editable. is there any straight forward method to achieve this?
NOTE : I tried to override the function isCellEditable
as specified in this post. But when running the screen shows only empty table.