北大青鸟光谷校区

北大青鸟光谷校区

  • 北大青鸟鲁广校区
  • 北大青鸟光谷校区
  • 北大青鸟宏鹏IT校区
  • 荆州青鸟之家
  • 襄阳青鸟之家

17740513250

百日千才

武汉北大青鸟学子经验分享-java中JTABLE操作

2013-02-22 18:51:17来源:武汉北大青鸟光谷校区作者:北大青鸟宏鹏光谷校区

     武汉北大青鸟学子经验分享-java中JTABLE操作

        学习java有一段时间了,也自认算得上的个小高手,然而在碰到JTABLE操作的时候,还是有点望而生畏。这次在项目中,碰到对CheckBox单元格进行背景色渲染,着实让我煞费了一番心思。以下是解决方法,与需要的同学共享。

  [java]

  for (int i = 0; i < table.getColumnCount(); i++) {

  // 第十二列是预警标志位,用CheckBox控件显示

  if (i == 11) {

  table.getColumn(table.getModel()。getColumnName(i))。setCellRenderer(

  new TableCellRenderer() {

  private JCheckBox checkBox = new JCheckBox();

  @Override

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

  if (value != null) {

  checkBox.setSelected(Boolean.valueOf(value.toString()));

  checkBox.setHorizontalAlignment(JCheckBox.CENTER);

  if (isSelected) {

  // 默认选中颜色

  checkBox.setBackground(new Color(10, 36, 106));

  } else {

  String warn_Flag = (String) table.getModel()。getValueAt(row, 13);

  if ("1".equals(warn_Flag)) {

  checkBox.setBackground(Color.RED);

  } else {

  checkBox.setBackground(Color.white);

  }

  }

  }

  return checkBox;

  }

  });

  } else {

  table.getColumn(table.getModel()。getColumnName(i))。setCellRenderer(

  new DefaultTableCellRenderer() {

  @Override

  public Component getTableCellRendererComponent(JTable table,

  Object value, boolean isSelected, boolean hasFocus,

  int row, int column) {

  String warn_Flag = (String) table.getModel()。getValueAt(row, 13);

  if ("1".equals(warn_Flag)) {

  setBackground(Color.RED);

  } else {

  setBackground(Color.white);

  }

  return super.getTableCellRendererComponent(table, value,

  isSelected, hasFocus, row, column);

  });

  <span style="white-space:pre">  </span>}}

        还有不足之处希望能够得到指教。

关闭

只为了方便您就学 北大青鸟光谷校区 北大青鸟光谷校区

武汉市江夏区庙山街道华泰街16号(北大青鸟综合大楼)

Copyright (c) 2006-2024 武汉宏鹏教育咨询有限公司 版权所有 All Rights Reserved.