Results 1 to 11 of 11
  1. #1
    fuzzdn is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Post 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

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    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.

  3. #3
    fuzzdn is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Could you please paste your code, so that we can have a look at it.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    when I try to do it, the entire table gets coloured red not just one cell.
    Hint: an if condition can also have an else. Pseudocode:
    Java Code:
    IF some condition
      setBackground some color
    ELSE                             // add these
      setBackground normal color     // lines
    db

  6. #6
    fuzzdn is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    The if statement seems to stop any of the cells changing colour.

    Heres the code im working with:

    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++;
    			}
    		}
    And heres the table cell renderer class currently:

    Java 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;
    		}
    	}
    }
    thanks

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    Java Code:
    				if(checker.equals(temp)) {
    					tblCalendar.setDefaultRenderer(Object.class, new MyTableCellRenderer());
    					System.out.println("run");
    				}
    I believe that you should set the renderer once and leave it be. The logic should be in the renderer code.

    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.

  8. #8
    fuzzdn is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

    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++;
    		}
    thanks!

    *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

  9. #9
    fuzzdn is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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

  10. #10
    ellias2007 is offline Member
    Join Date
    Feb 2010
    Posts
    25
    Rep Power
    0

    Default 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

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    here is what im using to perform the colour change, I hope it makes sense to you all.
    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.

    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

  1. Facing the Problem When I placed my Custom ComboCheckBox in JTable cell
    By miryala.rahul@gmail.com in forum AWT / Swing
    Replies: 0
    Last Post: 06-26-2010, 03:14 PM
  2. Jtable and cell coordinates
    By rattaman in forum AWT / Swing
    Replies: 2
    Last Post: 11-17-2009, 05:48 PM
  3. Colour Specific Cell within Table
    By poddy88 in forum New To Java
    Replies: 2
    Last Post: 04-14-2009, 08:20 AM
  4. set different font for each cell in JTable
    By success21061985 in forum AWT / Swing
    Replies: 3
    Last Post: 09-10-2008, 02:06 PM
  5. Jframe In Jtable cell
    By Clarion in forum AWT / Swing
    Replies: 4
    Last Post: 06-23-2008, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •