Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1897 > unrolled thread

Why there is no setCellRe

Started by"RC" <rc@THRWHITE.remove-dii-this>
First post2011-04-27 15:35 +0000
Last post2011-04-27 15:36 +0000
Articles 8 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Why there is no setCellRe "RC" <rc@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
    Re: Why there is no setCe "Adrian Hands, Raleigh NC" <adrian.hands,.raleigh.nc@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
      Re: Why there is no setCe "RC" <rc@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
        Re: Why there is no setCe "Adrian Hands, Raleigh NC" <adrian.hands,.raleigh.nc@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000
    Re: Why there is no setCe "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000
    Re: Why there is no setCe "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000
      Re: Why there is no setCe "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000
        Re: Why there is no setCe "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000

#1897 — Why there is no setCellRe

From"RC" <rc@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectWhy there is no setCellRe
Message-ID<f5rn13$mhf$1@news.nems.noaa.gov>
  To: comp.lang.java.gui,comp.l
Hi there,

So far I only found

JTable.setDefaultRenderer(Class, TableCellRenderer);

I would like set foreground color in 1st column of my table,
and also want to detected the selection listener.

Here are what I did as following, No errors, but nothing
is work.

Please help.
Thank Q!

table.setDefaultRenderer(Color.class,
       new LabelDefaultTableCellRenderer());



public class LabelDefaultTableCellRenderer extends JLabel
implements TableCellRenderer {

	private static final long serialVersionUID = 1L;

	public LabelDefaultTableCellRenderer() {
		super();
		setOpaque(true);
	}

	public Component getTableCellRendererComponent(JTable table,
                                                Object color,
                                                boolean isSelected,
                                                boolean hasFocus,
                                                int row,
                                                int column) {
		Color newColor = (Color)color;
		
		if (column == 1) {
			setForeground(Color.red);
			
		}	

		if (isSelected) {
			System.out.println("Cell selected at " +
                                        row + ", " + column);
		}
		return (this);
	}
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1898 — Re: Why there is no setCe

From"Adrian Hands, Raleigh NC" <adrian.hands,.raleigh.nc@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Why there is no setCe
Message-ID<1182884216.334575.70140@w5g2000hsg.googlegroups.com>
In reply to#1897
  To: comp.lang.java.gui,comp.l
On Jun 26, 2:48 pm, RC <raymond.c...@nospam.noaa.gov> wrote:

> public class LabelDefaultTableCellRenderer extends JLabel

no, don't extend JLabel

>         public Component getTableCellRendererComponent(JTable table,
>                                                 Object color,
>                                                 boolean isSelected,
>                                                 boolean hasFocus,
>                                                 int row,
>                                                 int column) {
>
>         java.awt.Component comp =
>                 super.getTableCellRendererComponent( table,
>                                                 color,
>                                                 isSelected,
>                                                 hasFocus,
>                                                 row,
>                                                 column;
>
>                 // Color newColor = (Color)color;
>
>                 comp.setForeground( column == 1 ? Color.red : Color.black );
>
>                 return comp;

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1899 — Re: Why there is no setCe

From"RC" <rc@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: Why there is no setCe
Message-ID<46816424.1080609@nospam.noaa.gov>
In reply to#1898
  To: Adrian Hands, Raleigh NC
Adrian Hands, Raleigh NC USA wrote:
> On Jun 26, 2:48 pm, RC <raymond.c...@nospam.noaa.gov> wrote:
> 
>> public class LabelDefaultTableCellRenderer extends JLabel
> 
> no, don't extend JLabel

Then what I should extends?

I got the example from this page

http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TableDialogEditDemoProject/src/components/ColorRenderer.java


> 
>>         public Component getTableCellRendererComponent(JTable table,
>>                                                 Object color,
>>                                                 boolean isSelected,
>>                                                 boolean hasFocus,
>>                                                 int row,
>>                                                 int column) {
>>
>>         java.awt.Component comp =
>>                 super.getTableCellRendererComponent( table,
>>                                                 color,
>>                                                 isSelected,
>>                                                 hasFocus,
>>                                                 row,
>>                                                 column;
>>
>>                 // Color newColor = (Color)color;
>>
>>                 comp.setForeground( column == 1 ? Color.red : Color.black );
>>
>>                 return comp;
>

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1901 — Re: Why there is no setCe

From"Adrian Hands, Raleigh NC" <adrian.hands,.raleigh.nc@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
SubjectRe: Why there is no setCe
Message-ID<1182886445.981081.173020@q69g2000hsb.googlegroups.com>
In reply to#1899
  To: comp.lang.java.gui,comp.l
public class Trash
{
    public static void main( String[] args )
    {
        javax.swing.JTable table = new javax.swing.JTable( 5, 5 );
        table.setDefaultRenderer( Object.class, new
LabelDefaultTableCellRenderer() );

        javax.swing.JFrame frame = new javax.swing.JFrame();
        frame.getContentPane().add( table );
        frame.pack();
        frame.setVisible( true );
    }

    public static class LabelDefaultTableCellRenderer
        extends javax.swing.table.DefaultTableCellRenderer
    {

        private static final long serialVersionUID = 1L;

        public LabelDefaultTableCellRenderer() {
            super();
            setOpaque(true);
        }

        public java.awt.Component
getTableCellRendererComponent( javax.swing.JTable table,
 
Object color,
 
boolean isSelected,
 
boolean hasFocus,
                                                                 int
row,
                                                                 int
column ) {

            java.awt.Component comp =
                super.getTableCellRendererComponent( table,
                                                     color,
                                                     isSelected,
                                                     hasFocus,
                                                     row,
                                                     column );

            // Color newColor = (Color)color;

            // note: column 1 is the second column
            comp.setForeground( column == 1 ? java.awt.Color.red :
java.awt.Color.black );

            // note: this gets written every time the cell is
rendered, e.g. when it's uncovered, etc--very often!
            if (isSelected) {
                System.out.println("Cell selected at " +
                                   row + ", " + column);
            }
            return comp;
        }

    }

}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1900 — Re: Why there is no setCe

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
SubjectRe: Why there is no setCe
Message-ID<468163b8$0$8722$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1897
  To: comp.lang.java.gui,comp.l
RC wrote:
> public class LabelDefaultTableCellRenderer extends JLabel

That's an odd class name. DefaultTableCellRenderer is so called, because 
it's the implementation you get by default.

I wouldn't subclass JLabel. You don't need to subclass it, so having a 
reference to a label is probably a better idea. Although 
DefaultTableCellRenderer takes a different view.

The common thing to do is subclass DefaultTableCellRenderer.

> implements TableCellRenderer {
> 
>     private static final long serialVersionUID = 1L;
> 
>     public LabelDefaultTableCellRenderer() {
>         super();
>         setOpaque(true);
>     }
> 
>     public Component getTableCellRendererComponent(JTable table,
>                                                Object color,
>                                                boolean isSelected,
>                                                boolean hasFocus,
>                                                int row,
>                                                int column) {
>         Color newColor = (Color)color;
>        
>         if (column == 1) {
>             setForeground(Color.red);
>            
>         }   

And if the column isn't 1? Most (but not all) if statements should 
either end in return/break/continue/throw or have an else clause.

> 
>         if (isSelected) {
>             System.out.println("Cell selected at " +
>                                        row + ", " + column);
>         }
>         return (this);
>     }
> }

The obvious thing you are failing to do is set the text of the label. 
Without any text, you wont see the change of foreground very well.

Tom Hawtin

[Note: Followup-To: comp.lang.java.gui]

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1911 — Re: Why there is no setCe

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
SubjectRe: Why there is no setCe
Message-ID<f0k883tdchnkntnrj7skt6be489s0pkjdf@4ax.com>
In reply to#1897
  To: comp.lang.java.gui,comp.l
Why there is no setCellRenderer in JTable? you ask.

It is very rare that the renderers for all columns are identical.  To
create such a beast, just loop over all columns.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1920 — Re: Why there is no setCe

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
SubjectRe: Why there is no setCe
Message-ID<4684c855$0$8729$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1911
  To: comp.lang.java.gui,comp.l
Roedy Green wrote:
> Why there is no setCellRenderer in JTable? you ask.

The documentation for getDefaultRenderer seems to imply that you could 
set a default renderer for all columns by setting the default renderer 
for Object.class, Number.class and Boolean.class.

Looking at the source, you would also need Float.class, Double.class, 
Date.class, Icon.class, and ImageIcon.class. Gotta love Swing.

Tom Hawtin

[Followup-to: comp.lang.java.gui]

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1921 — Re: Why there is no setCe

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
SubjectRe: Why there is no setCe
Message-ID<4684db06$0$8746$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1920
  To: comp.lang.java.gui
Tom Hawtin wrote:
> Roedy Green wrote:
>> Why there is no setCellRenderer in JTable? you ask.
> 
> The documentation for getDefaultRenderer seems to imply that you could 
> set a default renderer for all columns by setting the default renderer 
> for Object.class, Number.class and Boolean.class.
> 
> Looking at the source, you would also need Float.class, Double.class, 
> Date.class, Icon.class, and ImageIcon.class. Gotta love Swing.

Actually, a more reliable way of doing it is to subclass JTable and 
clear the protected defaultEditorsByColumnClass (UIDefaults) Hashtable.

Tom Hawtin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web