I want to paint a cell in a table with a custom renderer with a specific color i choose, i found some threads talking about it but still i think i miss something.(because it doesn't work.. )
this is not painting nothing in the table..
i tried to repaint the entire table but it seems the getTableCellRendererComponent() wont fire..how do i invoke it with a selected cells from the tables?..
Your code is not complete so I can't test it myself. Anyway...
When you want to custom render cells of your JTable, you need to provide more information about column type you are about to render (method getColumnClass() of table model does exactly that). For column types of your own, you provide custom cell renderer by implementing TableCellRenderer interface (one method to implement) and through it return component that is painted when cell is about to be rendered.
In your case, you JPanel for your CustomRenderer, so the class signature should look like this:
Depending on the object type you want to display as colored cell in your JTable, you need to tell your JTable of what class that object is. So, in your case:
Now your custom renderer will be used to display all objects of the given class.
The quieter you are, the more you are able to hear.
thanks for the reply,
I did so with JLabel, but it not seems to work, when i tell JTable i''m using JLabel, the method getTableCellRendererComponent() wont
fire, but when i tell JTable use Object.class it paints the JTable rows, also it denying me from selecting cells contrary to JLabel.class.
her's the full code, i built the GUI in NetBeans, sorry for the variable names and the code a little messy, i need it firstly to get it to work properly.
note: only the first two 'red' color buttons are working.
sorry for the variable names and the code a little messy, i need it firstly to get it to work properly.
I disagree. Firstly you need to sort out the variable and method names, for at least 2 reasons:
1. A long long time ago someone wiser than both of us told me, when I presented him with code like this, if you can't give a variable, method, class etc a concise meaningful name then you don't fully understand the problem you are trying to solve.
2. Without meaningful names it's really hard for other people (ie us, the people you are asking to help you) to read and understand the intent of your code.
In your cell renderer, if you are going to set a background colour (or any other attribute, really) you should write code which ALWAYS sets the background colour. Remember that there's only one renderer, and it's used to render all cells. So if you set its background colour to red, then every cell from then on which is drawn by the renderer will have red as its background.
because your JTable has no information of what class type is the content stored in its cells. If you are not defining your own table model, then you should override method I mentioned in my first post at block where you initialize your table model (e.g. add it after line 90 of your code):
You also need to modify your table in order to enable single cell selection, so the code you actually should have (for this) in your initializer block is:
Now, the fun part! In order to bypass the behavior as a result of one renderer for all the cells in your table, you need to store information about background color of each cell in your table. Note that this is just one approach that first came to my mind, perhaps someone else will have better solution. You can define your own class, say CellLabel as:
Now, your custom class is going to render these components that you will add in your table, with code similar to your original:
Just note that this code will throw NullPointerException unless you modify it a bit or you initialize cells with CellLabel components instead of nulls (as you did in your code).
THANK YOU ALL GUYS!!!
The JTable once in a while throws me a challenge but that's y i love programming.
her's the code WITH the proper names.
If anybody was interested i have wrote this app to make it easier to build a stage level in a brick breaker game i am developing
for android .
Joined: Oct 13, 2005
Tony Docherty wrote: . . . Without meaningful names it's really hard for other people (ie us, the people you are asking to help you) to read and understand the intent of your code.
The same applies to those long lines you get from the NetBeans GUI developer in code tags. I simply can’t read it.
Joined: Aug 07, 2007
Well done for getting it working.
A few comments on your code.
You don't need to initialise all the elements in object arrays to null.
You don't need to initialise all the elements in boolean arrays to false.
When you are writing the same line/lines of code over and over again with the only difference being an index, ask yourself "should this be in a loop".
When you are writing the same line/lines of code in different sections of the app ask yourself "should this be in a method".
All your colour setting action listeners do the same thing - this is one of the rare times when you can have a single action listener that is added to all the colour buttons and in the actionPerformed method you set 'color' to the event source's background colour.
I guess the GUI stuff has all been generated for you so you may not want to change that but I thought I'd just point out how you could condense your code and make it more understandable at the same time.
Yes, the code was generated with NetBeans IDE GUI builder.
About the loop thing i guess you are referring to
i was just a little over exited that it worked.
I also added an export button for exporting the color array to a file and by that i finished this part of my project.
Thanks for the advice with the action listener , didn't thought about it that way.