做成向excel那样的表格,和编辑功能.一定需要自己用jtabel做吗?
请问swing里面有没有如datagrid等电子表格的类,或者是其它简单实现办法?

解决方案 »

  1.   

    哎,刚刚查了些资料.说swing好象没有封装这样的类.
    看来swing还是封装得不够好啊.
      

  2.   

    Swing的表格是可以编辑的,可以通过修改Editable属性进行设置。
    同时编辑控件也是可以任意选择的,可以通过修改Renderer属性进行设置lz还是多学习吧,不要妄下断言
      

  3.   

    JTable除了多表头实现比较麻烦,还没发现什么不尽人意的地方。
      

  4.   

    回复楼上的:
    像Delphi那种都有已经封装好的datagrid.
    我知道JTabel可以实现,而且不难.发个帖子是问问有没有更为简便的方法.
      

  5.   

    那请说清楚你的具体需求。
    一句像Datagrid一样,很抱歉我才疏学浅,Delphi中的控件从没接触过。
    我只看到你主贴里描述需要编辑功能而已另外lz对于封装的概念是什么?
      

  6.   

    建议你用java2d画吧,自己添加响应
      

  7.   

    在编写日历里面有段代码是先设置表格的模式,然后可以选择里面日期的看对你有没有什么帮助,我加了注释
    部分代码如下:
     //设置表格模式为7行7列
            dModel = new AbstractTableModel() { 
                    public int getRowCount() { 
                        return 7; 
                    }
     
                    public int getColumnCount() { 
                        return 7; 
                    }
                    //获取表中单元格的值,为系统当前时间
                    public Object getValueAt(int row, int column) { 
                        if (row == 0) { 
                            //获取表格的标题
                            return getHeader(column); 
                        } 
                        row--; 
                        Calendar now = (Calendar) MyCalendar.this.now.clone();
                        //设置当前日期为一个月第一天
                        now.set(Calendar.DAY_OF_MONTH, 1);
                        //获得这个月的最大天数
                        int dayCount = now.getActualMaximum(Calendar.DAY_OF_MONTH); 
                        //获得当前周的第几天
                        int moreDayCount = now.get(Calendar.DAY_OF_WEEK) - 1; 
                        int index = row * 7 + column; 
                        //计算出当前日期
                        int dayIndex = index - moreDayCount + 1;
                        if (index < moreDayCount || dayIndex > dayCount) { 
                            return null; 
                        } else { 
                            return new Integer(dayIndex); 
                        } 
                    } 
                };
            //建立制定数据的表格    
            dTable = new CalendarTable(dModel, now); 
            dTable.setCellSelectionEnabled(true);//让用户能够同时选择单个单元格或者整个行,同时选择列
            dTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//SINGLE_SELECTION允许一次选择一行
            //设置表格注册接口TableCellRenderer
            dTable.setDefaultRenderer(dTable.getColumnClass(0), new TableCellRenderer() { 
                //当表格处于表现状态时调用 getTableCellRendererComponent   
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
                                                                   boolean hasFocus, int row, int column) { 
                        String text = (value == null) ? "" : value.toString(); 
                        JLabel color = new JLabel(text); 
                        color.setOpaque(true); //使得标签不透明
                        //设置标签颜色
                        if (row == 0) { 
                            color.setForeground(hForeground); 
                            color.setBackground(hBackground); 
                        } else { 
                            if (isSelected) { 
                                color.setForeground(sForeground); 
                                color.setBackground(sBackground); 
                            } else { 
                                color.setForeground(foreground); 
                                color.setBackground(background); 
                            } 
                        }
     
                        return color; 
                    } 
                });
            updateView();               
            //隐藏表格线
            dTable.setShowHorizontalLines(false);
            dTable.setShowVerticalLines(false);          
            add(dTable, BorderLayout.CENTER); 
            add(Box.createRigidArea(new Dimension(0,20)));
        }
     //设置表格标题显示
        public static String getHeader(int index) { 
            switch (index) { 
            case 0: 
                return SUN; 
            case 1: 
                return MON; 
            case 2: 
                return TUE; 
            case 3: 
                return WED; 
            case 4: 
                return THU; 
            case 5: 
                return FRI; 
            case 6: 
                return SAT; 
            default: 
                return null; 
            } 
        }
           public static class CalendarTable extends JTable {        private Calendar now;        public CalendarTable(TableModel model, Calendar now) {
                super(model);
                this.now = now;
            }        public void changeSelection(int row, int column, boolean toggle, boolean extend) {
                super.changeSelection(row, column, toggle, extend);
                if (row == 0) {
                    return;
                }
                //获得表格被点击的日期
                Object obj = getValueAt(row, column);
                if (obj != null) {
                    now.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());
                }
            }    }       
        //更新表格,使得表格处于选择状态
        public void updateView() { 
            dModel.fireTableDataChanged();
            dTable.setRowSelectionInterval(now.get(Calendar.WEEK_OF_MONTH),
                                              now.get(Calendar.WEEK_OF_MONTH));
            dTable.setColumnSelectionInterval(now.get(Calendar.DAY_OF_WEEK) - 1,
                                                 now.get(Calendar.DAY_OF_WEEK) - 1);
        }