该程序与患者及其生命体征有关。如果患者的生命体征异常(超出指定范围),我需要用红色突出显示该列。我写了下面提到的isNormal
函数。但是我不确定如何使用此功能以红色突出显示该列。有人可以建议一种方法吗?
private void refresh()
{
int rowCount = vitalSignTable.getRowCount();
int i;
for (i= rowCount-1; i>=0; i--)
{
((DefaultTableModel)vitalSignTable.getModel()).removeRow(i);
}
for(vitalSigns vs :vitalSign)
{
Object[] vital_row = new Object[6];
vital_row [0] = vs.getRespRate();
vital_row [1] = vs.getHeartRate();
vital_row [2] =vs.getBloodPressure();
vital_row [3] = vs.getWeightPound();
vital_row[4] = vs.getWeightKilos();
vital_row[5]= vs.getTimeStamp();
((DefaultTableModel)vitalSignTable.getModel()).addRow(vital_row);
}
}
private String isNormal( vitalSigns vitalSign) {
String isNormal = "Normal";
int respirationRate = vitalSign.getRespRate();
int heartRate = vitalSign.getHeartRate();
double bloodPressure = vitalSign.getBloodPressure();
int weightPound = vitalSign.getWeightPound();
int weightKilos = vitalSign.getWeightKilos();
{
if ((respirationRate < 20 || respirationRate > 30) /*Respiration Rate*/
|| (heartRate < 80 || heartRate > 130) /*Heart Rate*/
|| (bloodPressure < 80 || bloodPressure > 110) /*Blood Pressure*/
|| (weightPound <80 || weightPound > 160)
|| (weightKilos <40 || weightKilos > 80))
{
isNormal = "Abnormal";
}
}
return isNormal;
}
答案 0 :(得分:0)
这里是一个小示例,说明如何为表格中的单元格上色。为此,您需要实现自己的TableCellRenderer
并将其设置为一列。有关更多信息,请参阅有关JTable的文章。
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
/**
* <code>TableRendererTest</code>.
*/
public class TableColumnsTest implements Runnable {
@Override
public void run() {
JFrame frm = new JFrame("Renderer test");
DefaultTableModel model = new DefaultTableModel(20, 20);
for (int i = 0; i < 20; i++) {
model.setValueAt("Test " + i, i, 3);
}
JTable table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroller = new JScrollPane(table);
frm.add(scroller);
// set renderer for the column 3
table.getColumnModel().getColumn(3).setCellRenderer(
new ColoredRenderer(new DefaultTableCellRenderer(), Color.RED, Color.WHITE));
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new TableColumnsTest());
}
private static class ColoredRenderer implements TableCellRenderer {
private final TableCellRenderer delegate;
private final Color differentBackground;
private final Color differentForeground;
public ColoredRenderer(TableCellRenderer delegate, Color differentBackground, Color differentForeground) {
this.delegate = delegate;
this.differentBackground = differentBackground;
this.differentForeground = differentForeground;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component result = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// probably check for null values and whether the cell is selected (isSelected).
result.setBackground(differentBackground);
result.setForeground(differentForeground);
return result;
}
}
}