🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #12827

Re: Changin cell color in JTable

#12827
From: Alberto Robles
Date: Wed, 04 Nov 1998 00:00
129 lines
4234 bytes
Thanks Matt!
I've succeed in changing JTable behaviour. This is an extract of my
solution:

public class Frame1 extends JFrame {

  TableModel1 myModel = new TableModel1();
  JTable jTable1 = new JTable(myModel); // Create the table with your
own model.
  [...]

  public Frame1() {
    setUpColorRenderer(jTable1); // Change the Table render for Labels
with Color.
    [...]
  } 
  
  [...]

  class TableModel1 extends AbstractTableModel {
    Object[][] data = null; // This are the labels with different
colors.
    [...]

    public TableModel1(){
           [...]
           data = new Object[Max][1];
           for(int i=1;i<= Max;i++){
             TextWithBckg text= new TextWithBckg(" "+i+"
",Color.lightGray);
             data[i-1][0]=(Object)text;                 
           }
    }
    [...]
  }   

class ColorRenderer extends JLabel implements TableCellRenderer {
  Border unselectedBorder = null;
  Border selectedBorder = null;
  boolean isBordered = true;

  public ColorRenderer(boolean isBordered) {
            super();
	          this.isBordered = isBordered;
            setOpaque(true); //MUST do this for background to show up.
    }
        
    public Component getTableCellRendererComponent(
                                JTable table, Object b, 
                                boolean isSelected, boolean hasFocus,
                                int row, int column) {
    if (b!=null){
      TextWithBckg data=(TextWithBckg)b;
      setText(data.text);
      setHorizontalAlignment(com.sun.java.swing.SwingConstants.RIGHT);
      setBackground(data.color);
    }
      if (isBordered) {
       if (isSelected) {
		    if (selectedBorder == null) {
		        selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
                                                 
table.getSelectionBackground());
		    }
        setBorder(selectedBorder);
	     } else {
		    if (unselectedBorder == null) {
		        unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
                                                 
table.getBackground());
		    }
        setBorder(unselectedBorder);
	     }
      }
      
      return this;
    }
}

}

class TextWithBckg {

  String text;
  Color color;

  public TextoConfondo(String t, Color c){
    text=t;
    color=c;
  }  
}


And that's all necessary changes. Please, note that this is just an
extract and must be completed with all necessary methods. The URL
http://java.sun.com/docs/books/tutorial/ui/swing/table.html contains all
hints to do it.

Alberto.


Matt Krevs wrote:
> 
> Check out the following link.
> http://java.sun.com/docs/books/tutorial/ui/swing/table.html
> 
> It discusses many of the aspects of JTables.
> Basically you want to create your own table renderer class and set the
> background color based on your criteria.
> 
> Alberto Robles wrote in message <363F2A8D.D2D20E56@tecsidel.es>...
> >I've looking for a way to change one cell background colour in a JTable.
> >Does anyone succeed in changing it?
> >
> >Thanks
> >Albert
> >
> >--
> >--------------------------------------------------------------------
> >Alberto Robles | Contrary to popular belief, Unix is user friendly.
> > Tecsidel,S.A. | It just happens to be selective about who it makes
> >+34-93-2922110 | friends with.


-- 
--------------------------------------------------------------------
Alberto Robles | Contrary to popular belief, Unix is user friendly. 
 Tecsidel,S.A. | It just happens to be selective about who it makes
+34-93-2922110 | friends with.
Technical Details
Message-ID: <36408CAB.9A68B62C@tecsidel.es>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <363F2A8D.D2D20E56@tecsidel.es> <71nrtf$8iv2@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,dbd640fd7c418ab6
X-Google-Attributes: gid111d45,public
From: Alberto Robles <alberto.robles@tecsidel.es>
Subject: Re: Changin cell color in JTable
Date: 1998/11/04
Message-ID: <36408CAB.9A68B62C@tecsidel.es>
X-Deja-AN: 408355383
Content-Transfer-Encoding: 7bit
References: <363F2A8D.D2D20E56@tecsidel.es> <71nrtf$8iv2@forums.borland.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: TECSIDEL
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing