java - 在 JTable 单元格上编辑没有效果

标签 java swing jtable cell background-color

我想突出显示 JTable 中的某些单元格并进行必要的更改,但程序看不到它们。

当我调试代码时,我注意到程序渲染了表格,但没有转到 if 语句。

如果有人能告诉我出了什么问题,那就太好了。

    public class Frame extends JFrame {

        private JPanel contentPane;
        private static JTable table;
        final DefaultTableModel model;

    String data[][] = { {"E","S","M","I","S","T","P","G","L","E","I","C","H","Y","M"},
        {"G","H","J","K","F","K","N","F","Z","I","G","W","X","Z","T"} };

        String[] columns =  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };

    public static void main(String[] args) {
        try {
            Frame frame = new Frame();
            centerWindow(frame);
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        } }
//build the frame
        public Frame() {
        model = new DefaultTableModel();
        setBackground(Color.LIGHT_GRAY);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 570, 570);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

//build the table               
        table = new JTable();
        JTable table = new JTable(data, columns);
        table.setBackground(Color.LIGHT_GRAY);
        table.setFont(new Font("Verdana", Font.PLAIN, 14));
        table.setSize(800, 400);
        table.setRowHeight(30);
        table.setForeground(Color.WHITE);
        contentPane.add(table, BorderLayout.CENTER);

        //in this part, the program should change the color of certain cells ... but it does not :-(
        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
//highlight the cells with the value "s"
                    if (value instanceof String) {
                        if (value.equals("s")){
                            System.out.println(row + " " + column);
                            cell.setBackground(Color.BLUE);} else {
                            cell.setBackground(Color.WHITE);}  }  
            return cell;
            }  
        });  
    }  

最佳答案

更改行:

if (value.equals("s")){

if (value.equals("S")){

等于区分大小写。

关于java - 在 JTable 单元格上编辑没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53520637/

相关文章:

java - 将类与按钮和框架相结合 - GUI

java - 我的 JTable 未扩展到其所在面板的整个宽度

java - E(fx)clipce 与 Inno Setup 错误的 jre 文件夹名称

java - JFrame 在第一次实例化后不工作?

java - 静态对象存储在java中的位置

java - 在数据库获取操作期间显示进度条

java - TableCellRenderer 为许多单元格设置颜色,而不仅仅是一个

java - 结果集并不总是显示在 JTable 中

java - 如何从HQL中的 '!'符号转义

java - Winpcap 与 jnetpcap 和 Windows 10