Button to change bg-color in JTable cell

Hello!

I would like to change the background color of a cell with a klick on a button.

There will be 3 colors, and the selected cell are the only one thats going to change.

How do I do that?

Heres my code:

import javax.swing.*;

import java.awt.Color;

import java.awt.Component;

import java.awt.Rectangle;

import javax.swing.JButton;

import javax.swing.table.TableCellRenderer;

import javax.swing.table.TableColumn;

import javax.swing.table.TableColumnModel;

import javax.swing.table.TableModel;

publicclass Kinnaextends JFrame{

privatestaticfinallong serialVersionUID = 1;

private JPanel jContentPane =null;

private JTabbedPane jTabbedPane =null;

private JScrollPane jScrollPane =null;

private JTable jTable =null;

private JButton jButtonRed =null;

private JButton jButtonGreen =null;

private JButton jButtonBlue =null;

private JButton jButtonNewWeek =null;

private JButton jButtonDelWeek =null;

/**

* This method initializes jTabbedPane

*

* @return javax.swing.JTabbedPane

*/

private JTabbedPane getJTabbedPane(){

if (jTabbedPane ==null){

jTabbedPane =new JTabbedPane();

jTabbedPane.setBounds(new Rectangle(105, 45, 545, 488));

jTabbedPane.addTab(null, null, getJScrollPane(),null);

}

return jTabbedPane;

}

/**

* This method initializes jScrollPane

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getJScrollPane(){

if (jScrollPane ==null){

jScrollPane =new JScrollPane();

jScrollPane.setViewportView(getJTable());

}

return jScrollPane;

}

/**

* This method initializes jTable

*

* @return javax.swing.JTable

*/

private JTable getJTable(){

if (jTable ==null){

Object[] head ={"Namn","M錸dag","Tisdag","Onsdag","Torsdag","Fredag"};

Object[][] data ={{"Niklas","9-15","9-15","9-15","9-15","9-15"},

{"Niklas","9-15","9-15","9-15","9-15","9-15"}};

jTable =new JTable(data, head);

// This table shades every other column yellow

}

return jTable;

}

/**

* This method initializes jButtonRed

*

* @return javax.swing.JButton

*/

private JButton getJButtonRed(){

if (jButtonRed ==null){

jButtonRed =new JButton("R鰀");

jButtonRed.setBounds(new Rectangle(15, 30, 76, 31));

jButtonRed.setText("R鰀");

jButtonRed.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent e){

}

});

}

return jButtonRed;

}

/**

* This method initializes jButtonGreen

*

* @return javax.swing.JButton

*/

private JButton getJButtonGreen(){

if (jButtonGreen ==null){

jButtonGreen =new JButton("R鰀");

jButtonGreen.setBounds(new Rectangle(15, 75, 76, 31));

jButtonGreen.setText("Gr鰊");

}

return jButtonGreen;

}

/**

* This method initializes jButtonBlue

*

* @return javax.swing.JButton

*/

private JButton getJButtonBlue(){

if (jButtonBlue ==null){

jButtonBlue =new JButton("R鰀");

jButtonBlue.setBounds(new Rectangle(15, 120, 76, 31));

jButtonBlue.setText("Bl?);

}

return jButtonBlue;

}

/**

* This method initializes jButtonNewWeek

*

* @return javax.swing.JButton

*/

private JButton getJButtonNewWeek(){

if (jButtonNewWeek ==null){

jButtonNewWeek =new JButton();

jButtonNewWeek.setBounds(new Rectangle(270, 0, 91, 31));

jButtonNewWeek.setText("Ny Vecka");

jButtonNewWeek.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent e){

System.out.println("actionPerformed()");

int i = getJTabbedPane().getSelectedIndex() + 1;

String tab ="Index: " + i;

getJTabbedPane().addTab(tab,new JLabel("Hello"));

getJTabbedPane().setSelectedIndex(getJTabbedPane().getTabCount()-1);

}

});

}

return jButtonNewWeek;

}

/**

* This method initializes jButtonDelWeek

*

* @return javax.swing.JButton

*/

private JButton getJButtonDelWeek(){

if (jButtonDelWeek ==null){

jButtonDelWeek =new JButton();

jButtonDelWeek.setBounds(new Rectangle(375, 0, 121, 31));

jButtonDelWeek.setText("Ta bort vecka");

jButtonDelWeek.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent e){

System.out.println("actionPerformed()");

getJTabbedPane().removeTabAt(getJTabbedPane().getSelectedIndex());

}

});

}

return jButtonDelWeek;

}

/**

* @param args

*/

publicstaticvoid main(String[] args){

SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

Kinna thisClass =new Kinna();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}

/**

* This is the default constructor

*/

public Kinna(){

super();

initialize();

}

/**

* This method initializes this

*

* @return void

*/

privatevoid initialize(){

this.setSize(670, 580);

this.setContentPane(getJContentPane());

this.setTitle("JFrame");

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getJContentPane(){

if (jContentPane ==null){

jContentPane =new JPanel();

jContentPane.setLayout(null);

jContentPane.add(getJTabbedPane(),null);

jContentPane.add(getJButtonRed(),null);

jContentPane.add(getJButtonGreen(),null);

jContentPane.add(getJButtonBlue(),null);

jContentPane.add(getJButtonNewWeek(),null);

jContentPane.add(getJButtonDelWeek(),null);

}

return jContentPane;

}

}// @jve:decl-index=0:visual-constraint="29,25"

[13167 byte] By [huba] at [2007-11-27 2:45:32]
# 1
Then you need to store the Color information for each cell somehow. Maybe this posting will help: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=626145
camickra at 2007-7-12 3:13:11 > top of Java-index,Desktop,Core GUI APIs...
# 2

Ok.

Lets say that i use a hidden column in the jtable for the selected cell, then i would need 5 hidden columns.

Object[][] data = {{"Niklas", "9-15", "white", "9-15", "white","9-15", "white","9-15","white","9-15", "white"},

{"Niklas", "9-15", "white", "9-15", "white","9-15", "white","9-15","white","9-15", "white"}};

Then I only need to set the value to the color field, and the cellrenderer would paint the cell correct.

2 questions, how do I hide the color columns, and how do I write my one cell renderer?

huba at 2007-7-12 3:13:11 > top of Java-index,Desktop,Core GUI APIs...
# 3

If you have a different color for every cell in the table, then maybe it would be easier to store a custom Object in the TableModel that has two pieces of information:

a) the text to be displayed

b) the background color of the text.

Then you can write a custom renderer that uses both pieces of information to renderer the cell correctly.

Here is an untested example of what the custom renderer might look like:

class ColorRenderer extends DefaultTableCellRenderer

{

public Component getTableCellRendererComponent(

JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

{

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

CustomObject custom = (CustomObject)value;

setText( custom.getText() );

if (!isSelected)

setBackground( custom.getBackground() );

return this;

}

}

camickra at 2007-7-12 3:13:11 > top of Java-index,Desktop,Core GUI APIs...