我正在用Yahoo引号填充一个Jtable,该引号每60秒将子类Timertask
安排下载一次。我会根据新值更改单个单元格的背景(例如,如果新值小于旧值,则背景颜色变为绿色)。
我已经尝试过(没有结果)实现添加TableCellRenderer
的propertychangelistener
,如下所示:
private static class TextCellRenderer implements TableCellRenderer {
@Override public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected, boolean hasFocus, final int r, final int c) {
table.setRowHeight(10);
final JTextField text = new JTextField();
text.setOpaque(true);//disable transparence
text.setText(value.toString());
text.addPropertyChangeListener(new PropertyChangeListener(){
@Override
public void propertyChange(PropertyChangeEvent evt) {
JTextField l=(JTextField)evt.getSource();
l.setOpaque(true);
String property=evt.getPropertyName();
double d1=(Double)evt.getOldValue();//old value
if(property=="text"){
double d2=(Double)evt.getNewValue();//new value
//nothing happens - mantain old backround
if (d2==d1){
l.setBackground(l.getBackground());
}
//new value>old value
else if (d2>d1){
l.setBackground(Color.green);
}
//new value<old value
else if (d2<d1){
l.setBackground(Color.red);
}
d1=d2;
}
}
});
return text;
}
}
我需要了解我在做什么错。谢谢
最佳答案
我的建议是首先利用一些好的联机设计。创建一个像CurrentPrevious
这样的简单bean,在其中可以保存当前值和先前值。将CurrentPrevious
的那些实例添加到表模型中。
class CurrentPrevious {
Double current;
Double previous;
// getters an setters
@Override
public String toString() {
return current.toString();
}
}
您只希望在表中查看
current
值,因此可以简单地覆盖类的toString
并返回current
的字符串值。至于渲染,您不需要自定义渲染。我看不到您在此方面做任何特别的事情。创建文本字段作为渲染器似乎毫无意义。任何所需的编辑功能都应使用编辑器组件完成。因此,可以替代表的
prepareRenderer
而不是自定义渲染器。如下所示:@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
Object value = getValueAt(row, col);
if (value instanceof CurrentPrevious) {
CurrentPrevious curPrev = (CurrentPrevious) value;
Double current = curPrev.getCurrent();
Double previous = curPrev.getPrevious();
Color color = getColor(current, previous);
c.setBackground(color);
}
return c;
}
...
private Color getColor(Double current, Double previous) {
Color color;
if (current.equals(previous) || current > previous) {
color = Color.GREEN;
} else {
color = Color.RED;
}
return color;
}
当需要更新该值时,只需将以前的值更改为当前值,然后将当前值设置为新值即可。
DefaultTableModel model = (DefaultTableModel) table.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
CurrentPrevious curPrev = (CurrentPrevious) model.getValueAt(i, 0);
curPrev.setPrevious(curPrev.getCurrent());
Double newCurrent = getRandomDouble();
curPrev.setCurrent(newCurrent);
model.setValueAt(curPrev, i, 0);
}
结果如下所示:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public class ChangeCellColorDemo {
private Random random = new Random();
private JTable table = getTable();
public ChangeCellColorDemo() {
Timer timer = new Timer(2000, new TimerListener());
JFrame frame = new JFrame();
frame.add(new JScrollPane(table));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
timer.start();
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
CurrentPrevious curPrev = (CurrentPrevious) model.getValueAt(i, 0);
curPrev.setPrevious(curPrev.getCurrent());
Double newCurrent = getRandomDouble();
curPrev.setCurrent(newCurrent);
model.setValueAt(curPrev, i, 0);
}
}
}
private JTable getTable() {
JTable table = new JTable(getTableModel()) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
Object value = getValueAt(row, col);
if (value instanceof CurrentPrevious) {
CurrentPrevious curPrev = (CurrentPrevious) value;
Double current = curPrev.getCurrent();
Double previous = curPrev.getPrevious();
Color color = getColor(current, previous);
c.setBackground(color);
}
return c;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
};
return table;
}
private TableModel getTableModel() {
String[] cols = {"Value"};
DefaultTableModel model = new DefaultTableModel(cols, 0);
for (int i = 0; i < 10; i++) {
Object[] row = new Object[1];
Double current = getRandomDouble();
Double previous = getRandomDouble();
row[0] = new CurrentPrevious(current, previous);
model.addRow(row);
}
System.out.println(model.getRowCount());
return model;
}
private Color getColor(Double current, Double previous) {
Color color;
if (current.equals(previous) || current > previous) {
color = Color.GREEN;
} else {
color = Color.RED;
}
return color;
}
private Double getRandomDouble() {
BigDecimal bd = new BigDecimal(random.nextDouble());
bd = bd.setScale(4, RoundingMode.HALF_UP);
return bd.doubleValue();
}
class CurrentPrevious {
Double current;
Double previous;
public CurrentPrevious() {}
public CurrentPrevious(Double current, Double previous) {
this.current = current;
this.previous = previous;
}
public Double getCurrent() {
return current;
}
public Double getPrevious() {
return previous;
}
public void setCurrent(Double current) {
this.current = current;
}
public void setPrevious(Double previous) {
this.previous = previous;
}
@Override
public String toString() {
return current.toString();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ChangeCellColorDemo();
}
});
}
}