⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basictableheaderui.java

📁 JAVA 所有包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		aColumn = cm.getColumn(column);		columnWidth = aColumn.getWidth();		cellRect.width = columnWidth;		if (aColumn != draggedColumn) {		    paintCell(g, cellRect, column);		}                cellRect.x += columnWidth;	    }	}         // Paint the dragged column if we are dragging.         if (draggedColumn != null) {             int draggedColumnIndex = viewIndexForColumn(draggedColumn); 	    Rectangle draggedCellRect = header.getHeaderRect(draggedColumnIndex);                         // Draw a gray well in place of the moving column.             g.setColor(header.getParent().getBackground());            g.fillRect(draggedCellRect.x, draggedCellRect.y,                               draggedCellRect.width, draggedCellRect.height);            draggedCellRect.x += header.getDraggedDistance();	    // Fill the background. 	    g.setColor(header.getBackground());	    g.fillRect(draggedCellRect.x, draggedCellRect.y,		       draggedCellRect.width, draggedCellRect.height);             paintCell(g, draggedCellRect, draggedColumnIndex);        }	// Remove all components in the rendererPane. 	rendererPane.removeAll();     }    private Component getHeaderRenderer(int columnIndex) {         TableColumn aColumn = header.getColumnModel().getColumn(columnIndex); 	TableCellRenderer renderer = aColumn.getHeaderRenderer();         if (renderer == null) { 	    renderer = header.getDefaultRenderer(); 	}                boolean hasFocus = !header.isPaintingForPrint()                           && (columnIndex == getSelectedColumnIndex())                           && header.hasFocus();        return renderer.getTableCellRendererComponent(header.getTable(), 						aColumn.getHeaderValue(),                                                false, hasFocus,                                                 -1, columnIndex);    }    private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {        Component component = getHeaderRenderer(columnIndex);         rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y,                            cellRect.width, cellRect.height, true);    }    private int viewIndexForColumn(TableColumn aColumn) {        TableColumnModel cm = header.getColumnModel();        for (int column = 0; column < cm.getColumnCount(); column++) {            if (cm.getColumn(column) == aColumn) {                return column;            }        }        return -1;    }//// Size Methods//    private int getHeaderHeight() {        int height = 0; 	boolean accomodatedDefault = false;         TableColumnModel columnModel = header.getColumnModel();        for(int column = 0; column < columnModel.getColumnCount(); column++) { 	    TableColumn aColumn = columnModel.getColumn(column);             boolean isDefault = (aColumn.getHeaderRenderer() == null);            if (!isDefault || !accomodatedDefault) { 		Component comp = getHeaderRenderer(column); 		int rendererHeight = comp.getPreferredSize().height; 		height = Math.max(height, rendererHeight);                 // Configuring the header renderer to calculate its preferred size                // is expensive. Optimise this by assuming the default renderer                // always has the same height as the first non-zero height that                // it returns for a non-null/non-empty value.                if (isDefault && rendererHeight > 0) {                    Object headerValue = aColumn.getHeaderValue();                    if (headerValue != null) {                        headerValue = headerValue.toString();                        if (headerValue != null && !headerValue.equals("")) {                            accomodatedDefault = true;                         }                    }                }	    }        }        return height;    }    private Dimension createHeaderSize(long width) {        TableColumnModel columnModel = header.getColumnModel();        // None of the callers include the intercell spacing, do it here.        if (width > Integer.MAX_VALUE) {            width = Integer.MAX_VALUE;        }        return new Dimension((int)width, getHeaderHeight());    }    /**     * Return the minimum size of the header. The minimum width is the sum      * of the minimum widths of each column (plus inter-cell spacing).     */    public Dimension getMinimumSize(JComponent c) {        long width = 0;        Enumeration enumeration = header.getColumnModel().getColumns();        while (enumeration.hasMoreElements()) {            TableColumn aColumn = (TableColumn)enumeration.nextElement();            width = width + aColumn.getMinWidth();        }        return createHeaderSize(width);    }    /**     * Return the preferred size of the header. The preferred height is the      * maximum of the preferred heights of all of the components provided      * by the header renderers. The preferred width is the sum of the      * preferred widths of each column (plus inter-cell spacing).     */    public Dimension getPreferredSize(JComponent c) {        long width = 0;        Enumeration enumeration = header.getColumnModel().getColumns();        while (enumeration.hasMoreElements()) {            TableColumn aColumn = (TableColumn)enumeration.nextElement();            width = width + aColumn.getPreferredWidth();        }        return createHeaderSize(width);    }    /**     * Return the maximum size of the header. The maximum width is the sum      * of the maximum widths of each column (plus inter-cell spacing).     */    public Dimension getMaximumSize(JComponent c) {        long width = 0;        Enumeration enumeration = header.getColumnModel().getColumns();        while (enumeration.hasMoreElements()) {            TableColumn aColumn = (TableColumn)enumeration.nextElement();            width = width + aColumn.getMaxWidth();        }        return createHeaderSize(width);    }        private static class Actions extends UIAction {        public static final String TOGGLE_SORT_ORDER =            "toggleSortOrder";        public static final String SELECT_COLUMN_TO_LEFT =            "selectColumnToLeft";        public static final String SELECT_COLUMN_TO_RIGHT =            "selectColumnToRight";        public static final String MOVE_COLUMN_LEFT =            "moveColumnLeft";        public static final String MOVE_COLUMN_RIGHT =            "moveColumnRight";        public static final String RESIZE_LEFT =            "resizeLeft";        public static final String RESIZE_RIGHT =            "resizeRight";        public static final String FOCUS_TABLE =            "focusTable";                    public Actions(String name) {            super(name);        }                public boolean isEnabled(Object sender) {            if (sender instanceof JTableHeader) {                JTableHeader th = (JTableHeader)sender;                TableColumnModel cm = th.getColumnModel();                if (cm.getColumnCount() <= 0) {                    return false;                }                String key = getName();                BasicTableHeaderUI ui =                     (BasicTableHeaderUI)BasicLookAndFeel.getUIOfType(th.getUI(),                                                      BasicTableHeaderUI.class);                                if (ui != null) {                                    if (key == MOVE_COLUMN_LEFT) {                        return th.getReorderingAllowed()                            && maybeMoveColumn(true, th, ui, false);                    } else if (key == MOVE_COLUMN_RIGHT) {                        return th.getReorderingAllowed()                            && maybeMoveColumn(false, th, ui, false);                    } else if (key == RESIZE_LEFT ||                               key == RESIZE_RIGHT) {                        return canResize(cm.getColumn(ui.getSelectedColumnIndex()), th);                    } else if (key == FOCUS_TABLE) {                        return (th.getTable() != null);                    }                }            }            return true;        }        public void actionPerformed(ActionEvent e) {            JTableHeader th = (JTableHeader)e.getSource();            BasicTableHeaderUI ui =                 (BasicTableHeaderUI)BasicLookAndFeel.                                        getUIOfType(th.getUI(),                                            BasicTableHeaderUI.class);            if (ui == null) {                return;            }                        String name = getName();                        if (TOGGLE_SORT_ORDER == name) {                JTable table = th.getTable();                RowSorter sorter = table.getRowSorter();                if (sorter != null) {                    int columnIndex = ui.getSelectedColumnIndex();                    columnIndex = table.convertColumnIndexToModel(                                                      columnIndex);                    sorter.toggleSortOrder(columnIndex);                }            } else if (SELECT_COLUMN_TO_LEFT == name) {                if (th.getComponentOrientation().isLeftToRight()) {                    ui.selectPreviousColumn(true);                } else {                    ui.selectNextColumn(true);                }            } else if (SELECT_COLUMN_TO_RIGHT == name) {                if (th.getComponentOrientation().isLeftToRight()) {                    ui.selectNextColumn(true);                } else {                    ui.selectPreviousColumn(true);                }            } else if (MOVE_COLUMN_LEFT == name) {                moveColumn(true, th, ui);            } else if (MOVE_COLUMN_RIGHT == name) {                moveColumn(false, th, ui);            } else if (RESIZE_LEFT == name) {                resize(true, th, ui);            } else if (RESIZE_RIGHT == name) {                resize(false, th, ui);            } else if (FOCUS_TABLE == name) {                JTable table = th.getTable();                if (table != null) {                    table.requestFocusInWindow();                }            }        }                private void moveColumn(boolean leftArrow, JTableHeader th,                                BasicTableHeaderUI ui) {            maybeMoveColumn(leftArrow, th, ui, true);        }                private boolean maybeMoveColumn(boolean leftArrow, JTableHeader th,                                        BasicTableHeaderUI ui, boolean doIt) {            int oldIndex = ui.getSelectedColumnIndex();            int newIndex;                                    if (th.getComponentOrientation().isLeftToRight()) {                newIndex = leftArrow ? ui.selectPreviousColumn(doIt)                                     : ui.selectNextColumn(doIt);            } else {                newIndex = leftArrow ? ui.selectNextColumn(doIt)                                     : ui.selectPreviousColumn(doIt);            }             if (newIndex != oldIndex) {                if (doIt) {                    th.getColumnModel().moveColumn(oldIndex, newIndex);                } else {                    return true; // we'd do the move if asked                }            }                        return false;        }                private void resize(boolean leftArrow, JTableHeader th,                            BasicTableHeaderUI ui) {            int columnIndex = ui.getSelectedColumnIndex();            TableColumn resizingColumn =                th.getColumnModel().getColumn(columnIndex);            th.setResizingColumn(resizingColumn);            int oldWidth = resizingColumn.getWidth();            int newWidth = oldWidth;                        if (th.getComponentOrientation().isLeftToRight()) {                newWidth = newWidth + (leftArrow ? -1 : 1);            } else {                newWidth = newWidth + (leftArrow ? 1 : -1);            }                         ui.changeColumnWidth(resizingColumn, th, oldWidth, newWidth);        }    }}  // End of Class BasicTableHeaderUI

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -