Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Colouring Rows of a table

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Colouring Rows of a table

    Hi all.
    I have an events tables and Im currently struggling to colour the rows in which the event starts and stops.
    I have a customer renderer but dont what vaules/tests i need to set the colour of the rows.
    I would like it to colour all rows between the start and the stop time. My SSCCE is below

    package testcode;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableCellRenderer;
     
    public class TableTest {
     
      public static void main(String args[]) {
          /*  Populated the table with times and events */
        Object rows[][] = { { "12:00", "" }, { "13:00", "StartTime" }, { "14:00", "" }, { "15:00", "" }, { "16:00", "" },
            { "17:00", "" }, { "18:00", "" }, { "19:00", "StopTime" }, { "20:00", "" }, { "21:00", "" }, { "22:00", "" } };
        Object headers[] = { "Time", "Event" };
        JFrame frame = new JFrame("Renderer");
        JTable table = new JTable(rows, headers);
        TableCellRenderer renderer = new MyRenderer();
        table.setDefaultRenderer(Object.class, renderer);
        JScrollPane scrollPane = new JScrollPane(table);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    }
     
    class MyRenderer implements TableCellRenderer {
     
      public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
     
      public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
        Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        ((JLabel) renderer).setOpaque(true);
        Color background = null;
     
        /**  Colors row 1 blue
        if (row == 1){
            background = Color.BLUE;
        }else{
            background = Color.WHITE;
        }*/
     
        // Here i need to get the start time and stop time and fill the rows with a different colour.
     
        renderer.setBackground(background);
        return renderer;
      }
    }

    Any ideas would be helpful.
    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Colouring Rows of a table

    What about this are you stuck on? The parameter value in the getTableCellRendererComponent() method gives you the value in the cell. Check that against the start time, setting the color appropriately. You might also have to call setOpaque(true) on the JLabel returned from the DefaultTableCellRenderer- but why don't you just extend that instead and call super to get the default renderer?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Colouring Rows of a table

    The parameter value in the getTableCellRendererComponent() method gives you the value in the cell. Check that against the start time, setting the color appropriately. You might also have to call setOpaque(true) on the JLabel returned from the DefaultTableCellRenderer
    Thanks. i now have any row which is not empty coloured using the value variable
       if (!(value.equals(""))  && (column == 1)){
            background = Color.BLUE;
        }

    how can i colour the rows in between? i tried setting a boolean but then got stuck in an infinate loop as i dont know when to set it to false.

    but why don't you just extend that instead and call super to get the default renderer?
    Im confused on how to do this. where do i call super from and how do i set the values or the rows to colour.

    Thanks for your help
    Kurt

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Colouring Rows of a table

    There are a ton of ways to do it. The first thing that pops out to me is to simply do a test- if StartTime is in a cell above the current cell, and StopTime is in the cell below the current cell, then set the background to whatever you want.

    Another way is to have a value in that cell, something like "BetweenTime". Check against that, and if the value of the cell being rendered is "BetweenTime", then set the text to nothing and set the background.

    And what I meant with extending DefaultTableCellRenderer was something like this:
    class MyRenderer extends DefaultTableCellRenderer {
      public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
        Component renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
       //actually, strangely enough, renderer can be exchanged with the keyword "this" after the super method has been called.
        ((JLabel) renderer).setOpaque(true);
       //...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Colouring Rows of a table

    Firstly, Thanks for your help

    There are a ton of ways to do it. The first thing that pops out to me is to simply do a test- if StartTime is in a cell above the current cell, and StopTime is in the cell below the current cell, then set the background to whatever you want.
    Because the getTableCellRendererComponent method is in the MyRenderer class, and the Start time and stop time are in the main class, Im confused on how i can do a check against the start time and stop time (these time can be different and there may also be more than one event per table).

    Another way is to have a value in that cell, something like "BetweenTime". Check against that, and if the value of the cell being rendered is "BetweenTime", then set the text to nothing and set the background.
    I tried this way and it works fine but i then cannot set the cell value from "BetweenTime" To ""?

    and i tried calling the method via super (like you did) but i got errors. (cannot find symbol)
    Also is there an advantage of using super instead of setting a default_renderer like i did?

    Thanks for your time
    Kurt

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Colouring Rows of a table

    Quote Originally Posted by kurt-hardy View Post
    Because the getTableCellRendererComponent method is in the MyRenderer class, and the Start time and stop time are in the main class, Im confused on how i can do a check against the start time and stop time (these time can be different and there may also be more than one event per table).
    Can't you get the values directly from the table? You have access to it via the table parameter. Or, since you're writing a custom renderer, you could just pass the main class (or better yet, the start and stop times) in as parameters to the constructor of the renderer.



    Quote Originally Posted by kurt-hardy View Post
    I tried this way and it works fine but i then cannot set the cell value from "BetweenTime" To ""?
    I'm not sure what you mean. Why can't you? I'd suggest you post an SSCCE that demonstrates the problem.

    Quote Originally Posted by kurt-hardy View Post
    and i tried calling the method via super (like you did) but i got errors. (cannot find symbol)
    Again, that's not really specific enough for me to be able to help. What is the actual error? What is the actual code you're trying to use? Again, an SSCCE would be nice.

    Quote Originally Posted by kurt-hardy View Post
    Also is there an advantage of using super instead of setting a default_renderer like i did?
    The advantage would be that you'd only have one instance of a renderer instead of the two that you currently have. There's no need to have two like you do, since you're only really using the methods of one of them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to add rows dynamically to in a JSPpage
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: January 21st, 2011, 09:04 AM
  2. How can i print rows 1 by 1 using System.out.println?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2010, 07:35 AM
  3. Building Table of Specified Rows and Columns
    By wale89 in forum Java Servlet
    Replies: 1
    Last Post: August 3rd, 2010, 08:57 AM
  4. Need a loop for rows and columns
    By Ceasar in forum Loops & Control Statements
    Replies: 8
    Last Post: October 9th, 2009, 05:47 PM
  5. adding rows to coloumns. Netbeans
    By haygaurav in forum Java IDEs
    Replies: 1
    Last Post: April 1st, 2009, 03:16 PM