我有一个Jtable,已经从数据库查询出来数据, 然后表格里也有 数据了 现在 对某1列操作 (比如数值大于20的背景色设置成红色,字体设置成白色)后要
改变这一列 中的数据 的 文字颜色和背景颜色,这个颜色已经能够获取不知道 改如何 去 刷新 这一列数据 然后 改变我想要的颜色?谢谢
改变这一列 中的数据 的 文字颜色和背景颜色,这个颜色已经能够获取不知道 改如何 去 刷新 这一列数据 然后 改变我想要的颜色?谢谢
public void setValueAt(Object value, int row, int col) {import java.awt.Dimension;
import java.awt.GridLayout;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;/**
* TableDemo is just like SimpleTableDemo, except that it
* uses a custom TableModel.
*/
public class TableDemo extends JPanel {
private boolean DEBUG = true; public TableDemo() {
super(new GridLayout(1,0)); JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setRowSelectionAllowed(true);
table.setCellSelectionEnabled(true); //Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel.
add(scrollPane);
} class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)},
{"Isaac", "Rabinovitch",
"Nitpicking", new Integer(1000), new Boolean(false)}
}; public int getColumnCount() {
return columnNames.length;
} public int getRowCount() {
return data.length;
} public String getColumnName(int col) {
return columnNames[col];
} public Object getValueAt(int row, int col) {
//根据不同值设置单元格背景颜色
if(col == 3){
if((Integer)data[row][col] >= 10)
return "<html><font color=red>"+data[row][col]+"</font></html>";
else return "<html><font color=blue>"+data[row][col]+"</font></html>";
}else {
return data[row][col];
}
} /*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
} /*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
return true;
} /*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
} data[row][col] = value;
fireTableCellUpdated(row, col); if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
} private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount(); for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
} /**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane); //Display the window.
frame.pack();
frame.setVisible(true);
} public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setText((value == null) ? "" : value.toString());
if (value != null && Double.parseDouble(value.toString()) > 0.2) {
if (isSelected) {
setForeground(Color.black);
setBackground(Color.green);
} else {
setForeground(Color.white);
setBackground(Color.red);
}
} else {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
}
return this;
}
}然后MyRenderer myRenderer = new MyRenderer();
table.getColumnModel().getColumn(XX).setCellRenderer(myRenderer);
TableModel中的setValueAt改完应该自动刷新的
public void setValueAt(Object value, int row, int col) {
}你自己实现 setValueAt(Object value, int row, int col)方法
public class TestTable extends JPanel { public TestTable() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setRowSelectionAllowed(true);
table.setCellSelectionEnabled(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
} class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)},
{"Isaac", "Rabinovitch",
"Nitpicking", new Integer(1000), new Boolean(false)}
}; public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
//根据不同值设置单元格背景颜色
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return true;
} public void setValueAt(Object value, int row, int col) {
if(col == 3){
System.out.println("value is :"+value);
value = "<html><font color=red>"+value+"</font></html>";
}
data[row][col] = value;
fireTableCellUpdated(row, col);
}
} private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane.
TestTable newContentPane = new TestTable();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane); //Display the window.
frame.pack();
frame.setVisible(true);
} public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
然后用红字体显示
我觉得得用
class MyTableActionListener implements TableModelListener
{
@Override
public void tableChanged(TableModelEvent arg0)
{}
}
应该是得添加这个监听