Question

I am going to be using a lot of custom TableCellRenderers in a JTable, but I don't want to have to recreate the default properties for every single one, because it seems I have to start with a plain JLabel. This creates a ton of annoying overhead, because I even have to populate the value myself as well as the matching background, the foreground, the font, etc... to match the rest of the JTable.

Is there a way I can retrieve the parent TableCellRenderer or the it's resulting JLabel and manipulate that? That way I have all the defaults set already, and I can just manipulate the properties I am actually changing? I have tried everything with super.getCellRenderer and it is not giving me anything to accomplish that.

Also, my TableCellRenderer is not column-specific. Each cell can vary.

public class ActionTable extends JTable  {

...

public TableCellRenderer getCellRenderer(int row, int column) {

    String colName = ((ActionTableModel)this.getModel()).getColumnName(column);

    if ( colName.equals("CUSTOM COL") ) {
        return  new ActionCellRenderer(this);
    }

    return super.getCellRenderer(this.convertRowIndexToModel(row), this.convertColumnIndexToModel(column));

}

public class ActionCellRenderer extends JLabel implements TableCellRenderer {


private ActionTable actionTable;

public ActionCellRenderer(ActionTable actionTable) {
    this.actionTable = actionTable;
    setOpaque(true); //MUST do this for background to show up.
}

@Override
public Component getTableCellRendererComponent(JTable table, Object color,boolean isSelected, boolean hasFocus,int row, int column) {



    int modelRow =  actionTable.convertRowIndexToModel(row);
    int modelCol = actionTable.convertColumnIndexToModel(column);

    String colName = table.getModel().getColumnName(modelCol);

 /* 
annoying overhead to retrieve default cell renderer properties
to match the rest of the JTable
*/
    if (isSelected)
    {
        setBackground(table.getSelectionBackground());
        setForeground(table.getSelectionForeground());
    }
    else
    {
        setBackground(table.getBackground());
        setForeground(table.getForeground());
    }


    //AND I HAVE TO RETRIEVE THE VALUE MYSELF TOO
    String textVal = ((ActionTableModel)table.getModel()).getValueAt(modelRow, modelCol).toString();



    this.setHorizontalAlignment(SwingConstants.CENTER);
    this.setFont(new Font("Serif", Font.PLAIN, 15));

    this.setText(textVal);

//NOW I CAN DO CUSTOMIZATIONS
//PUT CUSTOMIZATIONS HERE

    return this;

}

Was it helpful?

Solution

Just have your ActionCellRenderer extend DefaultTableCellRenderer and call the getTableCellRendererComponent() method to get the JLabel:

public class ActionCellRenderer extends DefaultTableCellRenderer{

   public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus,int row, int column){

      //get the label
      JLabel label = (JLabel)super.getTableCellRendererComponent(table, value,isSelected, hasFocus,row, column);

      //do whatever you want with the label

      return label;
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top