gpt4 book ai didi

java - jTable 单元格背景颜色

转载 作者:行者123 更新时间:2023-12-01 18:11:53 24 4
gpt4 key购买 nike

我正在尝试使用渲染器为 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);

}
}
}
}

我没有将它放入渲染器类中,因为它滞后,我将其放入 double for 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
}

最佳答案

首先,变量名称不应以大写字符开头。您的某些变量是正确的,其他变量则不正确。保持一致!!!

I've tried to color a cell of a jTable using the renderers, but they are useless they lag the table and make it impossible to see.

仅仅因为您不理解这个概念并不意味着它毫无用处。问题在于您的代码,而不是渲染器的概念。

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

it colors the table completely

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

the background must be red just in case it's a number AND it's higher than 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;
}
}

关于java - jTable 单元格背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com