I know changing cell backgrounds in jtable is done by creating a new cellrenderer class. I've done that. I've read about DefaultTableRenderer "color memory" issue, but I can't figure out how to work around it for my particular purpose.
My goal is simple enough: When a button is clicked, change the background color of all selected cells in the jtable.
I have the adequate method calls set up for the event, but I can't get the renderer to work the way I want it to.
I have all selected cells stored in an arraylist of TableCells (a class containing row, column, and the cell's text string data). Here is my code for getTableCellRendererComponent inside of my CustomCellRenderer.
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
for(TableCell c: selectedCells)
{
if(c.row ==row && c.col == column)
{
this.setBackground(Color.black);
}
else
{
this.setBackground(Color.BLUE);
}
}
return this;
}
This code sets the background of all table cells' backgrounds to blue. Obviously I need some different logic to work around this color memory issue. Any ideas on this would be great.
Thanks.
How about giving your renderer class a boolean variable, say btnClicked that is initialized to false but set to true in the button's ActionListener, a listener which also instructs the table to repaint itself. Then in the renderer itself, you could use the selected property to see if a cell is selected or not. Perhaps something like:
private boolean btnClicked = false;
public void setBtnClicked(boolean btnClicked) {
this.btnClicked = btnClicked;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (btnClicked) {
if (isSelected) {
setBackground(Color.black);
} else {
setBackground(Color.blue);
}
} else {
// if button not clicked
setBackground(Color.lightGray);
}
return this;
}
Also regarding:
Obviously I need some different logic to work around this color memory issue. Any ideas on this would be great.
What is this "color memory issue that you speak of?
Edit 1
Here is a compilable example of what I meant. I'm still not sure what you mean by the color memory issue though, sorry.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
@SuppressWarnings("serial")
public class DisplaySelectedTableCells extends JPanel {
public static final Integer[][] DATA = {
{1, 2, 3}, {4, 5, 6}, {7, 8, 9},
{1, 2, 3}, {4, 5, 6}, {7, 8, 9},
{1, 2, 3}, {4, 5, 6}, {7, 8, 9}
};
public static final String[] COLS = {"A", "B", "C"};
private static final int PREF_WIDTH = 400;
private static final int PREF_HEIGHT = 300;
private DefaultTableModel model = new DefaultTableModel(DATA, COLS) {
@Override
public Class<?> getColumnClass(int columnIndex) {
return Integer.class;
}
};
private JTable table = new JTable(model);
private JToggleButton toggleBtn = new JToggleButton("Show Selected Rows");
private MyCellRenderer myCellRenderer = new MyCellRenderer();
public DisplaySelectedTableCells() {
table.setDefaultRenderer(Integer.class, myCellRenderer);
JPanel btnPanel = new JPanel();
btnPanel.add(toggleBtn);
setLayout(new BorderLayout());
add(new JScrollPane(table), BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
toggleBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myCellRenderer.setShowSelected(toggleBtn.isSelected());
table.repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_WIDTH, PREF_HEIGHT);
}
private static class MyCellRenderer extends DefaultTableCellRenderer {
private static final Color SELECTED_COLOR = Color.pink;
private static final Color UNSELECTED_COLOR = Color.lightGray;
private static final Color BASE_COLOR = null;
private boolean showSelected = false;
public void setShowSelected(boolean showSelected) {
this.showSelected = showSelected;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component superComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
row, column);
if (showSelected) {
if (isSelected) {
superComponent.setBackground(SELECTED_COLOR);
} else {
superComponent.setBackground(UNSELECTED_COLOR);
}
} else {
superComponent.setBackground(BASE_COLOR);
}
return superComponent;
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("DisplaySelectedTableCells");
frame.getContentPane().add(new DisplaySelectedTableCells());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}