Hi,
How do I add a certain colour to the background of an individual cell?
Thanks
Printable View
Hi,
How do I add a certain colour to the background of an individual cell?
Thanks
To change the color of a selected cell you would want to call either setSelectionForeground(Color selectionForeground) or setSelectionBackground(Color selectionBackground). setGridColor(Color gridColor) is also pretty fun to use. :p
thanks, but how do I do this?
Code:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class G{
public static void main(String[] args){
JFrame jf = new JFrame("Inventory");
Container c = jf.getContentPane();
Object[] colnames = {"Title", "Author", "Isbn", "Price"};
Object[][] data = {
{"Weaveworld", "CliveBarker", "125457", new Double(9.56)},
{"Watership Down", "Richard Adams", "898454", new Double(8.43)},
{"The Guardian", "John Saul", "87987", new Double(10.98)},
};
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
JTable jt = new JTable(data, colnames);
jt.setGridColor(Color.RED);
jt.setSelectionBackground(Color.yellow);
JScrollPane scrollableTable = new JScrollPane(jt);
c.add(scrollableTable);
jf.setSize(500, 100);
jf.setVisible(true);
}
}
I have tried this, and all this has acheived is setting the whole row to yellow, when a cell in that row is selected.
I want to set a single cell colour to a set colour (ie yellow)
example:
if cell.value = 45 then make that cell yellow
Thanks