Chinaunix首页 | 论坛 | 博客
  • 博客访问: 381182
  • 博文数量: 61
  • 博客积分: 1546
  • 博客等级: 中尉
  • 技术积分: 708
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-22 20:07
文章分类

全部博文(61)

文章存档

2011年(61)

分类: Java

2011-04-01 16:22:20

有时候需要在一张表格里显示数据或者选择一些内容,如些图所示:
主要用到了JFrame显示窗口,JTable类以及继承了AbstractTableModel的一个类。
 
  1. import java.awt.*;
  2. import java.util.*;

  3. import javax.swing.*;
  4. import javax.swing.table.*;

  5. /**
  6.  * JTable的例子
  7.  */
  8. public class Test {
  9.     
  10.     private JFrame frame = null;

  11.     private JTable table = null;

  12.     private Table_Model model = null;

  13.     private JScrollPane s_pan = null;

  14.     public Test() {
  15.         frame = new JFrame("Test");
  16.         model = new Table_Model(20);
  17.         table = new JTable(model);
  18.         String[] age = { "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" };
  19.         JComboBox com = new JComboBox(age);
  20.         TableColumnModel tcm = table.getColumnModel();
  21.         tcm.getColumn(2).setCellEditor(new DefaultCellEditor(com)); // 设置某列采用JComboBox组件


  22.         model.addRow("宋江", true, "30");
  23.         model.addRow("孙二娘", false, "21");
  24.         model.addRow("武松", true, "24");
  25.         
  26.         s_pan = new JScrollPane(table);

  27.         frame.getContentPane().add(s_pan, BorderLayout.CENTER);
  28.         
  29.         frame.setSize(300, 200);
  30.         frame.setVisible(true);

  31.         //model.addRow(2); // 在某处插入一空行

  32.         //table.updateUI(); // 刷新


  33.     }

  34.     public static void main(String args[]) {
  35.         new Test();
  36.     }

  37. }

  38. class Table_Model extends AbstractTableModel {
  39.     private static final long serialVersionUID = -3094977414157589758L;

  40.     private Vector content = null;

  41.     private String[] title_name = { "姓名", "性别", "年龄" };

  42.     public Table_Model() {
  43.         content = new Vector();
  44.     }

  45.     public Table_Model(int count) {
  46.         content = new Vector(count);
  47.     }

  48.     /**
  49.      * 加入一空行
  50.      * @param row 行号
  51.      */
  52.     public void addRow(int row) {
  53.         Vector v = new Vector(3);
  54.         v.add(0, null);
  55.         v.add(1, null);
  56.         v.add(2, null);
  57.         content.add(row, v);
  58.     }

  59.     /**
  60.      * 加入一行内容
  61.      */
  62.     public void addRow(String name, boolean ***, String age) {
  63.         Vector v = new Vector(3);
  64.         v.add(0, name);
  65.         v.add(1, new Boolean(***)); // JCheckBox是Boolean的默认显示组件,这里仅仅为了看效果,其实用JComboBox显示***更合适

  66.         v.add(2, age); // 本列在前面已经设置成了JComboBox组件,这里随便输入什么字符串都没关系

  67.         content.add(v);
  68.     }

  69.     public void removeRow(int row) {
  70.         content.remove(row);
  71.     }

  72.     public boolean isCellEditable(int rowIndex, int columnIndex) {
  73.         if(rowIndex == 2) {
  74.             return false;
  75.         }
  76.         return true;
  77.     }

  78.     public void setValueAt(Object value, int row, int col) {
  79.         ((Vector) content.get(row)).remove(col);
  80.         ((Vector) content.get(row)).add(col, value);
  81.         this.fireTableCellUpdated(row, col);
  82.     }

  83.     public String getColumnName(int col) {
  84.         return title_name[col];
  85.     }

  86.     public int getColumnCount() {
  87.         return title_name.length;
  88.     }

  89.     public int getRowCount() {
  90.         return content.size();
  91.     }

  92.     public Object getValueAt(int row, int col) {
  93.         return ((Vector) content.get(row)).get(col);
  94.     }

  95.     public Class getColumnClass(int col) {
  96.         return getValueAt(0, col).getClass();
  97.     }

  98. }
阅读(26609) | 评论(1) | 转发(0) |
0

上一篇:雨过天晴

下一篇:java实现进度条

给主人留下些什么吧!~~

java_expert2015-01-28 10:33:25

这其实用finereport做很方便的,连代码也不怎么需要写