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 閱讀(416) 評論(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++;
          ??? }
          ? }



          主站蜘蛛池模板: 竹北市| 布尔津县| 翁源县| 定州市| 天柱县| 惠东县| 苏尼特左旗| 定边县| 务川| 保山市| 浦江县| 淳化县| 香河县| 吴忠市| 呼图壁县| 百色市| 图片| 宜良县| 通化县| 伊宁县| 高要市| 彭阳县| 扎赉特旗| 绥德县| 元氏县| 乐陵市| 横山县| 伊金霍洛旗| 阿坝县| 五原县| 龙南县| 慈利县| 惠来县| 德安县| 苍山县| 江门市| 朝阳市| 满城县| 清水县| 琼海市| 鱼台县|