java - 一起使用 TableCellRenderer 和 getColumnClass

标签 java swing jtable tablecellrenderer abstracttablemodel

当我将 getcolumn 类添加到我的 Abstracttablemodel 时,我无法使用自定义 TableCellRenderer 来设置背景颜色。 (我用它来排序、对齐数字列)

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

这全是我的代码。

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();
        }
      }

}

非常感谢您的意见。

最佳答案

您的 getColumnClass() 方法将返回:第 0、1、2 列的 String.class、Double.class 和 Integer.class。

JTable 将为 Double 和 Integer 列提供默认呈现器。

如果您想对所有列使用自定义渲染器,那么您需要执行以下操作:

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);

当您使用“Object.class”时,这意味着仅当没有将特定类的其他自定义渲染器添加到表中时,才使用对象渲染器作为最后的渲染器。

关于java - 一起使用 TableCellRenderer 和 getColumnClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369527/

相关文章:

java - 在 Java 中,如何在一个类中使用另一个类的方法?

javascript - java中的正则表达式从javascript方法获取参数

java - 如何使用arcobject(Arcgis 10)构建java桌面应用程序?

java - 获取触发 RowSorterEvent 的列

java - JTable:看不到列名

java - SwingWorker异常

java - 将一维数组迭代为二维数组

java - Gif 安全颜色选择器

java - 如何设置JTable中特定单元格的值?

java - 如何使用TableColumnModelListener根据java中的重新定位来更改列颜色