Hello All,
I am trying to set tooltip for the column headers of a table. While doing so, I am losing the previous font, borders and color values of the table. Though I am able to set back the color but not the font and border.
The code is a bit like this:
public class SampeTable extends TableScrollPane{
private JTable _mainTable=null;
public JTable buildTable(){
if (_mainTable == null)
{
_mainTable = this.getMainTable();
//Setting the widths as per column specifications
List<TicketColumn> columnsList= _appControlIntf.getBlotterTableModel().getColumns();
Color color=null;
Font font=null;
for(int i=0;i<columnsList.size();i++)
{
TableColumn tableColumn = null;
switch (getInBlotterTableModel().getColumnType(i))
{
case MultiTableModel.HEADER_COLUMN:
tableColumn = getRowHeaderTable().getColumnModel().getColumn(i);
break;
default:
tableColumn = getMainTable().getColumnModel().getColumn(i - getRowHeaderTable().getColumnCount());
}
tableColumn.setPreferredWidth(columnsList.get(i).getWidth());
// Render the first column as a checkbox
if (i == 0)
{
tableColumn.setCellRenderer(new BooleanCheckBoxCellRenderer());
color = ((BooleanCheckBoxCellRenderer) tableColumn.getCellRenderer()).getBackground();
font = ((BooleanCheckBoxCellRenderer) tableColumn.getCellRenderer()).getFont();
font = font.deriveFont(Font.BOLD);
tableColumn.setCellEditor(new BooleanCheckBoxCellEditor());
}
else
{
JLabel headerRenderer = new DefaultTableCellRenderer();
headerRenderer.setBackground(color);
headerRenderer.setFont(font);
headerRenderer.setToolTipText("Hello");
tableColumn.setHeaderRenderer((TableCellRenderer) headerRenderer);
}
}
initializeTable(_mainTable);
initializeTable(getRowHeaderTable());
}
return _mainTable;
}
private void initializeTable(JTable table)
{
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
((SortableTable) table).setOptimized(true);
JTableHeader header = table.getTableHeader();
header.setBorder(BorderFactory.createRaisedBevelBorder());
header.setFont(header.getFont().deriveFont(java.awt.Font.BOLD));
table.addMouseListener(new MouseListener()
{
public void mouseReleased(MouseEvent e)
{
showPopup(e);
}
public void mousePressed(MouseEvent e)
{
showPopup(e);
}
private void showPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
getInboundBlotterPopup().show(e.getComponent(), e.getX(), e.getY());
}
}
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
_appControlIntf.showFixMessageBlotter(getSelectedInboundTicketView());
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
});
}
}
With the above code, I am able to set the tooltip text, but the font and border are being compromised. For the column (i=0), the font and borders are fine as I am not using the DefaultCellRenderer. For the rest of the columns the color attribute is being set but not the font. I even tried to set the borders using the setBorder(), but to no avail.
Any help would be appreciated. Let me know if more inputs/code is required.
Regards,
pp_dv