gpt4 book ai didi

java - 根据列值更改 JTable 行的背景颜色

转载 作者:搜寻专家 更新时间:2023-11-01 01:07:57 29 4
gpt4 key购买 nike

嗨,我是 java jtable cellrendered 的新手。我正在寻找一种适用于我的程序的方法,但我没有找到它。这是我的 Jtable

Employee ID   |   Name     |   Status    |   Position
00565651 Roger Active Manager
00565652 Gina Active Crew
00565652 Alex Inactive Crew
00565652 Seph Active Manager

数据来自 ms access 数据库,但我想更改状态列中值为“inactive”的行的背景/前景。我在互联网上找到了很多例子,但在我的程序中不可能全部实现。有人能帮我吗?这是我的模型

String[] columnNames = {"Employee ID","Name", "Status", "Position"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);

这是创建表的方式以及我如何从数据库中获取数据

public MyList(){//my constructor
frame();
loadListFromDB();
}
public void frame(){//
//codes for frame setsize,titles etc...
tblList = new JTable();
tblList.getTableHeader().setPreferredSize(new Dimension(100, 40));
tblList.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 25));
tblList.setAutoCreateRowSorter(true);
tblList.setModel(model);
scrollPane.setViewportView(tblList);
loadListFromDB();

}
public void loadListFromDB(){
String sql = "SELECT emp_id,lname,fname,positional_status from tblEmployee";
try{
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()){
Vector row = new Vector();
for (int i = 1; i <= 4; i++){
row.addElement( rs.getObject(i) );
}
model.addRow(row);
}
}catch(Exception err){
//for error code
}
}

我应该如何以这种方式添加 tableredered?谁能举个简单的例子来改变行的颜色?在此先感谢.. 我的程序停止在这个问题上。

最佳答案

"i want to change the background/foreground of the rows which has a value of "inactive" in status column"

这实际上只是从表/模型中获取值的问题。如果该行的状态为非 Activity 状态,则为该行中的每个单元格设置背景/前景。由于渲染器是为每个单元格渲染的,所以基本上您需要获取 [row][statusColumn] 的值,这将是每一行的状态值。有点像

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int col) {

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);

String status = (String)table.getModel().getValueAt(row, STATUS_COL);
if ("active".equals(status)) {
setBackground(Color.BLACK);
setForeground(Color.WHITE);
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
});

这是一个简单的例子

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class TableRowDemo {

private static final int STATUS_COL = 1;

private static JTable getTable() {
final String[] cols = {"col 1", "status", "col 3"};
final String[][] data = {
{"data", "active", "data"},
{"data", "inactive", "data"},
{"data", "inactive", "data"},
{"data", "active", "data"}
};
DefaultTableModel model = new DefaultTableModel(data, cols);
return new JTable(model) {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(350, 150);
}
};
}

private static JTable getNewRenderedTable(final JTable table) {
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int col) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
String status = (String)table.getModel().getValueAt(row, STATUS_COL);
if ("active".equals(status)) {
setBackground(Color.BLACK);
setForeground(Color.WHITE);
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
});
return table;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JOptionPane.showMessageDialog(null, new JScrollPane(getNewRenderedTable(getTable())));
}
});
}
}

另一种选择是@Override prepareRenderer 表。它会给你相同的结果。

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class TableRowDemo {

private static final int STATUS_COL = 1;

private static JTable getTable() {
final String[] cols = {"col 1", "status", "col 3"};
final String[][] data = {
{"data", "active", "data"},
{"data", "inactive", "data"},
{"data", "inactive", "data"},
{"data", "active", "data"}
};
DefaultTableModel model = new DefaultTableModel(data, cols);
return new JTable(model) {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(350, 150);
}
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
String status = (String)getValueAt(row, STATUS_COL);
if ("active".equals(status)) {
c.setBackground(Color.BLACK);
c.setForeground(Color.WHITE);
} else {
c.setBackground(super.getBackground());
c.setForeground(super.getForeground());
}
return c;
}
};
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JOptionPane.showMessageDialog(null, new JScrollPane(getTable()));
}
});
}
}

关于java - 根据列值更改 JTable 行的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24848314/

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