I want to change the color of JTable
row after clicking a button. I found many examples that show how to initiate a table with different colors of rows, and also how to change the color on selecting a row. However, I wonder what is the right way to change the color on clicking a JButton.
TableCellRenderer colorRenderer = new ColorRenderer();
table.setDefaultRenderer(String.class, colorRenderer);
private class ColorRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (hasFocus) {
setBackground(Color.cyan);
} else if (isSelected) {
setBackground(table.getSelectionBackground());
} else {
setBackground(table.getBackground());
}
return this;
}
}
Considering that you are loading data in JTable
as :
public void fillTable(){
List<String> columns = new ArrayList<String>();
List<String[]> values = new ArrayList<String[]>();
columns.add("col1");
columns.add("col2");
columns.add("col3");
for (int i = 0; i < 100; i++) {
values.add(new String[] {"val"+i+" col1","val"+i+" col2","val"+i+" col3"});
}
TableModel tableModel = new DefaultTableModel(values.toArray(new Object[][] {}), columns.toArray());
tableName.setModel(tableModel);
}
Then, you can use like this class to set a rendered to JTable
:
public class EvenOddRenderer implements TableCellRenderer {
public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
DEFAULT_RENDERER.setHorizontalAlignment(JLabel.CENTER);
Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
((JLabel) renderer).setOpaque(true);
Color foreground, background;
Color alternate = new Color(0xC0, 0xC0, 0xF0);
Color lightBlue = new Color(204, 204, 255);
if (isSelected) {
foreground = Color.black;
background = Color.gray;
} else {
if (row % 2 == 0) {
foreground = Color.black;
background = Color.white;
} else {
foreground = Color.black;
background = lightBlue;
}
}
renderer.setForeground(foreground);
renderer.setBackground(background);
return renderer;
}
}
Like this :
TableCellRenderer renderer = new EvenOddRenderer();
tableName.setDefaultRenderer(Object.class, renderer);
tableName.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Then you can use setRowSelectionInterval()
method to select any row :
private void btn(java.awt.event.ActionEvent evt) {
int index = 0;
tableName.setRowSelectionInterval(index, index);
}
Here you are selecting the first row in JTable
In this example, you are choosing Color.gray
as JTable
selection color
You can change it for sure
Check out Table Row Rendering .
This approach will also work for tables that contain different data types in each column so you don't have to create custom renderers for every type of data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.