Using TableCellRenderer and getColumnClass together

岁酱吖の 提交于 2019-12-09 03:44:39

问题


when i add getcolumn class to my abstracttablemodel, i couldnt use my custom TableCellRenderer to set background color. (i use this for sorting,alignment numeric columns)

public Class getColumnClass(int columnIndex) {
        Object o = getValueAt(0, columnIndex);
        if (o == null) {
          return Object.class;
        } else {
          return o.getClass();
        }
      }

This is full of my code.

import java.awt.*;
import java.text.DecimalFormat;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.table.*;

public class DemoRenderer extends JFrame {

    public static void main( String[] args ) {
        DemoRenderer frame = new DemoRenderer();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
    }

    public DemoRenderer() {

                JTable table = new JTable();
                table.setModel(new MyTablemodel());
                table.setDefaultRenderer(Object.class, new MyCustomTableCellRenderer());

                // Tell the table what to use to render our column of doubles

                table.repaint();
                //table.getColumnModel().getColumn(1).setCellRenderer(new DecimalFormatRenderer() );                
        getContentPane().add(new JScrollPane(table));
    }

}

/**
         Here is our class to handle the formatting of the double values
         */

class MyCustomTableCellRenderer extends DefaultTableCellRenderer{

    private static final DecimalFormat formatter = new DecimalFormat( "#0.00" );

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

        if(column==1) obj = formatter.format((Number)obj);        
        Component cell = super.getTableCellRendererComponent(
            table, obj, isSelected, hasFocus, row, column);
    if (isSelected) {
    cell.setBackground(Color.green);
    } 
    else {
    if (row % 2 == 0) {
    cell.setBackground(Color.cyan);
    }
    else {
    cell.setBackground(Color.lightGray);
    }
    }    
    return cell; 
    }
}

class MyTablemodel extends AbstractTableModel{

    Object[] columnNames = { "A", "B", "C" };
    Object[][] data = {
    { "1abc", new Double(850.503), 53 },
    { "2def", new Double(36.23254), 6 },
    { "3ghi", new Double( 8.3 ), 7 },
    { "4jkl", new Double( 246.0943 ), 23 }};

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }


    public Class getColumnClass(int columnIndex) {
        Object o = getValueAt(0, columnIndex);
        if (o == null) {
          return Object.class;
        } else {
          return o.getClass();
        }
      }

}

thank you very much for your opinions.


回答1:


Your getColumnClass() method will return: String.class, Double.class and Integer.class for columns 0, 1, 2.

JTable will provide the default renderer for the Double and Integer columns.

If you want to use your custom renderer for all your columns then you need to do:

MyCustomTableCellRenderer renderer = new MyCustomTableCellRenderer();
table.setDefaultRenderer(Object.class, renderer); // or you could use "String.class"
table.setDefaultRenderer(Double.class, renderer);
table.setDefaultRenderer(Integer.class, renderer);

When you use "Object.class" it means use the Object renderer as a last resort renderer, only if no other custom renderer for the specific class has been added to the table.




回答2:


Use TableColumn#setCellRenderer() to draw individual values for each column.

Just add below lines in your code

MyCustomTableCellRenderer cellRenderer = new MyCustomTableCellRenderer();
for (int i = 0; i < table.getColumnCount(); i++) {
    table.getColumnModel().getColumn(i).setCellRenderer(cellRenderer);
}

instead of

table.setDefaultRenderer(Object.class, new MyCustomTableCellRenderer());

and your problem will be solved.

Note: You are using DefaultTableCellRenderer that will apply on cell hence use setCellRenderer() instead of setDefaultRenderer().

screenshot: (one row is selected)



来源:https://stackoverflow.com/questions/23369527/using-tablecellrenderer-and-getcolumnclass-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!