📄 jxtable.java
字号:
/** * Instantiates a JXTable with data in a array or rows and column names. * * @param rowData * Row data, as a two-dimensional Array of Objects (by row, for * column). * @param columnNames * Column names, as a Array of Strings. */ public JXTable(Object[][] rowData, Object[] columnNames) { super(rowData, columnNames); init(); } /** Initializes the table for use. */ protected void init() { setSortable(true); // guarantee getFilters() to return != null setFilters(null); initActionsAndBindings(); // instantiate row height depending on font size updateRowHeightUI(false); } /** * Property to enable/disable rollover support. This can be enabled to show * "live" rollover behaviour, f.i. the cursor over LinkModel cells. Default * is disabled. If using a RolloverHighlighter on the table, this should be * set to true. * * @param rolloverEnabled */ public void setRolloverEnabled(boolean rolloverEnabled) { boolean old = isRolloverEnabled(); if (rolloverEnabled == old) return; if (rolloverEnabled) { rolloverProducer = createRolloverProducer(); addMouseListener(rolloverProducer); addMouseMotionListener(rolloverProducer); linkController = new LinkController(); addPropertyChangeListener(linkController); } else { removeMouseListener(rolloverProducer); removeMouseMotionListener(rolloverProducer); rolloverProducer = null; removePropertyChangeListener(linkController); linkController = null; } firePropertyChange("rolloverEnabled", old, isRolloverEnabled()); } /** * creates and returns the RolloverProducer to use. * * @return */ protected RolloverProducer createRolloverProducer() { RolloverProducer r = new RolloverProducer() { protected void updateRolloverPoint(JComponent component, Point mousePoint) { JXTable table = (JXTable) component; int col = table.columnAtPoint(mousePoint); int row = table.rowAtPoint(mousePoint); if ((col < 0) || (row < 0)) { row = -1; col = -1; } rollover.x = col; rollover.y = row; } }; return r; } /** * Returns the rolloverEnabled property. * * @return <code>true</code> if rollover is enabled */ public boolean isRolloverEnabled() { return rolloverProducer != null; } /** * If the default editor for LinkModel.class is of type LinkRenderer enables * link visiting with the given linkVisitor. As a side-effect the rollover * property is set to true. * * @param linkVisitor */ public void setDefaultLinkVisitor(ActionListener linkVisitor) {// TableCellEditor editor = getDefaultEditor(LinkModel.class);// if (editor instanceof LinkRenderer) {// ((LinkRenderer) editor).setVisitingDelegate(linkVisitor);// } TableCellRenderer renderer = getDefaultRenderer(LinkModel.class); if (renderer instanceof LinkRenderer) { ((LinkRenderer) renderer).setVisitingDelegate(linkVisitor); } setRolloverEnabled(true); } /** * listens to rollover properties. * Repaints effected component regions. * Updates link cursor. * * @author Jeanette Winzenburg */ public class LinkController implements PropertyChangeListener { private Cursor oldCursor; public void propertyChange(PropertyChangeEvent evt) { if (RolloverProducer.ROLLOVER_KEY.equals(evt.getPropertyName())) { rollover((JXTable) evt.getSource(), (Point) evt .getOldValue(), (Point) evt.getNewValue()); } else if (RolloverProducer.CLICKED_KEY.equals(evt.getPropertyName())) { click((JXTable) evt.getSource(), (Point) evt.getOldValue(), (Point) evt.getNewValue()); } }// --------------------------- JTable rollover private void rollover(JXTable table, Point oldLocation, Point newLocation) { if (oldLocation != null) { Rectangle r = table.getCellRect(oldLocation.y, oldLocation.x, false); r.x = 0; r.width = table.getWidth(); table.repaint(r); } if (newLocation != null) { Rectangle r = table.getCellRect(newLocation.y, newLocation.x, false); r.x = 0; r.width = table.getWidth(); table.repaint(r); } setLinkCursor(table, newLocation); } private void click(JXTable list, Point oldLocation, Point newLocation) { if (!isLinkColumn(list, newLocation)) return; TableCellRenderer renderer = list.getCellRenderer(newLocation.y, newLocation.x); // PENDING: JW - don't ask the model, ask the list! Component comp = list.prepareRenderer(renderer, newLocation.y, newLocation.x); if (comp instanceof AbstractButton) { // this is fishy - needs to be removed as soon as JList is editable ((AbstractButton) comp).doClick(); list.repaint(); } } private void setLinkCursor(JXTable table, Point location) { if (isLinkColumn(table, location)) { if (oldCursor == null) { oldCursor = table.getCursor(); table.setCursor(Cursor .getPredefinedCursor(Cursor.HAND_CURSOR)); } } else { if (oldCursor != null) { table.setCursor(oldCursor); oldCursor = null; } } } private boolean isLinkColumn(JXTable table, Point location) { if (location == null || location.x < 0) return false; return (table.getColumnClass(location.x) == LinkModel.class); } } //--------------------------------- ColumnControl /** * overridden to addionally configure the upper right corner of an enclosing * scrollpane with the ColumnControl. */ protected void configureEnclosingScrollPane() { super.configureEnclosingScrollPane(); configureColumnControl(); configureViewportBackground(); } /** * set's the viewports background to this.background.<p> * * PENDING: need to * repeat on background changes to this! * */ protected void configureViewportBackground() { Container p = getParent(); if (p instanceof JViewport) { p.setBackground(getBackground()); } } /** * configure the upper right corner of an enclosing scrollpane with/o the * ColumnControl, depending on setting of columnControl visibility flag.<p> * * PENDING: should choose corner depending on component orientation. */ private void configureColumnControl() { Container p = getParent(); if (p instanceof JViewport) { Container gp = p.getParent(); if (gp instanceof JScrollPane) { JScrollPane scrollPane = (JScrollPane) gp; // Make certain we are the viewPort's view and not, for // example, the rowHeaderView of the scrollPane - // an implementor of fixed columns might do this. JViewport viewport = scrollPane.getViewport(); if (viewport == null || viewport.getView() != this) { return; } if (isColumnControlVisible()) { verticalScrollPolicy = scrollPane .getVerticalScrollBarPolicy(); scrollPane.setCorner(JScrollPane.UPPER_TRAILING_CORNER, getColumnControl()); scrollPane .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); } else { if (verticalScrollPolicy != 0) { // Fix #155-swingx: reset only if we had force always before // PENDING: JW - doesn't cope with dynamically changing the policy // shouldn't be much of a problem because doesn't happen too often?? scrollPane.setVerticalScrollBarPolicy(verticalScrollPolicy); } try { scrollPane.setCorner(JScrollPane.UPPER_TRAILING_CORNER, null); } catch (Exception ex) { // Ignore spurious exception thrown by JScrollPane. This // is a Swing bug! } } } } } /** * Hack around core swing JScrollPane bug: can't cope with * corners when changing component orientation at runtime. * overridden to re-configure the columnControl. */ @Override public void setComponentOrientation(ComponentOrientation o) { super.setComponentOrientation(o); configureColumnControl(); } /** * returns visibility flag of column control. * <p> * * Note: if the table is not inside a JScrollPane the column control is not * shown even if this returns true. In this case it's the responsibility of * the client code to actually show it. * * @return */ public boolean isColumnControlVisible() { return columnControlVisible; } /** * returns the component for column control. * * @return */ public JComponent getColumnControl() { if (columnControlButton == null) { columnControlButton = new ColumnControlButton(this, new ColumnControlIcon()); } return columnControlButton; } /** * bound property to flag visibility state of column control. * * @param showColumnControl */ public void setColumnControlVisible(boolean showColumnControl) { boolean old = columnControlVisible; this.columnControlVisible = showColumnControl; configureColumnControl(); firePropertyChange("columnControlVisible", old, columnControlVisible); } //--------------------- actions /** * A small class which dispatches actions. TODO: Is there a way that we can * make this static? JW: I hate those if constructs... we are in OO-land! */ private class Actions extends UIAction { Actions(String name) { super(name); } public void actionPerformed(ActionEvent evt) { if ("print".equals(getName())) { try { print(); } catch (PrinterException ex) { // REMIND(aim): should invoke pluggable application error // handler ex.printStackTrace(); } } else if ("find".equals(getName())) { find(); } } } private void initActionsAndBindings() { // Register the actions that this class can handle. ActionMap map = getActionMap(); map.put("print", new Actions("print")); map.put("find", new Actions("find")); map.put(PACKALL_ACTION_COMMAND, createPackAllAction()); map.put(PACKSELECTED_ACTION_COMMAND, createPackSelectedAction()); map.put(HORIZONTALSCROLL_ACTION_COMMAND, createHorizontalScrollAction()); // JW: this should be handled by the LF! KeyStroke findStroke = KeyStroke.getKeyStroke("control F"); getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(findStroke, "find"); } /** Creates an Action for horizontal scrolling. */ private Action createHorizontalScrollAction() { String actionName = getUIString(HORIZONTALSCROLL_ACTION_COMMAND); BoundAction action = new BoundAction(actionName, HORIZONTALSCROLL_ACTION_COMMAND); action.setStateAction(); action.registerCallback(this, "setHorizontalScrollEnabled"); action.setSelected(isHorizontalScrollEnabled()); return action; } private String getUIString(String key) { String text = UIManager.getString(UIPREFIX + key); return text != null ? text : key; } /** Creates an Action for packing selected columns. */ private Action createPackSelectedAction() { String text = getUIString(PACKSELECTED_ACTION_COMMAND); BoundAction action = new BoundAction(text, PACKSELECTED_ACTION_COMMAND); action.registerCallback(this, "packSelected"); action.setEnabled(getSelectedColumnCount() > 0); return action; } /** Creates an Action for packing all columns. */ private Action createPackAllAction() { String text = getUIString(PACKALL_ACTION_COMMAND); BoundAction action = new BoundAction(text, PACKALL_ACTION_COMMAND); action.registerCallback(this, "packAll"); return action; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -