我正在使用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;
}
}
我需要了解我做错了什么。感谢
答案 0 :(得分:1)
我的建议是首先使用一些好的ol oop设计。创建一个简单的bean,如CurrentPrevious
,您可以在其中保存当前值和以前的值。将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();
}
});
}
}