📄 basiclistui.java
字号:
for (i = 0; i < size; i++) { if ((y0 >= y) && (y0 < y + cellHeights[i])) { return row; } y += cellHeights[i]; row += 1; } return i - 1; } } /** * Returns the closest row that starts at the specified y-location * in the passed in column. */ private int convertLocationToRowInColumn(int y, int column) { int x = 0; if (layoutOrientation != JList.VERTICAL) { if (isLeftToRight) { x = column * cellWidth; } else { x = list.getWidth() - (column+1)*cellWidth - list.getInsets().right; } } return convertLocationToRow(x, y, true); } /** * Returns the closest location to the model index of the passed in * location. */ private int convertLocationToModel(int x, int y) { int row = convertLocationToRow(x, y, true); int column = convertLocationToColumn(x, y); if (row >= 0 && column >= 0) { return getModelIndex(column, row); } return -1; } /** * Returns the number of rows in the given column. */ private int getRowCount(int column) { if (column < 0 || column >= columnCount) { return -1; } if (layoutOrientation == JList.VERTICAL || (column == 0 && columnCount == 1)) { return list.getModel().getSize(); } if (column >= columnCount) { return -1; } if (layoutOrientation == JList.VERTICAL_WRAP) { if (column < (columnCount - 1)) { return rowsPerColumn; } return list.getModel().getSize() - (columnCount - 1) * rowsPerColumn; } // JList.HORIZONTAL_WRAP int diff = columnCount - (columnCount * rowsPerColumn - list.getModel().getSize()); if (column >= diff) { return Math.max(0, rowsPerColumn - 1); } return rowsPerColumn; } /** * Returns the model index for the specified display location. * If <code>column</code>x<code>row</code> is beyond the length of the * model, this will return the model size - 1. */ private int getModelIndex(int column, int row) { switch (layoutOrientation) { case JList.VERTICAL_WRAP: return Math.min(list.getModel().getSize() - 1, rowsPerColumn * column + Math.min(row, rowsPerColumn-1)); case JList.HORIZONTAL_WRAP: return Math.min(list.getModel().getSize() - 1, row * columnCount + column); default: return row; } } /** * Returns the closest column to the passed in location. */ private int convertLocationToColumn(int x, int y) { if (cellWidth > 0) { if (layoutOrientation == JList.VERTICAL) { return 0; } Insets insets = list.getInsets(); int col; if (isLeftToRight) { col = (x - insets.left) / cellWidth; } else { col = (list.getWidth() - x - insets.right - 1) / cellWidth; } if (col < 0) { return 0; } else if (col >= columnCount) { return columnCount - 1; } return col; } return 0; } /** * Returns the row that the model index <code>index</code> will be * displayed in.. */ private int convertModelToRow(int index) { int size = list.getModel().getSize(); if ((index < 0) || (index >= size)) { return -1; } if (layoutOrientation != JList.VERTICAL && columnCount > 1 && rowsPerColumn > 0) { if (layoutOrientation == JList.VERTICAL_WRAP) { return index % rowsPerColumn; } return index / columnCount; } return index; } /** * Returns the column that the model index <code>index</code> will be * displayed in. */ private int convertModelToColumn(int index) { int size = list.getModel().getSize(); if ((index < 0) || (index >= size)) { return -1; } if (layoutOrientation != JList.VERTICAL && rowsPerColumn > 0 && columnCount > 1) { if (layoutOrientation == JList.VERTICAL_WRAP) { return index / rowsPerColumn; } return index % columnCount; } return 0; } /** * If updateLayoutStateNeeded is non zero, call updateLayoutState() and reset * updateLayoutStateNeeded. This method should be called by methods * before doing any computation based on the geometry of the list. * For example it's the first call in paint() and getPreferredSize(). * * @see #updateLayoutState */ protected void maybeUpdateLayoutState() { if (updateLayoutStateNeeded != 0) { updateLayoutState(); updateLayoutStateNeeded = 0; } } /** * Recompute the value of cellHeight or cellHeights based * and cellWidth, based on the current font and the current * values of fixedCellWidth, fixedCellHeight, and prototypeCellValue. * * @see #maybeUpdateLayoutState */ protected void updateLayoutState() { /* If both JList fixedCellWidth and fixedCellHeight have been * set, then initialize cellWidth and cellHeight, and set * cellHeights to null. */ int fixedCellHeight = list.getFixedCellHeight(); int fixedCellWidth = list.getFixedCellWidth(); cellWidth = (fixedCellWidth != -1) ? fixedCellWidth : -1; if (fixedCellHeight != -1) { cellHeight = fixedCellHeight; cellHeights = null; } else { cellHeight = -1; cellHeights = new int[list.getModel().getSize()]; } /* If either of JList fixedCellWidth and fixedCellHeight haven't * been set, then initialize cellWidth and cellHeights by * scanning through the entire model. Note: if the renderer is * null, we just set cellWidth and cellHeights[*] to zero, * if they're not set already. */ if ((fixedCellWidth == -1) || (fixedCellHeight == -1)) { ListModel dataModel = list.getModel(); int dataModelSize = dataModel.getSize(); ListCellRenderer renderer = list.getCellRenderer(); if (renderer != null) { for(int index = 0; index < dataModelSize; index++) { Object value = dataModel.getElementAt(index); Component c = renderer.getListCellRendererComponent(list, value, index, false, false); rendererPane.add(c); Dimension cellSize = c.getPreferredSize(); if (fixedCellWidth == -1) { cellWidth = Math.max(cellSize.width, cellWidth); } if (fixedCellHeight == -1) { cellHeights[index] = cellSize.height; } } } else { if (cellWidth == -1) { cellWidth = 0; } if (cellHeights == null) { cellHeights = new int[dataModelSize]; } for(int index = 0; index < dataModelSize; index++) { cellHeights[index] = 0; } } } columnCount = 1; if (layoutOrientation != JList.VERTICAL) { updateHorizontalLayoutState(fixedCellWidth, fixedCellHeight); } } /** * Invoked when the list is layed out horizontally to determine how * many columns to create. * <p> * This updates the <code>rowsPerColumn, </code><code>columnCount</code>, * <code>preferredHeight</code> and potentially <code>cellHeight</code> * instance variables. */ private void updateHorizontalLayoutState(int fixedCellWidth, int fixedCellHeight) { int visRows = list.getVisibleRowCount(); int dataModelSize = list.getModel().getSize(); Insets insets = list.getInsets(); listHeight = list.getHeight(); listWidth = list.getWidth(); if (dataModelSize == 0) { rowsPerColumn = columnCount = 0; preferredHeight = insets.top + insets.bottom; return; } int height; if (fixedCellHeight != -1) { height = fixedCellHeight; } else { // Determine the max of the renderer heights. int maxHeight = 0; if (cellHeights.length > 0) { maxHeight = cellHeights[cellHeights.length - 1]; for (int counter = cellHeights.length - 2; counter >= 0; counter--) { maxHeight = Math.max(maxHeight, cellHeights[counter]); } } height = cellHeight = maxHeight; cellHeights = null; } // The number of rows is either determined by the visible row // count, or by the height of the list. rowsPerColumn = dataModelSize; if (visRows > 0) { rowsPerColumn = visRows; columnCount = Math.max(1, dataModelSize / rowsPerColumn); if (dataModelSize > 0 && dataModelSize > rowsPerColumn && dataModelSize % rowsPerColumn != 0) { columnCount++; } if (layoutOrientation == JList.HORIZONTAL_WRAP) { // Because HORIZONTAL_WRAP flows differently, the // rowsPerColumn needs to be adjusted. rowsPerColumn = (dataModelSize / columnCount); if (dataModelSize % columnCount > 0) { rowsPerColumn++; } } } else if (layoutOrientation == JList.VERTICAL_WRAP && height != 0) { rowsPerColumn = Math.max(1, (listHeight - insets.top - insets.bottom) / height); columnCount = Math.max(1, dataModelSize / rowsPerColumn); if (dataModelSize > 0 && dataModelSize > rowsPerColumn && dataModelSize % rowsPerColumn != 0) { columnCount++; } } else if (layoutOrientation == JList.HORIZONTAL_WRAP && cellWidth > 0 && listWidth > 0) { columnCount = Math.max(1, (listWidth - insets.left - insets.right) / cellWidth); rowsPerColumn = dataModelSize / columnCount; if (dataModelSize % columnCount > 0) { rowsPerColumn++; } } preferredHeight = rowsPerColumn * cellHeight + insets.top + insets.bottom; } private Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } /** * Mouse input, and focus handling for JList. An instance of this * class is added to the appropriate java.awt.Component lists * at installUI() time. Note keyboard input is handled with JComponent * KeyboardActions, see installKeyboardActions(). * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see #createMouseInputListener * @see #installKeyboardActions * @see #installUI */ public class MouseInputHandler implements MouseInputListener { public void mouseClicked(MouseEvent e) { getHandler().mouseClicked(e); } public void mouseEntered(MouseEvent e) { getHandler().mouseEntered(e); } public void mouseExited(MouseEvent e) { getHandler().mouseExited(e); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -