rednight

          0x2B|~0x2B,That's not a question,Just do it.
          posts - 32, comments - 14, trackbacks - 0, articles - 0

          groupable header table

          Posted on 2006-11-08 21:05 rednight 閱讀(418) 評論(0)  編輯  收藏
          public class GroupableHeaderExample extends JFrame {

          ? GroupableHeaderExample() {
          ??? super( "Groupable Header Example" );

          ??? DefaultTableModel dm = new DefaultTableModel();
          ??? dm.setDataVector(new Object[][]{
          ????? {"119","foo","bar","ja","ko","zh"},
          ????? {"911","bar","foo","en","fr","pt"}},
          ??? new Object[]{"SNo.","1","2","Native","2","3"});

          ??? JTable table = new JTable( dm ) {
          ????? protected JTableHeader createDefaultTableHeader() {
          ? return new GroupableTableHeader(columnModel);
          ????? }
          ??? };
          ??? TableColumnModel cm = table.getColumnModel();
          ??? ColumnGroup g_name = new ColumnGroup("Name");
          ??? g_name.add(cm.getColumn(1));
          ??? g_name.add(cm.getColumn(2));
          ??? ColumnGroup g_lang = new ColumnGroup("Language");
          ??? g_lang.add(cm.getColumn(3));
          ??? ColumnGroup g_other = new ColumnGroup("Others");
          ??? g_other.add(cm.getColumn(4));
          ??? g_other.add(cm.getColumn(5));
          ??? g_lang.add(g_other);
          ??? GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
          ??? header.addColumnGroup(g_name);
          ??? header.addColumnGroup(g_lang);

          ??? TableCellRenderer renderer =? new DefaultTableCellRenderer() {
          ??????? public Component getTableCellRendererComponent(JTable table, Object value,
          ???????????????????????? boolean isSelected, boolean hasFocus, int row, int column) {
          ????????? JTableHeader header = table.getTableHeader();
          ????????? if (header != null) {
          ??????????? setForeground(header.getForeground());
          ??????????? setBackground(header.getBackground());
          ??????????? setFont(header.getFont());
          ????????? }
          ????????? setHorizontalAlignment(JLabel.CENTER);
          ????????? setText((value == null) ? "" : value.toString());
          ????????? setBorder(UIManager.getBorder("TableHeader.cellBorder"));
          ????????? return this;
          ??????? }
          ????? };

          ??? TableColumnModel model = table.getColumnModel();
          ??? for (int i=0;i<model.getColumnCount();i++) {
          ????? model.getColumn(i).setHeaderRenderer(renderer);
          ??? }
          ??? JScrollPane scroll = new JScrollPane( table );
          ??? getContentPane().add( scroll );
          ??? setSize( 400, 120 );
          ? }

          ? public static void main(String[] args) {
          ??? GroupableHeaderExample frame = new GroupableHeaderExample();
          ??? frame.addWindowListener( new WindowAdapter() {
          ????? public void windowClosing( WindowEvent e ) {
          ? System.exit(0);
          ????? }
          ??? });
          ??? frame.setVisible(true);
          ? }
          }

          =============================================
          public void paint(Graphics g, JComponent c) {
          ??? Rectangle clipBounds = g.getClipBounds();
          ??? if (header.getColumnModel() == null) return;
          //??? ((GroupableTableHeader)header).setColumnMargin();
          ??? int column = 0;
          ??? Dimension size = header.getSize();
          ??? Rectangle cellRect? = new Rectangle(0, 0, size.width, size.height);
          ??? Hashtable h = new Hashtable();
          //??? int columnMargin = header.getColumnModel().getColumnMargin();

          ??? Enumeration enumeration = header.getColumnModel().getColumns();
          ??? while (enumeration.hasMoreElements()) {
          ????? cellRect.height = size.height;
          ????? cellRect.y????? = 0;
          ????? TableColumn aColumn = (TableColumn)enumeration.nextElement();
          ????? Enumeration cGroups = ((GroupableTableHeader)header).getColumnGroups(aColumn);
          ????? if (cGroups != null) {
          ??????? int groupHeight = 0;
          ??????? while (cGroups.hasMoreElements()) {
          ????????? ColumnGroup cGroup = (ColumnGroup)cGroups.nextElement();
          ????????? Rectangle groupRect = (Rectangle)h.get(cGroup);
          ????????? if (groupRect == null) {
          ??????????? groupRect = new Rectangle(cellRect);
          ??????????? Dimension d = cGroup.getSize(header.getTable());
          ??????????? groupRect.width? = d.width;
          ??????????? groupRect.height = d.height;
          ??????????? h.put(cGroup, groupRect);
          ????????? }
          ????????? paintCell(g, groupRect, cGroup);
          ????????? groupHeight += groupRect.height;
          ????????? cellRect.height = size.height - groupHeight;
          ????????? cellRect.y????? = groupHeight;
          ??????? }
          ????? }
          ????? cellRect.width = aColumn.getWidth() ;//+ columnMargin;
          ????? if (cellRect.intersects(clipBounds)) {
          ??????? paintCell(g, cellRect, column);
          ????? }
          ????? cellRect.x += cellRect.width;
          ????? column++;
          ??? }
          ? }



          主站蜘蛛池模板: 彭泽县| 新密市| 韶关市| 隆德县| 哈巴河县| 息烽县| 双峰县| 红河县| 鄂尔多斯市| 长汀县| 景洪市| 若羌县| 临汾市| 乡宁县| 枣阳市| 肇州县| 巨野县| 中牟县| 富民县| 廊坊市| 西贡区| 邮箱| 堆龙德庆县| 孝义市| 融水| 平昌县| 灌阳县| 修水县| 西藏| 新丰县| 洪泽县| 麻栗坡县| 揭东县| 萍乡市| 西吉县| 兴安盟| 洛宁县| 水富县| 阜宁县| 蕲春县| 三台县|