CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2000
    Posts
    16

    JTable(urgent)!!!

    Hi,
    How do I change foreground color of an indivisual cell in JTable? I need to change the color depending on some condition.
    I need an urgent help.
    Thanks.


  2. #2
    Join Date
    Nov 1999
    Location
    Indianapolis, IN
    Posts
    191

    Re: JTable(urgent)!!!

    Here's how I did mine:

    In the constructor of your GUI class, call
    table().setDefaultRenderer(new String().getClass(), new JLabelTableCellRenderer());



    package dataccesstool.gui;

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.JLabel;
    /**
    * A specific implementation of a JLabel which can be used in a JLabel, and the JLabel's
    * background color will change depending on whether or not its text is valid. This
    * class is used exclusively by the MultiFileDeleteDialog.
    * @author Michael Pell, Solutions Plus, Inc.
    */
    public class JLabelTableCellRenderer extends JLabel implements TableCellRenderer
    {
    public JLabelTableCellRenderer()
    {
    super();
    setOpaque(true); /* it´s essential */
    this.setBackground(Color.white);
    }
    public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
    String text = (String)value;
    //if(text.equals("Waiting"))
    //setBackground(Color.yellow); /* Decides the color else if(text.equals("Red")) * setBackground(Color.red); */

    setText(text); /* Put the current text in the JLabel */
    MultiFileDeleteTableModel tableModel = (MultiFileDeleteTableModel)table.getModel();
    if (! tableModel.isTextValid( row, column ) )
    setBackground(Color.yellow);

    return this;
    }
    }






Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured