「我想改變它的值爲行的背景/前景‘’在狀態欄」
不活躍這真的只是一個正從表/模型中的價值的問題。如果該行的狀態爲非活動狀態,則爲該行中的每個單元格設置背景/前景。由於渲染器是針對每個單元渲染的,因此基本上需要獲取[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;
}
});
的東西在這裏是一個簡單的例子
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()));
}
});
}
}
的[如何設置JTable中列和行顏色?(
可能重複http://stackoverflow.com/questions/3548986/how-do-i-set-the-jtable-column-and -row-color) – DavidPostill
但是數據剛剛從一開始就被初始化了,我的數據來自數據庫,我不知道如何把我的數據放在字符串[] []的基礎上。 –
你的問題不在於把數據放在表格中,而是改變行的顏色。您應該查看示例中的TableCellRenderer部分並將渲染添加到您的表中。 – DavidPostill