Results 1 to 4 of 4
Thread: Color cell in JTable
- 03-24-2009, 02:45 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
Color cell in JTable
Hi,
I'm new of Java graphics and I've some problems with a JTable.
I need to change the color of cells(not of the text in the cell) only for the first 5 cells in my JTable. This color should change in a jToggleActionPerform method.
I know that a CustomTableCellRenderer is necessary but how can I use it in the method?
Is there anyone who could help me?
Thanks a lot
- 03-24-2009, 05:16 PM #2
You may be able to achieve this without a custom renderer. Set a boolean field to the state of the JToggleButton in its actionPerformed, and repaint() the table. Then setBackground of the cell in a prepareRenderer override based on the boolean field value and the row, column parameters.
Here's a prepareRenderer override that colors table cells on the diagonal yellow:dbJava Code:JTable table = new JTable(data, headers) { @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { JLabel label = (JLabel) super.prepareRenderer(renderer, row, column); if (row == column) { label.setBackground(Color.YELLOW); } else { label.setBackground(Color.WHITE); } return label; } }
- 03-25-2009, 10:53 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
Thanks a lot for your help.
I've not understood very well the question about the boolean field in jToggle button. Considering I work with NetBeans IDE and I don't create directly the jTable, how could I do for solving the question?
Thanks again
- 03-25-2009, 11:53 AM #4
Learn how to code a GUI and dump the visual designer until you do that.
Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)
Or limit yourself to what you can do with the visual designer.
It's your choice.
Similar Threads
-
Set focus on particular cell in a JTable when using TableModel
By sridhar_negi in forum AWT / SwingReplies: 0Last Post: 11-14-2008, 04:14 AM -
set different font for each cell in JTable
By success21061985 in forum AWT / SwingReplies: 3Last Post: 09-10-2008, 02:06 PM -
jTable Cell editing
By mahaling_m in forum AWT / SwingReplies: 0Last Post: 06-23-2008, 08:17 AM -
Jframe In Jtable cell
By Clarion in forum AWT / SwingReplies: 4Last Post: 06-23-2008, 04:42 AM -
Limiting the capacity of a cell of JTable
By rameshraj in forum Advanced JavaReplies: 0Last Post: 03-24-2008, 02:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks