Color Cell基于价值

时间:2014-10-09 21:21:39

标签: java jtable renderer jxtable

我正在使用JXTable并尝试根据值为行着色,但是显示为空(但渲染正在运行,因为它在控制台中显示了syso)

public class MyCellRenderer extends JLabel implements TableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
            SimpleTableModel mymodel = (SimpleTableModel) jxtableListar.getModel(); 
            ModelProtocolo actualModel= (ModelProtocolo ) mymodel.getProtocolo(rowIndex) ;

            if(actualModel.getValue() > 0) {
                System.out.println("Yep the Render is working");

            }
                        return this;
        }
    }


jxtableListar.setDefaultRenderer(Object.class, new MyCellRenderer ());

我没有找到任何有用的"教程"如何使用JXTable渲染器,因为他们谈论Hightlight的一些内容,但所有的教程都是"信息"学习

是否有任何好的方法/教程可以根据单元格的值为JXTable行着色?

1 个答案:

答案 0 :(得分:0)

您的渲染器返回的JLabel(本身)尚未设置任何内容。相反,扩展DefaultTableCellRenderer

public class MyCellRenderer extends DefaultTableCellRenderer {
    @override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
        SimpleTableModel mymodel = (SimpleTableModel) jxtableListar.getModel(); 
        ModelProtocolo actualModel= (ModelProtocolo ) mymodel.getProtocolo(rowIndex) ;

        JLabel label = (JLabel) super.getTableCellRendererComponent(/* pass in all params */);
        label.setText(/*whatever the text should be*/);
        label.setBackground(/*whatever the color should be*/);
        return label;
    }
}