3

puuuuuuf, I'm starting to like swing :) I'm trying to write a cellRenderer to customy render all cells besides those which in first row and column. So I wrote the following:

public class CustomTableCellRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);

            if(row >0&&column>0){
                cell.setBackground(Color.GREEN);
            }

            return cell;
        }
    }

and set the renderer as following:

scheduleTable.setDefaultRenderer(Object.class, new CustomTableCellRenderer()); 

but for some reason such an approach applies renderer to all the cell. So all of them are Green. If I'm doing something wrong, could you help me with that please?

Thanks in advance!

ADDITION

scheduleTable = new JTable() {
                    @Override
                    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                        Component comp = super.prepareRenderer(renderer, row, column);
                        int modelRow = convertRowIndexToModel(row);
                        int modelColumn = convertColumnIndexToModel(column);
                        if (modelColumn != 0 && modelRow != 0) {
                            comp.setBackground(Color.GREEN);
                        }
                        return comp;
                    }
                };  

this code makes all the table green as well.

This code:

scheduleTable = new JTable(tableModel) {
                    @Override
                    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                        Component comp = super.prepareRenderer(renderer, row, column);
                        int modelRow = convertRowIndexToModel(row);
                        int modelColumn = convertColumnIndexToModel(column);

                            if (modelRow != 0 && modelColumn != 0) {
                                setBackground(Color.GREEN);
                            } else {
                                setBackground(Color.WHITE);
                            }

                        return comp;
                    }
                };

gives me the following result ;(
enter image description here

The following situations I have with differents n in expression row != 0 && column != 0:
enter image description here

mr.nothing
  • 5,141
  • 10
  • 53
  • 77

2 Answers2

7

EDIT:

if (modelColumn != 0 || modelRow != 0) {

enter image description here

and with if (modelColumn != 0 && modelRow != 0) {

enter image description here

from code

import java.awt.*;
import java.awt.font.TextAttribute;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.*;

public class TablePrepareRenderer extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public TablePrepareRenderer() {
        Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
        Object[][] data = {
            {"Buy", "IBM", new Integer(1000), new Double(80.50), false},
            {"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
            {"Sell", "Apple", new Integer(3000), new Double(7.35), true},
            {"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
        };
        DefaultTableModel model = new DefaultTableModel(data, columnNames) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;
            private Border outside = new MatteBorder(1, 0, 1, 0, Color.red);
            private Border inside = new EmptyBorder(0, 1, 0, 1);
            private Border highlight = new CompoundBorder(outside, inside);

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                int modelRow = convertRowIndexToModel(row);
                int modelColumn = convertColumnIndexToModel(column);
                if (!isRowSelected(modelRow)) {
                    if (modelColumn != 0 || modelRow != 0) {
                        comp.setBackground(Color.GREEN);
                    } else {
                        comp.setBackground(table.getBackground());
                    }
                }
                return comp;

                /*Component comp = super.prepareRenderer(renderer, row, column);
                JComponent jc = (JComponent) comp;
                Map attributes = (new Font("Serif", Font.PLAIN, 12)).getAttributes();
                //attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
                attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
                if (!isRowSelected(row)) {
                comp.setForeground(Color.black);
                comp.setBackground(row % 2 == 0 ? Color.white : Color.orange);
                int modelRow = convertRowIndexToModel(row);
                String type = (String) getModel().getValueAt(modelRow, 0);
                if (type.equals("Sell")) {
                comp.setFont(new Font(attributes));
                comp.setForeground(Color.red);
                } else {
                comp.setFont(new Font("Serif", Font.BOLD, 12));
                }
                } else {
                comp.setFont(table.getFont());
                }
                jc.setBorder(BorderFactory.createCompoundBorder(jc.getBorder(), BorderFactory.createEmptyBorder(0, 0, 0, 5)));
                return comp;*/
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TablePrepareRenderer frame = new TablePrepareRenderer();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • tried it with prepare renderer, nothing changed. All the table is green, see the addition, please. – mr.nothing May 17 '12 at 12:10
  • mr.nothing I post wrong code I forgot for else statement, last update is correct and if (modelColumn != 0 && modelRow != 0) { works too – mKorbel May 17 '12 at 12:36
  • it seems that I have problems with... I even can't guess with what :D See my edit, please. – mr.nothing May 17 '12 at 13:08
  • 1
    and what's happens when you replace `if (modelColumn != 0 || modelRow != 0) {` with `if (column != 0 || row != 0) {`, that meaning ignore the value from `Model` and only directly painting by the value from `JTable's view` – mKorbel May 17 '12 at 13:22
  • hm, it is strange, but nothing changes. The result is the same. Is the convertXXXXXToModel() depends on models methods somehow? It seems, that problem is hidden in my model. – mr.nothing May 17 '12 at 13:35
  • then force that with -1 value instrad of 0 – mKorbel May 17 '12 at 13:36
  • but my question is [are you using](http://stackoverflow.com/questions/10625075/jtable-row-header-text), then output is correct – mKorbel May 17 '12 at 13:45
  • no, that's doesn't work, all the table is green in case of -1. But in case of 1 have really interesting situation. See edit please, it is really strange. p.s. I don't use the tips from there, unfortunately we had to refuse them. – mr.nothing May 17 '12 at 13:49
0

Use the row and column value without converting into model

Deb
  • 31
  • 1
  • 2
  • no, that's unrelated (though probably more what the OP wants to achieve, guessing only :-) The original problem was to have set the background only in one branch of the if when it must be done always. – kleopatra Apr 17 '13 at 07:30