I have a table to show values, in this table user can select one row, but can not change its content. My code is:
JTable tablaCurvas1 = new JTable();
TableCellRenderer tableRender = new SubtractTableRenderer();
tablaCurvas1.setDefaultRenderer(Object.class, tableRender);
tablaCurvas1.setModel(new DefaultTableModel(rowData, columnNames){
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return false; //So I make every cell non-editable
}
});
JScrollPane scrollPane = new JScrollPane(tablaCurvas1);
tablaCurvas1.setFillsViewportHeight(true);
tablaCurvas1.setPreferredScrollableViewportSize(new Dimension(200,100));
pane.add(scrollPane,c);
But now table rows do not get selected when I click over them. How could I solve this?
The ability to select a row has nothing to do with the ability to edit a cell in the given row.
The problem is your SubractTableRenderer which is responsible for doing the highlighting. You need code something like the following in the renderer:
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// add custom rendering code
if (isSelected)
setBackground( table.getSelectionBackground() );
else
setBackground( table.getBackground() );
return this;
}
JTable implements
table.setRowSelectionAllowed(boolean);
table.setColumnSelectionAllowed(boolean);
table.setCellSelectionEnabled(boolean);
for example
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class MyTable {
public MyTable() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(createTable()));
JFrame frame = new JFrame("My Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(new Dimension(400, 130));
frame.setVisible(true);
}
private JTable createTable() {
Object[][] data = {{"Nazli", "Shahi", new Date()},
{"Nima", "Sohrabi", new Date()},
{"Farsheed", "Tari", new Date()},
{"Anousheh", "Modaressi", new Date()}};
String[] columnNames = {"First Name", "Last Name", "DOB"};
DefaultTableModel 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 column) {
return false;
}
};
JTable table = new JTable(model);
table.setAutoCreateRowSorter(true);
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(true);
table.setCellSelectionEnabled(true);
return table;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyTable myTable = new MyTable();
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With