【发布时间】:2015-11-29 02:41:58
【问题描述】:
我正在尝试使用渲染器为 jTable 的单元格着色,但它们工作不正常,因为它们滞后于表格并使其无法看到。这是我的代码:
TableCellRenderer Tcr = jTable1.getCellRenderer(x, y);
Component c = Tcr.getTableCellRendererComponent(jTable1, jTable1.getValueAt(x, y), false, false, x, y);
if(x > 0 && x < (jTable1.getRowCount()-1) && y > 1 && y < (jTable1.getColumnCount()-1)){
if(!jTable1.getValueAt(x, y).equals(null) && !jTable1.getValueAt(x, y).equals("F") && !jTable1.getValueAt(x, y).equals(" ")){
if(!jTable1.getValueAt(x, y).toString().contains("/P") && !jTable1.getValueAt(x, y).toString().equals("P")){
if(Double.parseDouble(jTable1.getValueAt(x, y).toString()) > 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
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();
}// </editor-fold>
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 < jTable1.getColumnCount(); y++){
TableCellRenderer Tcr = jTable1.getCellRenderer(x, y);
Component c = Tcr.getTableCellRendererComponent(jTable1, jTable1.getValueAt(x, y), false, false, x, y);
if(jTable1.getValueAt(x, y) == null)
{
jTable1.setValueAt("P", x, y);
}
if(jTable1.getValueAt(x, y) != null && !jTable1.getValueAt(x, y).equals("F") && !jTable1.getValueAt(x, y).equals(" ")){
System.out.print(jTable1.getValueAt(x, y)+"\n");
if(!jTable1.getValueAt(x, y).toString().contains("/P") && !jTable1.getValueAt(x, y).toString().equals("P")){
System.out.print("prima del maggiore di 24");
if(Double.parseDouble(jTable1.getValueAt(x, y).toString()) > 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 */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* 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);
}
//</editor-fold>
//</editor-fold>
/* 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
}
【问题讨论】:
-
考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将导致更少的混乱和更好的响应
-
使用和扩展 DefaultTableCellRenderer 不会滞后。但是,它必须正确使用:单元格渲染器应返回可用于所有单元格的单个相同组件,并且工作量应最少。也许一个 TableModel 更像是为红色保存一个布尔值。
-
首先查看Concepts: Editors and Renderers 和Using Custom Renderers。你提供的代码不可能工作,因为你得到的组件实际上并没有附加到任何东西上,当渲染时,表格只是使用渲染器将输出“橡皮图章”到表格上,实际上没有生命桌子上的组件(编辑器除外,当处于活动状态时)
-
我已经看过自定义渲染器的东西了,因为很多人在我读过的其他问题中建议使用它,但是由于 CustomRenderer 似乎根本不起作用,我会使用 defaultrenderers 重试
标签: java swing jtable background-color tablecellrenderer