0
votes

Hi i am working on jtable and i have to work with cell renderer in such a way that a am applying CurrencyRender in particular column. Also i am apply coloring ion each row. Everything going perfect but when i apply currencyRenderer in my numenric column, it lost the background color. This could be either due to adding cellrenderer of currency . Please suggest what should i do to color column with currency renderer .Here i my code

    this.installAllignment(this.tblDemandView.getColumnModel().getColumn(numAmount), SwingConstants.RIGHT);
    this.tblDemandView.getTableHeader().setReorderingAllowed(false);
    this.tblDemandView.getTableHeader().setResizingAllowed(true);
    tblDemandView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    NumberFormat _formatf = NumberFormat.getNumberInstance();
    _formatf.setMinimumFractionDigits(2);
    _formatf.setMaximumFractionDigits(2);

    MyCurrencyRenderer _rendererf = new MyCurrencyRenderer(_formatf); 
    TableColumnModel _model = tblDemandView.getColumnModel();
    TableColumn _columnPu=_model.getColumn(_model.getColumnIndex("Amount"));
     _columnPur.setCellRenderer(_rendererf);

private void installAllignment(TableColumn tableColumn, final int alignmentCode) {

    tableColumn.setCellRenderer(new DefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                       boolean hasFocus, int row, int column) {
            Component myself =
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            setHorizontalAlignment(alignmentCode);

           DefaultTableModel model = (DefaultTableModel) table.getModel();
                  Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
     DefaultTableCellRenderer() {
            if (row == table.getRowCount() - 1) {
                Insets insets = new Insets(1, 0, 0, 0);

                // setBorder(BorderFactory.createLineBorder(Color.BLACK));
            }

            return myself;
        }
    });

}
2
A better solution would be to devise a delegate pattern, which would applied to each column, so that it would call a series of delegates to provide the rendering functionality in a pluggable way - MadProgrammer
This is actually hard then it might seem. Using nothing but the default API, you would need a base renderer class which handled the row colouring, you would then use it as the parent class for all you other renderers. This isn't always possible, nor is it particular pretty and becomes and management headache very quickly. Instead, you could utilities one of the many candy stripping APIs, such as this example - MadProgrammer

2 Answers

0
votes

Your code doesn't make a lot of sense. You don't do anything with the model, c or insets variables.

But changing the renderer is really pretty trivial. Subclass the TableColumn and override getTableCellRenderer. In the body call the super method to generate the default renderer component and then set the background before returning it.

Alternatively you can implement your own TableCellRenderer and then call setTableCellRenderer on the column rather than subclassing.

Both approaches work fine and are useful in different situations.

0
votes

Everything going perfect but when i apply currencyRenderer in my numenric column, it lost the background color.

I don't know if you are talking about losing the selection background color, or some other color you use for custom rendering.

Try using Table Row Rendering instead of cell rendering. This approach override the prepareRenderer(...) method of the table to do custom coloring.

You may also want to check out Table Format Renderers in the same blog. It show how to create custom renderer easily.

By the way the code in your renderer makes no sense to me:

Component myself = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setHorizontalAlignment(alignmentCode);

DefaultTableModel model = (DefaultTableModel) table.getModel();
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

I have no idea why you would be invoking the getTableCellRendererComponent(...) method twice.