Results 1 to 10 of 10
  1. #1
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default Backgorund of a cell in a table

    Hello,

    I want to set different colors for different columns in the table. I use JLabel for setting the background color. Here is the code

    private Color colorGreen = new Color( 44, 222, 74);
    private Color colorYellow = new Color(255, 255, 0);
    ..
    ...
    ...

    if(column == 0)
    setBorder(new EmptyBorder(1, 1, 1, 0));

    else if(column == 1) {

    setBackground(colorGreen);

    if(percents == null) {
    return new JLabel("");
    }
    DecimalFormat f = new DecimalFormat("##.00");

    String sectionText = "N:" + f.format(percents.getGreenPercent());

    return new JLabel(sectionText);

    }

    else if(column == 2) {
    setBackground(colorYellow);
    if(percents == null) {
    return new JLabel("");
    }
    DecimalFormat f = new DecimalFormat("##.00");

    String sectionText = " M:" + f.format(percents.getYellowPercent());
    return new JLabel(sectionText);
    }

    But the color of the columns stays white, doesn't change.
    Can anybody help me please where I go wrong?

    Thanks

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Backgorund of a cell in a table

    Hi,

    Assuming you're using a JTable, you shouldn't need to use JPanel to add colour to the column background. Instead you:
    1. get the TableColumnModel for each of the columns
    2. get the TableColumn from TableColumnModel
    3. create a custom TableCellRenderer, where the getTableCellRendererComponent method sets the background colour of each of the cells in the column
    4. set the custom TableCellRenderer to the TableColumn

    The following is an example:

    Java Code:
    		String column[] = { "RED", "GREEN", "BLUE" };
    		String emptyRow[] = { "", "", "" };
    		String data[][] = { { "one", "two", "three" }, emptyRow, emptyRow, emptyRow, emptyRow };
    
    		JTable table = new JTable(data, column);
    
    		TableColumn redCol = table.getColumnModel().getColumn(0);
    		redCol.setCellRenderer(new ColourColumnRenderer(Color.RED));
    
    		TableColumn greenCol = table.getColumnModel().getColumn(1);
    		greenCol.setCellRenderer(new ColourColumnRenderer(Color.GREEN));
    
    		TableColumn blueCol = table.getColumnModel().getColumn(2);
    		blueCol.setCellRenderer(new ColourColumnRenderer(Color.BLUE));
    An example custom TableCellRenderer:

    Java Code:
    	class ColourColumnRenderer extends DefaultTableCellRenderer {
    
    		private Color bgColour;
    
    		public ColourColumnRenderer(Color bgColour) {
    			super();
    			this.bgColour = bgColour;
    		}
    
    		@Override
    		public Component getTableCellRendererComponent(JTable table, Object value,
    			boolean isSelected, boolean hasFocus, int row, int column) {
    			Component cell = super.getTableCellRendererComponent(table, value, isSelected,
    				hasFocus, row, column);
    
    			cell.setBackground(bgColour);
    
    			return cell;
    		}
    	}

  3. #3
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default Re: Backgorund of a cell in a table

    Hi,

    Thank you very much, but I have already created getTableCellRendererComponent method, here is the code:

    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column) {

    Section section = (Section)value;
    ColorsPercent percents = sectionsResultsProvider.getSectionColorPercents(se ction); //03.03

    // Background and Foreground

    setBackground(colorNormal2);

    if((row % 2) == 0) {
    setBackground(colorNormal);
    }

    setForeground(colorFont);

    // Border
    if((hasFocus) || (isSelected)) {
    if(column == 0)
    setBorder( new MatteBorder(1, 1, 1, 0, colorBorder) );
    // else if (column == 1){
    // DecimalFormat f = new DecimalFormat("##.00");
    // String sectionText = "N:" + f.format(percents.getGreenPercent());
    // }
    // else if (column == 2){
    // DecimalFormat f = new DecimalFormat("##.00");
    // String sectionText = "M:" + f.format(percents.getYellowPercent());
    // }
    // else if (column == 3){
    // DecimalFormat f = new DecimalFormat("##.00");
    // String sectionText = "H:" + f.format(percents.getRedPercent());
    // }
    // else if (column == 4){
    // DecimalFormat f = new DecimalFormat("##.00");
    // String sectionText = "S:" + f.format(percents.getBlackPercent());
    // }
    else if(column == table.getColumnCount()-1)
    setBorder( new MatteBorder(1, 0, 1, 1, colorBorder) );
    else
    setBorder( new MatteBorder(1, 0, 1, 0, colorBorder) );
    } else {
    if(column == 0)
    setBorder(new EmptyBorder(1, 1, 1, 0));

    else if(column == 1) {

    setBackground(colorGreen);


    if(percents == null) {
    return new JLabel("");
    }
    DecimalFormat f = new DecimalFormat("##.00");

    String sectionText = "N:" + f.format(percents.getGreenPercent());


    return new JLabel(sectionText);
    }

    else if(column == 2) {
    setBackground(colorYellow);
    if(percents == null) {
    return new JLabel("");
    }
    DecimalFormat f = new DecimalFormat("##.00");

    String sectionText = " M:" + f.format(percents.getYellowPercent());
    return new JLabel(sectionText);
    }

    else if(column == 3) {
    if(percents == null) {
    return new JLabel("");
    }
    DecimalFormat f = new DecimalFormat("##.00");

    String sectionText = " H:" + f.format(percents.getRedPercent());
    return new JLabel(sectionText);
    }
    else if(column == 4) {
    if(percents == null) {
    return new JLabel("");
    }
    DecimalFormat f = new DecimalFormat("##.00");

    String sectionText = " S:" + f.format(percents.getBlackPercent());
    return new JLabel(sectionText);
    }

    else if(column == table.getColumnCount()-1) {
    setBorder(new EmptyBorder(1, 0, 1, 1));

    } else {
    setBorder(new EmptyBorder(1, 0, 1, 0));
    }
    }


    int rightColumn = table.convertColumnIndexToModel(column);
    if(rightColumn == 0) {
    setHorizontalAlignment(SwingConstants.LEFT);
    setText(section.getId());
    }

    return this;
    }

    but I don't know what's wrong with it.
    Can you please help me with my code?

    Thank you very much in advance

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Backgorund of a cell in a table

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Backgorund of a cell in a table

    Firstly, please wrap your code with code tags, or under Advanced mode click on the hash button on the toolbar to get highlighting and preserve formatting. Code in your first post is still readable, but the second...

    Secondly,
    "Our life is frittered away by detail. Simplify, simplify." - Henry David Thoreau

    When debugging a difficult problem, as far as possible simplify your code to the bare minimum so as not to be distracted by irrelevant details. Assuming your custom TableCellRenderer extends JLabel, the following is a simple example:

    Java Code:
    	class ColourColumnRenderer extends JLabel implements TableCellRenderer {
    
    		private Color bgColour;
    
    		ColourColumnRenderer(Color bgColour) {
    			super();
    			this.bgColour = bgColour;
    			
    			setOpaque(true);
    		}
    	
    		@Override
    		public Component getTableCellRendererComponent(JTable table, Object value,
    			boolean isSelected, boolean hasFocus, int row, int column) {
    
    			// Background and Foreground
    
    			setBackground(Color.GRAY);
    
    			if ((row % 2) == 0) {
    				setBackground(bgColour);
    			}
    
    			setForeground(Color.BLACK);
    
    			return this;
    		}
    	}
    Code in your second post is too cluttered to be properly investigated, but my guess is you're missing the setOpaque(true) call that I have in the constructor above.

    If you already have setOpaque(true), or adding that doesn't help, try replacing your custom TableCellRenderer with my simple example above to confirm that you can get column background colouring, then add the border-creation code, and finally add the text-formatting code to rebuild your code step-by-step.

  6. #6
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default Re: Backgorund of a cell in a table

    Thanks

  7. #7
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default Re: Backgorund of a cell in a table

    thanks

  8. #8
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default Re: Backgorund of a cell in a table

    Hi,

    I have another problem now,
    if I set only the color of the columns it displays the colors, but if I also set the value that should be displayed in the column for separate rows, it doesn't display the color.
    Here is the code for two columns

    if((hasFocus) || (isSelected)) {
    if(column == 0)
    setBorder( new MatteBorder(1, 1, 1, 0, colorBorder) );
    else if (column == 1){
    if(percents == null) {
    return new JLabel("");
    }

    String sectionText = " N:" + f.format(percents.getGreenPercent());
    return new JLabel(sectionText);

    }
    else if (column == 2){
    setBackground(colorYellow);
    if(percents == null) {
    return new JLabel("");
    }
    } else {
    if(column == 0)
    setBorder(new EmptyBorder(1, 1, 1, 0));
    if(column == 1) {

    setBackground(colorGreen);

    }

    if(column == 2) {
    setBackground(colorYellow);

    }
    }
    On this way the colors are set nut not the values in the columns, and if a row is selected than the values are dispalyed, but not the colors.
    Can anybody please tell me why can't I use the both at the same time, setting the color and the value that should be displayed?
    Thanks

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Backgorund of a cell in a table

    The default renderer is (an extension of) a JLabel iself; you can set the color or the text of 'this'; don't create a new JLabel in that rendering method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    nat
    nat is offline Member
    Join Date
    Dec 2013
    Posts
    56
    Rep Power
    0

    Default Re: Backgorund of a cell in a table

    THANK YOU VERY VERY MUCH JosAH. I really appreciate your help a lot, and I finally solve my problem. Now it works just as I liked :)

Similar Threads

  1. How to get cell selection to work right in table?
    By Bill Ewing in forum SWT / JFace
    Replies: 4
    Last Post: 02-17-2011, 06:52 PM
  2. Using attributes of table cell
    By ajincoep in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-02-2011, 12:02 PM
  3. please help me in editing a table cell in AWT
    By kushiG in forum AWT / Swing
    Replies: 4
    Last Post: 10-11-2010, 03:41 PM
  4. If on cell of table
    By Tovam in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-18-2010, 09:34 AM
  5. Colour Specific Cell within Table
    By poddy88 in forum New To Java
    Replies: 2
    Last Post: 04-14-2009, 09:20 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
  •