简体   繁体   English

使用其他类和方法更改JTable行运行时的颜色

[英]Changing color of a JTable row runtime using another class and methods

Main class: 主班:

public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               new NewMDIApplication().setVisible(true); 
               new TestTable();
            }
        });
    }

 public JTable getMyJTable() {
      return jTable2;
   }

Other class: 其他类:

package javaapplication9;

import java.awt.Color;
import javax.swing.table.DefaultTableModel;

 public class TestTable extends NewMDIApplication {
   public NewMDIApplication obj=new NewMDIApplication();
     public TestTable() {
                final DefaultTableModel model = (DefaultTableModel) obj.getMyJTable().getModel();
                obj.getMyJTable().setModel(model);
                obj.getMyJTable().setDefaultRenderer(obj.getMyJTable().getClass(), new MyCellRenderer());
               }

    public class MyCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
            public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            final java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            Object val = table.getValueAt(row, 1);
            String sval = val.toString();
            if (sval.equalsIgnoreCase("A")) {
                cellComponent.setForeground(Color.black);
                cellComponent.setBackground(Color.red);

            } else {
                cellComponent.setBackground(Color.white);
                cellComponent.setForeground(Color.black);
            }
            if (isSelected) {
                cellComponent.setForeground(table.getSelectionForeground());
                cellComponent.setBackground(table.getSelectionBackground());
            }

            return cellComponent;
        }
    }
  }

This program is running.... but not showing any error. 该程序正在运行...。但未显示任何错误。 But when I edit some data of table through combo box, the color of table is not changing at runtime . 但是当我通过组合框编辑表的某些数据时,表的颜色在运行时不会改变。

What would be a solution to this problem? 有什么办法可以解决这个问题?

There are two aspects to JTable cells: a renderer, and an editor. JTable单元有两个方面:渲染器和编辑器。 The renderer is used to display the cells, the editor is used when a cell is edited. 渲染器用于显示单元格,编辑器用于单元格的编辑。 You have the renderer done, but not the editor. 您已完成渲染器,但尚未完成编辑器。

Supply the JTable with an editor that colors the edited cell the same way the renderer does. 向JTable提供一个编辑器,以与渲染器相同的方式为编辑后的单元格着色。 Eg by way of the JTable.setDefaultEditor(Class<?> columnClass, TableCellEditor editor) method. 例如,通过JTable.setDefaultEditor(Class<?> columnClass, TableCellEditor editor)方法。

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

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