I want to change the color of a row in my table and i also need to be able to change the color of that row back to the standard white color. I am invoking setRowColor() on my CustomTableCellRenderer-object and its working fine. however, after i have done a color-change on a row it is not possible to highlight that row anymore by dragging the mouse over the cells which is a problem. What am i missing? Thanks!
edit: yes, sorry about that, im new here. anyway, here's the code for the CustomTableCellRenderer and a class with main() to run. Ive added some buttons to do the color changing and as you see the rows that have been exposed to color-change can no longer be highlighted if you drag your mouse over the cells.
is not working for some reason. Thank you for your help!if(isSelected) { cell.setBackground(table.getSelectionBackground()) ; }
CusomtTableCellRenderer.java :
import java.awt.Color; import java.awt.Component; import java.util.HashMap; import java.util.Map; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; class CustomTableCellRenderer extends DefaultTableCellRenderer { private Map<Integer, Color> mapColors; public CustomTableCellRenderer() { mapColors = new HashMap<Integer, Color>(); } public void setRowColor(int row, Color color) { mapColors.put(row, color); } @Override public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) { Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, 1); Color color = mapColors.get(row); if (color != null ) { cell.setBackground(color); } else { if(isSelected) { cell.setBackground(table.getSelectionBackground()); } else { cell.setBackground(table.getBackground()); } } return cell; } }
RunThis.java :
import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; public class RunThis extends JFrame implements ActionListener{ JTable table; JPanel panel; JScrollPane pane; CustomTableCellRenderer cell; JButton setRed, setWhite; public RunThis() { table = new JTable(50,2); pane = new JScrollPane(table); panel = new JPanel(); panel.add(pane); add(panel, BorderLayout.CENTER); setRed = new JButton("Set red color"); setRed.addActionListener(this); setRed.setActionCommand("red"); add(setRed,BorderLayout.SOUTH); setWhite = new JButton("Set white color"); setWhite.addActionListener(this); setWhite.setActionCommand("white"); add(setWhite,BorderLayout.NORTH); cell = new CustomTableCellRenderer(); for(int i=0; i<table.getColumnCount(); i++) { table.getColumnModel().getColumn(i).setCellRenderer(cell); } pack(); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { RunThis obj = new RunThis(); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand() == "red") { for(int i=0; i<table.getRowCount(); i++) { if(table.isRowSelected(i)) { cell.setRowColor(i, Color.RED); } } } if(e.getActionCommand() == "white") { for(int i=0; i<table.getRowCount(); i++) { if(table.isRowSelected(i)) { cell.setRowColor(i, Color.WHITE); } } } } }