Why doesn't cell renderer work ???

Z

zbyszek.kielich

Hi!
I'm writing an application in which the table is displayed. I want to
some rows be bold. I don't know why the code below doesn't work :

//class 1
(...)
table = new JTable(tableModel);
table.setDefaultRenderer(Object.class, new
MyTableCellRenderer());
(...)


// class 2
public class MyTableCellRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
boolean value, boolean isSelected,
boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);

if( row % 2 == 0)
{
cell.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
}
else
{
cell.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 22));
}

return cell;
}
}
 
R

Roland de Ruiter

Hi!
I'm writing an application in which the table is displayed. I want to
some rows be bold. I don't know why the code below doesn't work :

// class 2
public class MyTableCellRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
boolean value, boolean isSelected,
^^^^^^^^^^^^^
Object value
boolean hasFocus, int row, int column)

Incorrect method signature: second parameter (value) must be of type
Object instead of boolean.
 
R

Roedy Green

{
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);

if( row % 2 == 0)
{
cell.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
}
else
{
cell.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 22));
}

return cell;

You are mixing Swing and AWT. AWT supports only the logical fonts.
That may be the problem. I have some notes on writing renderers with
examples at http://mindprod.com/jgloss/jtable.html

Try making yours match mine more closely.

As an aside, I would create my two Font objects statically rather than
every since time you need to render a cell.
 
S

Stefan Rybacki

Roland said:
^^^^^^^^^^^^^
Object value


Incorrect method signature: second parameter (value) must be of type
Object instead of boolean.
Eagle eye ;)

@OP: try to use the @Override annotations to get a compiler error in such a case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top