tags:

views:

440

answers:

2

Hi, is possible to turn header text bold when I select a cell of its column? How to do it?

Thanks Leandro

A: 

It is partially possible. What you have to do is write your model is such way that on table's cell selection your model executes fireTableStructureChanges method( assuming you use AbstractTableModel as a base). This will repaint the whole table including column header. All you have to do is keep the state of which column is selected.

In the beginning I said "partially" possible. That is because calling fireTableStructureChanges will revalidate the whole table and you will lose your current column model state - column widths and sequence.

To make your text bold you can use HTML - something like <html><b>your text</b></html>, but it has to change dynamically based on your model's internal state

UPDATE: Also column table header text can be set directly but model change or tableStructureChanged event will make the table to reread from the model.

eugener
This isn't correct solution. You are mixing Model and UI together.
Rastislav Komara
Not necessarily. Since column names are returned from the model - they are part of the model. Model just needs to react on selection change somehow. BTW your solution brakes look and feel because you're using cell renderer to renderer header. And as far as I know it is now possible to truly replicate default header renderer
eugener
+1  A: 

Set custom renderer into table header. If current column is selected column set font to bold.

JTable table = new JTable()
table.getTableHeader().setDefaultRenderer(new MyRenderer());

class MyRenderer implements TableCellRenderer {
    //todo implement
}
Rastislav Komara
I don't know why but it is not respected by my look and feel. I can change the foreground but it's impossible to change the text decorate.
Leandro
It is a CELL renderer. There is no real TableHeader renderer. See my comment below.
eugener
Table header uses sun.swing.table.DefaultTableCellHeaderRenderer to render each column in header row. Unfortunately sun.** is private SUN package. You will need to make more painting as usual to meet LaF.
Rastislav Komara