jTable细胞背景颜色

在任何人告诉我去谷歌搜索它之前,我已经做到了,到目前为止每一种方法都没有用。

我曾尝试使用渲染器为jTable的单元格着色,但它们没用,因为它们落后于桌面而无法看到。 这是我的代码:

TableCellRenderer Tcr = jTable1.getCellRenderer(x, y); Component c = Tcr.getTableCellRendererComponent(jTable1, jTable1.getValueAt(x, y), false, false, x, y); if(x > 0 && x  1 && y  24){ setBackground(java.awt.Color.red); } } } } 

我没有把它放到渲染器类中因为它滞后,我把它放在一个双重的cicle中,特别是放入第二个cicle中。 我希望它为超过24的单元格着色,就像现在一样,如果我写的话,它不起作用

 c.setBackground(Color.red); 

它完全为桌子着色

编辑

如我所知,我创建了一个描述我的问题的小例子,我不知道是否有一个特定的方式来发布一个可运行的例子,但是下面的代码(在netbeans中)代表完整的程序:

 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package fatturazione; import ObjectModel.Timesheet; import java.awt.Component; import javax.swing.JLabel; import javax.swing.table.TableCellRenderer; /** * * @author xtphere */ public class Example extends javax.swing.JFrame { /** * Creates new form Main */ public Example() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") //  private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); CheckButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } )); jScrollPane1.setViewportView(jTable1); CheckButton.setText("Check the table"); CheckButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { CheckButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(CheckButton)) .addContainerGap(15, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(CheckButton) .addGap(0, 35, Short.MAX_VALUE)) ); pack(); }//  private void CheckButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int x, y, i = 1; for(x = 0; x < jTable1.getRowCount(); x++){ for(y = 0; y  24){ System.out.print("leggi il 25, almeno?"); c.setBackground(java.awt.Color.red); } } } } } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Example().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton CheckButton; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration } 

首先,变量名称不应以大写字符开头。 你的一些变量是正确的,有些则不是。 始终如一!!!

我试图使用渲染器为jTable的单元格着色,但是它们没用,它们落后于桌面并且无法看到。

仅仅因为你不理解这个概念并没有使它变得无用。 问题在于您的代码,而不是渲染器的概念。

您发布的代码毫无意义。 您无法设置单个单元格的颜色。 单元格是渲染器时确定颜色,这就是您需要使用渲染器的原因。

它完全为桌子着色

是的,一旦设置了渲染器的背景,将来所有单元格都将使用该颜色。 在渲染每个单元格之前,您需要将颜色重置为其默认值

背景必须是红色,以防它是一个数字并且它高于24,

然后做一个积极的检查,忘记所有这些负面检查。

使用以上所有建议您可能会有一个渲染器:

 class ColorRenderer extends 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) setBackground( table.getSelectionBackground() ); else { setBackground( table.getBackground() ); try { int number = Integer.parseInt( value.toString() ); if (number > 24) setBackground( Color.RED ); } catch(Exception e) {} } return this; } } 

好的,在查看讨论主题后,我来到这个解决方案……

…不要试图获取渲染的Component本身 – 告诉渲染器以你想要的方式绘制你想要的组件……

 CellRenderer renderer = new DefaultCellRenderer(){ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); JLabel label = (JLabel)c; if (yourAlgorithmToDetectTheProperCell){ //i can't insert your condition from above, it's overkill ^^ label.setBackGround(Color.RED); } return label; } }; table.setCellRenderer(renderer); 

SDF

在您的第一个代码示例中,您将完成一系列检查,并将背景颜色设置为红色。 在所有其他情况下,您应将背景设置为默认背景颜色。 您可以通过table.getBackground();查找此背景颜色table.getBackground();