首页 > 代码库 > Swing JTable 详解
Swing JTable 详解
改变列头
flightTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
MButton btn = new MButton();
btn.setSkinColor(getSkinColor());
if (!isEnabledColorShinUI()) {
btn.setBorderPainted(true);
btn.setEnabledColorShinUI(isEnabledColorShinUI());
}
btn.setAlpha(0.9f);
btn.setText(value.toString());
// 去除背景颜色 改为透明
((JComponent) table.getTableHeader()).setOpaque(true);
((JComponent) table.getTableHeader()).setBackground(MUIManager.getColor(getSkinColor(), 0.2f));
return btn;
}
});
设置行颜色
DefaultTableCellRenderer cellRender = new DefaultTableCellRenderer() {
private static final long serialVersionUID = 5419938797587921762L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
// 浅绿:#5dd24d,浅蓝:#49a1e1,中紫:#a733b5 ,浅黄:#dfd74c,天蓝:#3880da
// 设置颜色
FlightDispatch flight = flightList.get(row);
int fdType = flight.FDTYPE;
// 状态为已到达的过站航班(进港并出港的航班,飞机号为同一飞机号):LightGreen(浅绿)
if (fdType == 3) {
setBackground(Color.decode("#5dd24d"));
}
// 状态为已到达的单进航班:MediumOrchid(中紫)
else if (fdType == 2) {
setBackground(Color.decode("#49a1e1"));
}
// 单出航班:White(白)
else if (fdType == 1) {
setBackground(Color.WHITE);
}
// 前方未起飞的航班:LightYellow(浅黄)
// 加油开始或补油开始或抽油开始:绿色
// 加油员接收任务到到达机位的这段时间(未点击到达机位动作之前,点击后就变绿色):黄色
// 异常航班(如状态为:延误、备降)红色
// 已取消的航班 :天蓝色
// 状态为前方起飞未到达本场的航班:LightBlue(浅蓝)
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
};
cellRender.setHorizontalAlignment(SwingConstants.CENTER);
flightTable.setDefaultRenderer(Object.class, cellRender);
// flightTable.setSelectionBackground(Color.RED);