Results 1 to 11 of 11
Thread: Jtable colour cell problem
- 04-03-2011, 12:12 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Jtable colour cell problem
I have a jtable that is representing a calendar and a list of dates that have appointments on. What i want to do is colour the days on the calendar that have appointments on.
The calendar is in a month view using the gregorian calendar
Each time i switch month the calendar is redrawn, so this is where i will need to put the cell colour change code in surely. I tried reading up on tablecellrenderers but couldn't get it to work how i wanted to, and now ive become confused by it all.
Any help would be appreciated :)
Thanks
- 04-03-2011, 04:53 PM #2
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
Did you get a chance to look at this article:
How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
I think this mentiones what you want to do.
- 04-03-2011, 05:07 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
I did have a look through yes, unfortunately I couldn't apply it correctly to what I want to do, it seems that when I try to do it, the entire table gets coloured red not just one cell. But thanks for the response
Here's whats happening in my code so anyone can have some background on my problem:
There is an arraylist that stores appointments where i can get the date.
I am looping through this arraylist.
A nested loop then goes through each day in the current month being viewed and compares it to the arraylist item. If they are the same, then there is an appointment on that day.
This is the line where that cell needs to change foreground colour.
Thanks
- 04-03-2011, 05:10 PM #4
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
Could you please paste your code, so that we can have a look at it.
- 04-03-2011, 05:24 PM #5
Hint: an if condition can also have an else. Pseudocode:when I try to do it, the entire table gets coloured red not just one cell.dbJava Code:IF some condition setBackground some color ELSE // add these setBackground normal color // lines
- 04-03-2011, 05:31 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
The if statement seems to stop any of the cells changing colour.
Heres the code im working with:
And heres the table cell renderer class currently:Java Code:public static String temp = ""; public static String checker = ""; public String gettemp(){ return this.temp; } public String getchecker(){ return this.checker; } for(int i = 0; i < p.getappointment().size(); i++) { int j = 1; temp = ((p.getAppointment(i).getStartDate().get(Calendar.YEAR))+"/"+(p.getAppointment(i).getStartDate().get(Calendar.MONTH))+"/"+(p.getAppointment(i).getStartDate().get(Calendar.DATE))); while(j <= numofdays) { checker = String.valueOf(year); checker += "/"; checker += month; checker += "/"; checker += j; if(checker.equals(temp)) { tblCalendar.setDefaultRenderer(Object.class, new MyTableCellRenderer()); System.out.println("run"); } j++; } }
thanksJava Code:import javax.swing.table.*; import javax.swing.*; import java.awt.*; public class MyTableCellRenderer extends DefaultTableCellRenderer{ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { organiserGUI g = new organiserGUI(); Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if(g.gettemp().equals(g.getchecker())) { System.out.println(g.gettemp() + " " + g.getchecker()); c.setForeground(Color.RED); return c; } else { c.setForeground(Color.BLACK); return c; } } }
-
I'm guessing that organiserGUI is your main class, and that you have more than one instance of this class occurring, one that is displayed and has all the information you want, and one that is being created in your renderer that is completely distinct from the displayed instance. I'm guessing that your problem is one of using an improper reference to this object, that you may solve your problem if you use the displayed instance rather than the freshly created instance in your renderer. To get the displayed instance, pass it into the renderer through the renderer's constructor.
edit: this looks iffy:
I believe that you should set the renderer once and leave it be. The logic should be in the renderer code.Java Code:if(checker.equals(temp)) { tblCalendar.setDefaultRenderer(Object.class, new MyTableCellRenderer()); System.out.println("run"); }
edit 2: in fact your whole code looks slightly odd. I strongly urge you to create and post an SSCCE, a small compilable program with no outside dependencies (i.e., no need for images or database), that we can compile, run, modify and hopefully correct.Last edited by Fubarable; 04-03-2011 at 06:35 PM.
- 04-05-2011, 12:52 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Ive made great progress on this, to the point where it works for what I described before. One date in the month turns red and it is the one i wanted. Hurray!
Ive stumbled upon another issue now though...
I now have a list with the coordinate values in the table that need to be turned red. This is perfect.
I have tried using this code, however... it seems to only turn the first set of coordinates red, i.e it only works for one day on the calendar.
here is what im using to perform the colour change, I hope it makes sense to you all.
thanks!Java Code://Colour change bit int a = 0; while(a < datestoturnred.size()) { specialcol = datestoturnred.pop(); specialrow = datestoturnred.pop(); if(row == specialrow && column == specialcol) { c.setForeground(Color.RED); } else { c.setForeground(null); } a++; }
*edit, im having to return Component c, which is the cell we are changing the colour of. Because of this only one cell is being returned as changed.
I hope that makes sense
Is there anything i can do?
Thanks!Last edited by fuzzdn; 04-05-2011 at 05:41 PM. Reason: more info
- 04-06-2011, 08:47 PM #9
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Been scouring page after page on the internet. All i can find is ways to turn one cell a different colour.
There must be a way, surely
Thanks
- 07-30-2011, 03:58 PM #10
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
Yes, But ....
Yes, your code is clear, but what i did not understand that the method :
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
returns Component, and i expect it to be of type VOID,
that means it does the job without returning any thing ... the expected senario is that i call a method with some arguments somewhere in the code :
void TheMethod (TheJTable, TheRow, TheColor) and i call it in the process where i need without returning any thing,
the point that is not clear is How and where to use it in the code ??
Mant thanks
- 07-30-2011, 05:30 PM #11
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
It doesn't make sense to me. Given that you are attempting to use a loop it tells me that you don't understand the concept of a renderer.here is what im using to perform the colour change, I hope it makes sense to you all.
The table determines when a cell needs to be painted and invokes the renderer. So in your renderering code you display the value in the cell. In your case you display the appointment for the cell. So you code should be easy. If the cell has an appointment you set the background one color. If it doesn't have an appointment you use the default color.
You need to create create a SSCCE to understand the basics of renderering. So create a table using the DefaultTableModel. Add a few values to the model using the table.setValueAt(...) method. Maybe add values like "Yes" and "No". Then create a custom renderer that changes the foreground color depending on the value found in the table model.
Once you get this working you apply the knowledge to your real program.
Similar Threads
-
Facing the Problem When I placed my Custom ComboCheckBox in JTable cell
By miryala.rahul@gmail.com in forum AWT / SwingReplies: 0Last Post: 06-26-2010, 03:14 PM -
Jtable and cell coordinates
By rattaman in forum AWT / SwingReplies: 2Last Post: 11-17-2009, 05:48 PM -
Colour Specific Cell within Table
By poddy88 in forum New To JavaReplies: 2Last Post: 04-14-2009, 08:20 AM -
set different font for each cell in JTable
By success21061985 in forum AWT / SwingReplies: 3Last Post: 09-10-2008, 02:06 PM -
Jframe In Jtable cell
By Clarion in forum AWT / SwingReplies: 4Last Post: 06-23-2008, 04:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks