简体   繁体   English

jtable tablecellrenderer什么都不做

[英]jtable tablecellrenderer not doing anything

I am trying to make a check where if the value of cell 3 is not blank, the whole row will be colored green, however this does nothing but, if I make no check of that, only checking to be sure nothing is selected (so that selecting gives the color to show that you did select) everything is drawn green. 我正在尝试检查如果单元格3的值不为空白,那么整行将变为绿色,但这无济于事,如果我不对此进行检查,则仅检查以确保未选择任何内容(因此该选择将给出显示您确实选择的颜色)所有内容均绘制为绿色。

if (!table.isRowSelected(row))
            {
                component.setBackground(getBackground());


                if(table.getModel().getValueAt(row, 3).equals(""))
                {
                component.setBackground(Color.GREEN);
                }
            }

I tried to output the value and everything works properly, is there a problem here? 我试图输出值,并且一切正常,这有问题吗? A different way of doing this? 这样做的另一种方式? thank you 谢谢

I tried to output the value and everything works properly, is there a problem here? 我试图输出值,并且一切正常,这有问题吗? A different way of doing this? 这样做的另一种方式?

We would need some more code/info to answer this question properly: 我们需要更多代码/信息才能正确回答此问题:

In any case I'd suggest you take a look to Using Custom Renderer section in How to Use Tables trail. 无论如何,我建议您看一下“ 如何使用表格”路径中的 使用自定义渲染器”部分。 Also you can see the example below and take this as start point: 您还可以看到下面的示例并将其作为起点:

import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Demo {

    private void initGUI() {

        DefaultTableModel model = new DefaultTableModel(new Object[]{"Manufacturer", "Model", "Country", "Price"}, 0);
        model.addRow(new Object[]{"Fender", "Stratocaster", "Japan", ""});
        model.addRow(new Object[]{"Gibson", "Les Paul", "USA", "$ 1599"});
        model.addRow(new Object[]{"Jackson", "Soloist S3", "Japan","$ 1299"});
        model.addRow(new Object[]{"Paul Reed Smith","Standard 24", "USA", ""});

        JTable table = new JTable(model);

        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {                
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if(!isSelected){
                    Color background = table.getModel().getValueAt(row, 3).equals("") ? Color.GREEN : table.getBackground();
                    setBackground(background);
                } else {
                    setBackground(table.getSelectionBackground());
                }
                return this;
            }            
        });

        JFrame frame = new JFrame("Demo");      
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(new JScrollPane(table));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {   
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }
}

Picture 图片

在此处输入图片说明

Is your if statement correct? 您的if陈述正确吗? You have to check for non-blank cell 您必须检查非空白单元格

So, your if statement should look like this 因此,您的if语句应如下所示

if(!table.getModel().getValueAt(row, 3).equals(""))

Also to check value at cell#3, you have to use index 2 like this. 同样要检查单元格3的值,您必须使用这样的索引2。

if(!table.getModel().getValueAt(row, 2).equals(""))

A different way of doing this? 这样做的另一种方式?

Check out Table Row Rendering for a different approach that doesn't require you to use a custom renderer. 请查看“ 表格行渲染”,了解不要求您使用自定义渲染器的其他方法。

This approach is easier because you don't need a custom renderer when you have different types of data in each table column. 这种方法比较容易,因为在每个表列中都有不同类型的数据时,您不需要自定义渲染器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM