- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是java初学者,在使用java表custemrenderer时遇到困难..我有名为“info”的数据库,其中包含3列调用“pvalue”,“pbankmoney”,“minvalue”..我制作java应用程序以在java表上查看它们..但我想让行背景颜色仅在“bankmoney”<“minvalue”(bankmoney低于minvalue)时不同..所以我使用java custemrenderer..但我有一些问题..firstone 正在使用表 CustomTableCellRenderer 我如何更改行颜色..我解释一下我的意思..当我说 jTable1.getColumnModel().getColumn(0).setCellRenderer(new CustomTableCellRenderer());
CustomTableCellRenderer 类仅在特定行收到第 0 列值,那么我可以比较那里 2 列之间的差异吗?但我认为有一种方法可以使用自定义渲染来更改前两列的颜色,而不是在单个单元格中。
然后我使用另一个 boolean 变量来实现我的目标..我在一开始就设置了一个名为“tabledif”的变量..并检查差异并将其更改为真或假,然后渲染类检查它并更改列颜色..是的,这不起作用..我发现该变量根据与minvalue和bankmoney值的差异正确更改..但是当我请求我的“rowidenty”类的 boolean 值时,每次它是我启动的值时它都会给出错误..为什么这个发生 boolean 问题?重要
1)我想使用 java custemrender 根据两个值之间的差异(从数据库获取)更改前 2 列颜色。
2)我想知道为什么 boolean 值总是给出 false,这是初始值,即使我已经更改为 true ??????
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tablerowcustemrender;
import java.awt.Color;
import java.awt.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/**
*
* @author Madhawa
*/
public class rowidenty extends javax.swing.JFrame {
public boolean tabledif=false;
/**
* Creates new form rowidenty
*/
public rowidenty() {
initComponents();
jTable1.setShowGrid(true);
jTable1.setShowVerticalLines(true);
//jTable1.setGridColor(Color.gray);
}
public void search()
{
try{
DefaultTableModel dm = (DefaultTableModel) jTable1.getModel();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:info");
java.sql.Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from Table1");
while(rs.next()){
String tablename=rs.getString("pname");
int tablemin= rs.getInt("minvalue");
int tablemoney=rs.getInt("pbankmoney");
if(tablemoney < tablemin)
{
tabledif=true;
}
else
{
tabledif=false;
}
System.out.println(tablename+" "+tablemoney+" "+tablemin);
Vector<Object> data = new Vector<>();
data.add(tablename);
data.add(tablemoney);
data.add(tablemin);
dm.addRow(data);
jTable1.getColumnModel().getColumn(0).setCellRenderer(new CustomTableCellRenderer());
jTable1.getColumnModel().getColumn(1).setCellRenderer(new CustomTableCellRenderer());
}
}
catch(Exception e){
System.out.println(e);
}
}
///////////////////////////////////////////////////////////////////
/**
* 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() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("action");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTable1.setBackground(new java.awt.Color(0, 204, 204));
jTable1.setBorder(javax.swing.BorderFactory.createMatteBorder(5, 5, 5, 5, new java.awt.Color(255, 0, 102)));
jTable1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"name", "money", "value"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jTable1.setGridColor(new java.awt.Color(255, 204, 0));
jTable1.setRowHeight(35);
jTable1.setRowMargin(10);
jTable1.setSelectionBackground(new java.awt.Color(255, 51, 153));
jScrollPane1.setViewportView(jTable1);
jButton2.setText("hide");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("show");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("bool");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("jButton5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(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.DEFAULT_SIZE, 504, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addComponent(jButton2)
.addGap(27, 27, 27)
.addComponent(jButton3)
.addGap(116, 116, 116)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton5)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(45, 45, 45)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton4)
.addComponent(jButton5))
.addGap(31, 31, 31))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
search();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTable1.getColumnModel().getColumn(2).setMinWidth(0);
jTable1.getColumnModel().getColumn(2).setMaxWidth(0);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTable1.getColumnModel().getColumn(2).setMinWidth(150);
jTable1.getColumnModel().getColumn(2).setMaxWidth(150);
jTable1.getColumnModel().getColumn(2).setMaxWidth(150);
jTable1.repaint();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
tabledif=true;
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("hey "+tabledif );
}
/**
* @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(rowidenty.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(rowidenty.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(rowidenty.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(rowidenty.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new rowidenty().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
public class CustomTableCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
rowidenty rowidenty1=new rowidenty();
if (rowidenty1.tabledif) {
cell.setBackground(Color.cyan);
} else {
cell.setBackground(Color.lightGray);
}
System.out.println(obj+" renderer "+column+" bool "+rowidenty1.tabledif);
return cell;
}
}
}
最佳答案
在单元格渲染中创建一个全新的 rowidenty
实例基本上会断开您与表中数据的连接...
public class CustomTableCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
// This is your problem....
rowidenty rowidenty1=new rowidenty();
if (rowidenty1.tabledif) {
cell.setBackground(Color.cyan);
} else {
cell.setBackground(Color.lightGray);
}
System.out.println(obj+" renderer "+column+" bool "+rowidenty1.tabledif);
return cell;
}
}
相反,您需要使用 obj
值或向表询问有关该行的更多信息
已更新...
请记住,每一行都需要单独检查,因此尝试维护某种标志是没有意义的,除非每一行都有一个标志。
相反,您需要在每次调用 CustomTableCellRenderer
时提取该行所需的值,并根据您的需要更新背景...
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
TableModel model = table.getModel();
int money = (int) model.getValueAt(row, 1);
int min = (int)model.getValueAt(row, 2);
if (money < min) {
cell.setBackground(Color.cyan);
} else {
cell.setBackground(Color.lightGray);
}
return cell;
}
}
关于java jtable custemrender rowcolor相对于2个值变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20340654/
我是java初学者,在使用java表custemrenderer时遇到困难..我有名为“info”的数据库,其中包含3列调用“pvalue”,“pbankmoney”,“minvalue”..我制作j
我是一名优秀的程序员,十分优秀!