有没有像excel那样的电子表格swt控件吗?

解决方案 »

  1.   

    /** 
     * Allows table columns to be resized not only using the header but from any 
     * rows. Based on the BasicTableHeaderUI.MouseInputHandler code. 
     * 
     * @author Santhosh Kumar T - [email protected] 
     */ 
    public class TableColumnResizer extends MouseInputAdapter{ 
        public static Cursor resizeCursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR); 
     
        private int mouseXOffset; 
        private Cursor otherCursor = resizeCursor; 
     
        private JTable table; 
     
        public TableColumnResizer(JTable table){ 
            this.table = table; 
            table.addMouseListener(this); 
            table.addMouseMotionListener(this); 
        } 
     
        private boolean canResize(TableColumn column){ 
            return column != null 
                    && table.getTableHeader().getResizingAllowed() 
                    && column.getResizable(); 
        } 
     
        private TableColumn getResizingColumn(Point p){ 
            return getResizingColumn(p, table.columnAtPoint(p)); 
        } 
     
        private TableColumn getResizingColumn(Point p, int column){ 
            if(column == -1){ 
                return null; 
            } 
            int row = table.rowAtPoint(p); 
            if(row==-1) 
                return null; 
            Rectangle r = table.getCellRect(row, column, true); 
            r.grow( -3, 0); 
            if(r.contains(p)) 
                return null; 
     
            int midPoint = r.x + r.width / 2; 
            int columnIndex; 
            if(table.getTableHeader().getComponentOrientation().isLeftToRight()) 
                columnIndex = (p.x < midPoint) ? column - 1 : column; 
            else 
                columnIndex = (p.x < midPoint) ? column : column - 1; 
     
            if(columnIndex == -1) 
                return null; 
            return table.getTableHeader().getColumnModel().getColumn(columnIndex); 
        } 
     
        public void mousePressed(MouseEvent e){ 
            table.getTableHeader().setDraggedColumn(null); 
            table.getTableHeader().setResizingColumn(null); 
            table.getTableHeader().setDraggedDistance(0); 
     
            Point p = e.getPoint(); 
     
            // First find which header cell was hit 
            int index = table.columnAtPoint(p); 
            if(index==-1) 
                return; 
     
            // The last 3 pixels + 3 pixels of next column are for resizing 
            TableColumn resizingColumn = getResizingColumn(p, index); 
            if(!canResize(resizingColumn)) 
                return; 
     
            table.getTableHeader().setResizingColumn(resizingColumn); 
            if(table.getTableHeader().getComponentOrientation().isLeftToRight()) 
                mouseXOffset = p.x - resizingColumn.getWidth(); 
            else 
                mouseXOffset = p.x + resizingColumn.getWidth(); 
        } 
     
        private void swapCursor(){ 
            Cursor tmp = table.getCursor(); 
            table.setCursor(otherCursor); 
            otherCursor = tmp; 
        } 
     
        public void mouseMoved(MouseEvent e){ 
            if(canResize(getResizingColumn(e.getPoint())) 
               != (table.getCursor() == resizeCursor)){ 
                swapCursor(); 
            } 
        } 
     
        public void mouseDragged(MouseEvent e){ 
            int mouseX = e.getX(); 
     
            TableColumn resizingColumn = table.getTableHeader().getResizingColumn(); 
     
            boolean headerLeftToRight = 
                    table.getTableHeader().getComponentOrientation().isLeftToRight(); 
     
            if(resizingColumn != null){ 
                int oldWidth = resizingColumn.getWidth(); 
                int newWidth; 
                if(headerLeftToRight){ 
                    newWidth = mouseX - mouseXOffset; 
                } else{ 
                    newWidth = mouseXOffset - mouseX; 
                } 
                resizingColumn.setWidth(newWidth); 
     
                Container container; 
                if((table.getTableHeader().getParent() == null) 
                   || ((container = table.getTableHeader().getParent().getParent()) == null) 
                                    || !(container instanceof JScrollPane)){ 
                    return; 
                } 
     
                if(!container.getComponentOrientation().isLeftToRight() 
                   && !headerLeftToRight){ 
                    if(table != null){ 
                        JViewport viewport = ((JScrollPane)container).getViewport(); 
                        int viewportWidth = viewport.getWidth(); 
                        int diff = newWidth - oldWidth; 
                        int newHeaderWidth = table.getWidth() + diff; 
     
                        /* Resize a table */ 
                        Dimension tableSize = table.getSize(); 
                        tableSize.width += diff; 
                        table.setSize(tableSize); 
     
                        /* 
                         * If this table is in AUTO_RESIZE_OFF mode and has a horizontal 
                         * scrollbar, we need to update a view's position. 
                         */ 
                        if((newHeaderWidth >= viewportWidth) 
                           && (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)){ 
                            Point p = viewport.getViewPosition(); 
                            p.x = 
                                    Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff)); 
                            viewport.setViewPosition(p); 
     
                            /* Update the original X offset value. */ 
                            mouseXOffset += diff; 
                        } 
                    } 
                } 
            } 
        } 
     
        public void mouseReleased(MouseEvent e){ 
            table.getTableHeader().setResizingColumn(null); 
            table.getTableHeader().setDraggedColumn(null); 
        } 
    }
      

  2.   

    SWT 可以插入 ActiveX 控件, 你可以试试直接使用 Excel
      

  3.   

    http://www.evget.com/view/article/viewArticle.asp?article=549
      

  4.   

    谢谢各位的热心回复,用ActiveX 可能不太好吧,那不能跨平台阿,我需要的是要个能够合并和拆分单元格的swt控件。
      

  5.   

    虽然说不一定真的需要跨平台,但对java来说垮平台是它的一大优势,也是一大卖点。而且客户端是要装excel吧。
      

  6.   

    http://www.finereport.com不过是Swing写的,不是SWT,好像在SWT当中也可以用Swing的控件
      

  7.   

    SWT 是可以载入 Swing 组件,如果你只为了实现并不确定需要的功能而浪费那么多精力这笔帐不划算的