Reputation: 10898
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.
Upvotes: 1
Views: 256
Reputation: 1513
You are using a TableModel, which implements isCellEditable by always returning true. Your desired behaviour can be achieved by writing a Table Model that always returns false. Example:
public class MyTableModel extends DefaultTableModel {
public MyTableModel(Vector data, Vector columnNames) {
setDataVector(data, columnNames);
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
}
and add this line to your code:
table.setModel(new MyTableModel(data, columnNames));
Alternative
If you wanna stick to the double array, try:
JTable table = new JTable(new AbstractTableModel() {
public String getColumnName(int column) { return columnNames[column].toString(); }
public int getRowCount() { return dataValues.length; }
public int getColumnCount() { return columnNames.length; }
public Object getValueAt(int row, int col) { return dataValues[row][col]; }
public boolean isCellEditable(int row, int column) { return false; }
});
For that to work, you will need to make your variables columnNames
and dataValues
final
.
Upvotes: 2
Reputation: 109823
answer to first of 5-6 potentials points
i cannot see any data in the table. Its loaded empty
code example (everything there is declared as local variables)
import java.awt.GridLayout;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class SimpleTableDemo extends JPanel {
private static final long serialVersionUID = 1L;
private JTable table;
private String[] columnNames = {"First Name",
"Last Name", "Sport", "# of Years", "Vegetarian", "Date"};
private Object[][] data = {
{"Kathy", "Smith", "Snowboarding", new Integer(5), (false), new Date()},
{"John", "Doe", "Rowing", new Integer(3), (true), new Date()},
{"Sue", "Black", "Knitting", new Integer(2), (false), null},
{"Jane", "White", "Speed reading", new Integer(20), (true), new Date()},
{"Joe", "Brown", "Pool", new Integer(10), (false), new Date()},};
private TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
@Override
public boolean isCellEditable(int row, int col) {
switch (col) {
case 1:
return true;
case 3:
return true;
default:
return false;
}
}
};
public SimpleTableDemo() {
super(new GridLayout(1, 0));
table = new JTable(model);
table.setRowHeight(20);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimpleTableDemo newContentPane = new SimpleTableDemo();
frame.add(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
Upvotes: 1
Reputation: 2757
Write a custom table model (you'll have to embed the data values in it and override getValueAt method to return those), then override its isCellEditable() to return false.
Upvotes: 0