Skip to main content

About JXTable & DefaultTableCellRenderer

3 replies [Last post]
yael800
Offline
Joined: 2006-09-27

Hi,
I want to get Yael's name in red, but It's not work in a JXTable..WHY???
Thanks :)

<br />
import java.awt.*;</p>
<p>import javax.swing.*;<br />
import javax.swing.table.*;</p>
<p>class Test extends JFrame<br />
{<br />
  public Test()<br />
  {<br />
    setLocation(400,100);<br />
    setDefaultCloseOperation(EXIT_ON_CLOSE);<br />
    JTable table = new JTable(2,2);</p>
<p>    String[] columnNames = {"Name","Id"};<br />
	Object[][] data = new Object[2][2];<br />
		data[0][0] = ("Yael");<br />
		data[0][1] = ("Dan");<br />
		data[1][0] = (1);<br />
		data[1][1] = (2);</p>
<p>	table = new JTable(new DefaultTableModel(data, columnNames));<br />
    table.setDefaultRenderer(Object.class,new changefontColour());<br />
    getContentPane().add(new JScrollPane(table));<br />
    pack();<br />
  }<br />
  public static void main (String[] args){new Test().setVisible(true);}<br />
}<br />
class changefontColour extends DefaultTableCellRenderer<br />
{<br />
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)<br />
  {<br />
    JLabel lbl = (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);</p>
<p>    if (isSelected) {<br />
		lbl.setForeground(Color.white);<br />
		lbl.setBackground(new Color(0, 0, 139));<br />
	}</p>
<p>   else if(value.equals("Yael"))<br />
    {<br />
    	lbl.setForeground(Color.RED);<br />
    }</p>
<p>    return lbl;<br />
  }<br />
}</p>
<p>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nr
Offline
Joined: 2007-03-02

Hi Yael,

I think you need to use an Highlighter for foreground and background colors changes.

++
Nat

yael800
Offline
Joined: 2006-09-27

Thanks,
Do u have an example about how to set color only for one row from this table
and not for all the table.. ?
table.setHighlighters(highlighterPipeline);

[code]
Highlighter[] highlighters = new Highlighter[] {
new AlternateRowHighlighter(Color.white,
new Color(0xF0, 0xF0, 0xE0), null),
new PatternHighlighter(null, Color.red, "^s", 0, 0)
};

HighlighterPipeline highlighterPipeline = new HighlighterPipeline(highlighters);
JXTable table = new JXTable();
table.setHighlighters(highlighterPipeline);

[/code]

Message was edited by: yael800

kleopatra
Offline
Joined: 2003-06-11

[code]
SearchHighlighter patternHighlighter = new SearchHighlighter(new Color(0, 0, 139), Color.WHITE);
patternHighlighter.setPattern("Yael", 0);
patternHighlighter.setHighlightAll();
table.addHighlighter(patternHighlighter);

[/code]

Resources for code snippets and implications (some might call it limitations )

- older threads - a bit inconvenient to search, but unfortunately that's the biggest bunch of documentation we have right now
- the swinglabs wiki, which has pages dedicated to swingx renderer and highlighters
- the xVisualCheck (demo) classes in the renderer package of the test code hierarchy

Cheers
Jeanette