I have been trying for several days to code a color chooser in a JTable without success. I have tried to combine different tutorials (here, here, and here). I have also tried to use a DefaultTableModel.
I do not manage to trigger the color changes at the appropriate moment and to keep them. You will see that the icon changes when clicking on another button. I feel that I am close to a solution but I am really stuck.
If someone can find what is going wrong that would be fantastic.
thanks!
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class ButtonExample extends JFrame {
private Object[][] data = {{"toto.bw", "toto", "Button"},
{"tata.bw", "tata", "Button"},
{"titi.bw", "titi", "Button"},
{"tutu.bw", "tutu", "Button"}};
private String[] title = {"Bigwig", "Name", "Color"};
//private TableColumn columnColor;
private CustomTableModel customModel = new CustomTableModel(data,title);
private JTable bigwigsTab = new JTable(customModel);
public ButtonExample(){
this.setTitle("Button Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setExtendedState(MAXIMIZED_BOTH);
this.setMinimumSize(new Dimension(400,400));
this.setResizable(true);
JScrollPane tableContainer = new JScrollPane(bigwigsTab);
bigwigsTab.setPreferredScrollableViewportSize(new Dimension(400, 400/3));
bigwigsTab.setFillsViewportHeight(true);
bigwigsTab.setDefaultRenderer(JButton.class, new TableComponent());
bigwigsTab.getColumn("Color").setCellRenderer(new ButtonRenderer(Color.blue));
bigwigsTab.getColumn("Color").setCellEditor(new ButtonEditor(new JCheckBox()));
this.setBackground(Color.white);
this.add(tableContainer);
this.setVisible(true);
}
public class CustomTableModel extends AbstractTableModel {
private Object[][] data;
private String[] title;
public CustomTableModel(Object[][] data, String[] title) {
this.data = data;
this.title = title;
}
public int getColumnCount(){
return this.title.length;
}
public int getRowCount(){
return this.data.length;
}
public Object getValueAt(int row, int col){
return this.data[row][col];
}
public String getColumnName(int col){
return this.title[col];
}
public Class getColumnClass(int col){
return this.data[0][col].getClass();
}
public boolean isCellEditable(int row, int col){
if(col==0 || col ==3) return false; //bigwig name and color button should not be editable
return true;
}
public void setValueAt(Object value, int row, int col){
this.data[row][col] = value;
}
}
public class TableComponent extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column){
if(value instanceof JButton)
return (JButton) value;
else
return this;
}
}
public class ButtonRenderer extends JButton implements TableCellRenderer{
private Color currentColor;
public ButtonRenderer(Color c){
this.currentColor = c;
this.setSelectedColor(currentColor);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean isFocus, int row, int col){
setText((value != null) ? value.toString():"");
return this;
}
private void setSelectedColor(Color newColor) {
if (newColor == null) return;
currentColor = newColor;
this.setIcon(createIcon(currentColor, 16, 16));
this.repaint();
}
private ImageIcon createIcon(Color main, int width, int height) {
BufferedImage image = new BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(main);
graphics.fillRect(0, 0, width, height);
graphics.setXORMode(Color.DARK_GRAY);
graphics.drawRect(0, 0, width-1, height-1);
image.flush();
ImageIcon icon = new ImageIcon(image);
return icon;
}
}
public class ButtonEditor extends DefaultCellEditor{
protected JButton button;
private boolean isPushed;
private ButtonListener bListener = new ButtonListener();
private Color selectedColor = null;
public ButtonEditor(JCheckBox checkBox){
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(bListener);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column){
bListener.setRow(row);
bListener.setColumn(column);
bListener.setTable(table);
button.setText((value == null) ? "":value.toString());
return button;
}
class ButtonListener implements ActionListener{
private int column, row;
private JTable table;
private int nbre = 0;
private JButton button;
public void setColumn(int col){this.column = col;}
public void setRow(int row){this.row = row;}
public void setTable(JTable table){this.table = table;}
public JButton getButton(){return this.button;}
public void actionPerformed(ActionEvent event){
Color newColor = JColorChooser.showDialog(null, "Choose a color", Color.blue);
setSelectedColor();
((AbstractTableModel)table.getModel()).setValueAt(button, this.row, this.column);
((AbstractTableModel)table.getModel()).fireTableCellUpdated(this.row, this.column);
selectedColor = newColor;
this.button = ((JButton)event.getSource());
}
}
private void setSelectedColor() {
if (selectedColor == null) return;
button.setIcon(createIcon(selectedColor, 16, 16));
button.repaint();
}
private ImageIcon createIcon(Color main, int width, int height) {
BufferedImage image = new BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(main);
graphics.fillRect(0, 0, width, height);
graphics.setXORMode(Color.DARK_GRAY);
graphics.drawRect(0, 0, width-1, height-1);
image.flush();
ImageIcon icon = new ImageIcon(image);
return icon;
}
}
public static void main(String[] args) {
ButtonExample coreFacility = new ButtonExample();
coreFacility.setVisible(true);
}
}