【发布时间】:2020-04-29 06:59:15
【问题描述】:
嗨,我已将 Jbutton RGB 背景颜色或前景色保存到数据库中,之后我想在 JTable 单元格中显示背景颜色。当程序结果未显示时,我遇到了问题,请指导我,我对 JTable 渲染不了解更多。以下在 JTable 中不根据数据库值绘制颜色
Java代码
/*
* 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 javaapplication7;
import java.awt.Color;
import java.awt.Component;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
/**
*
* @author ultronhouse11
*/
public class demo extends javax.swing.JFrame {
/**
* Creates new form demo
*/
public String var1;
public String var2;
public demo() {
initComponents();
showData();
// table.getColumnModel().getColumn(0).setCellRenderer(new SimpleCellRenderer());
getContentPane().setLayout(new java.awt.BorderLayout(5, 0));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
// a.setLayout(new MigLayout("fill,hidemode 5", "[80px][fill, grow]", ""));
}
public void showData() {
try {
Connection con = connectionDB.conDb();
String sql = "select btn_color,text_color from menu_cat where visible='visible'";
java.sql.PreparedStatement pst = con.prepareStatement(sql);
DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(
new Object[][]{},
new Object[]{"Button Color","Text Color"});
ResultSet rs = pst.executeQuery();
while (rs.next()) {
String m1 = rs.getString("btn_color");
String m2 = rs.getString("text_color");
dm.addRow(new Object[]{m1, m2});
}
// int cr = Integer.parseInt(rs.getString(4));
table.setModel(dm);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setCellRenderer(new SimpleCellRenderer());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 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() {
b = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
b.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
table.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"
}
));
table.setRowHeight(25);
table.setRowMargin(5);
jScrollPane1.setViewportView(table);
javax.swing.GroupLayout bLayout = new javax.swing.GroupLayout(b);
b.setLayout(bLayout);
bLayout.setHorizontalGroup(
bLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1042, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
bLayout.setVerticalGroup(
bLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 520, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(29, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(b, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(631, 631, 631))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(b, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
/**
* @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(demo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(demo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(demo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(demo.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 demo().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel b;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable table;
// End of variables declaration
}
SimpleCellRenderer.java
class SimpleCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
column);
// ((JLabel) cell).setIcon((Icon) value);
// ((JLabel) cell).setText("");
// ((JLabel) cell).setHorizontalAlignment(JLabel.CENTER);
try {
// int cr = Integer.parseInt();
// int ct = Integer.parseInt();
cell.setBackground(Color.red); // database value here color
// cell.setBackground(new Color(ct,true));
} catch (Exception e) {
e.printStackTrace();
}
return cell;
}
public void setColor(int cr){
}
}
【问题讨论】:
-
(1-) 此问题与:stackoverflow.com/questions/61479526/… 重复,现已删除。 OP 没有听取该帖子中的任何建议。发布的代码不是minimal reproducible example。问题是关于渲染。数据来自哪里无关紧要。因此,应删除任何数据库代码。首先学习如何使用带有硬编码数据的渲染器。然后你担心数据库。
-
这里是一个“MRE”的例子:stackoverflow.com/questions/1539329/…。 // 此处的数据库值颜色 - 渲染器从模型中获取数据,而不是从数据库中获取数据。你的“价值”变量就是数据!
-
告诉我如何从数据库中获取 RGB 值并设置单元格背景 JTable。
-
@camickr 我该怎么做才能指导我一点点或发布答案。
-
我确实指导过你。你没有说明你对任何建议的不理解。这是另一个建议。阅读 How to Use Tables 上的 Swing 教程中的部分,它包含一个工作示例,展示了如何为 Color 对象创建渲染器。
标签: java swing jtable swingworker jtableheader