📄 singleselectionlist.java
字号:
repaint( ); } //------------------------------------------------------------------ public void focusLost( FocusEvent event ) { repaint( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : MouseListener interface//////////////////////////////////////////////////////////////////////// public void mouseClicked( MouseEvent event ) { if ( SwingUtilities.isLeftMouseButton( event ) && (event.getClickCount( ) == 2) ) { int index = event.getY( ) / rowHeight; if ( (index >= 0) && (index < getNumElements( )) ) doEditElement( ); } } //------------------------------------------------------------------ public void mouseEntered( MouseEvent event ) { // do nothing } //------------------------------------------------------------------ public void mouseExited( MouseEvent event ) { // do nothing } //------------------------------------------------------------------ public void mousePressed( MouseEvent event ) { requestFocusInWindow( ); int index = event.getY( ) / rowHeight; if ( (index >= 0) && (index < getNumElements( )) ) { dragStartIndex = index; setSelectedIndex( index ); } else dragStartIndex = -1; } //------------------------------------------------------------------ public void mouseReleased( MouseEvent event ) { if ( SwingUtilities.isLeftMouseButton( event ) ) { if ( isDragging( ) ) { dragEndIndex = dragIndex; setDragIndex( -1 ); if ( dragEndIndex != selectedIndex ) fireActionPerformed( Command.DRAG_ELEMENT ); } } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : MouseMotionListener interface//////////////////////////////////////////////////////////////////////// public void mouseDragged( MouseEvent event ) { if ( SwingUtilities.isLeftMouseButton( event ) ) { boolean dragging = isDragging( ); if ( !dragging ) { int index = event.getY( ) / rowHeight; if ( (dragStartIndex >= 0) && (dragStartIndex != index) ) dragging = true; } if ( dragging ) { int index = Math.min( Math.max( 0, (event.getY( ) + rowHeight / 2) / rowHeight ), getNumElements( ) ); scrollRectToVisible( new Rectangle( event.getX( ), index * rowHeight, 1, rowHeight ) ); if ( dragIndex != index ) setDragIndex( index ); } } } //------------------------------------------------------------------ public void mouseMoved( MouseEvent event ) { // do nothing } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : MouseWheelListener interface//////////////////////////////////////////////////////////////////////// public void mouseWheelMoved( MouseWheelEvent event ) { if ( viewport != null ) { String command = null; int numUnits = event.getWheelRotation( ); if ( numUnits < 0 ) { numUnits = -numUnits; command = ListCommand.SCROLL_UP_UNIT; } else command = ListCommand.SCROLL_DOWN_UNIT; while ( --numUnits >= 0 ) actionPerformed( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, command ) ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : Scrollable interface//////////////////////////////////////////////////////////////////////// public Dimension getPreferredScrollableViewportSize( ) { return new Dimension( 2 * horizontalMargin + columns * columnWidth + extraWidth, viewableRows * rowHeight ); } //------------------------------------------------------------------ public boolean getScrollableTracksViewportWidth( ) { return false; } //------------------------------------------------------------------ public boolean getScrollableTracksViewportHeight( ) { return false; } //------------------------------------------------------------------ public int getScrollableUnitIncrement( Rectangle visibleRect, int orientation, int direction ) { int delta = 0; if ( orientation == SwingConstants.VERTICAL ) { int y = visibleRect.y; if ( direction < 0 ) { int row = (Math.max( 0, y ) + rowHeight - 1) / rowHeight; delta = y - (Math.max( 0, row - UNIT_INCREMENT_ROWS ) * rowHeight); } else { int row = Math.max( 0, y ) / rowHeight; int maxRow = Math.max( 0, getHeight( ) - visibleRect.height ) / rowHeight; delta = Math.min( row + UNIT_INCREMENT_ROWS, maxRow ) * rowHeight - y; } } return delta; } //------------------------------------------------------------------ public int getScrollableBlockIncrement( Rectangle visibleRect, int orientation, int direction ) { int delta = 0; if ( orientation == SwingConstants.VERTICAL ) { int y = visibleRect.y; if ( direction < 0 ) { int row = (Math.max( 0, y ) + rowHeight - 1) / rowHeight; delta = y - (Math.max( 0, row - BLOCK_INCREMENT_ROWS ) * rowHeight); } else { int row = Math.max( 0, y ) / rowHeight; int maxRow = Math.max( 0, getHeight( ) - visibleRect.height ) / rowHeight; delta = Math.min( row + BLOCK_INCREMENT_ROWS, maxRow ) * rowHeight - y; } } return delta; } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : overriding methods//////////////////////////////////////////////////////////////////////// public Dimension getPreferredSize( ) { return new Dimension( 2 * horizontalMargin + columns * columnWidth + extraWidth, Math.max( viewableRows, getNumElements( ) ) * rowHeight ); } //------------------------------------------------------------------ protected void paintComponent( Graphics gr ) { // Fill background Rectangle rect = gr.getClipBounds( ); gr.setColor( getBackground( ) ); gr.fillRect( rect.x, rect.y, rect.width, rect.height ); // Get start and end indexes of rows int startIndex = Math.max( 0, rect.y / rowHeight ); int endIndex = Math.max( 0, rect.y + rect.height + rowHeight - 1 ) / rowHeight; // Draw cells for ( int i = startIndex; i < endIndex; ++i ) { if ( i == selectedIndex ) { gr.setColor( isFocusOwner( ) ? FOCUSED_BACKGROUND_COLOUR : SELECTED_BACKGROUND_COLOUR ); gr.fillRect( rect.x, i * rowHeight, rect.width, rowHeight ); } if ( i < getNumElements( ) ) drawElement( gr, i ); } // Draw drag bar if ( isDragging( ) ) { int x1 = 0; int x2 = getWidth( ) - 1; int y = dragIndex * rowHeight - DRAG_BAR_HEIGHT / 2; gr.setColor( DRAG_BAR_COLOUR ); gr.drawLine( x1, y, x1, y ); gr.drawLine( x2, y, x2, y ); ++y; gr.drawLine( x1, y, x1 + 1, y ); gr.drawLine( x2 - 1, y, x2, y ); ++y; gr.drawLine( rect.x, y, rect.x + rect.width - 1, y ); ++y; gr.drawLine( rect.x, y, rect.x + rect.width - 1, y ); ++y; gr.drawLine( x1, y, x1 + 1, y ); gr.drawLine( x2 - 1, y, x2, y ); ++y; gr.drawLine( x1, y, x1, y ); gr.drawLine( x2, y, x2, y ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public int getColumns( ) { return columns; } //------------------------------------------------------------------ public int getViewableRows( ) { return viewableRows; } //------------------------------------------------------------------ public int getColumnWidth( ) { return columnWidth; } //------------------------------------------------------------------ public int getRowHeight( ) { return rowHeight; } //------------------------------------------------------------------ public int getHorizontalMargin( ) { return horizontalMargin; } //------------------------------------------------------------------ public int getExtraWidth( ) { return extraWidth; } //------------------------------------------------------------------ public int getSelectedIndex( ) { return selectedIndex; } //------------------------------------------------------------------ public boolean isDragging( ) { return ( dragIndex >= 0 ); } //------------------------------------------------------------------ public int getDragEndIndex( ) { return dragEndIndex; } //------------------------------------------------------------------ public JViewport getViewport( ) { return viewport; } //------------------------------------------------------------------ public Model<E> getModel( ) { return model; } //------------------------------------------------------------------ public int getNumElements( ) { return model.getNumElements( ); } //------------------------------------------------------------------ public E getElement( int index ) { return model.getElement( index ); } //------------------------------------------------------------------ public List<E> getElements( ) { List<E> elements = new ArrayList<E>( ); for ( int i = 0; i < getNumElements( ); ++i ) elements.add( getElement( i ) ); return elements; } //------------------------------------------------------------------ public E getSelectedElement( ) { return ( (selectedIndex < 0) ? null : getElement( selectedIndex ) ); } //------------------------------------------------------------------ public boolean isSelection( ) { return ( selectedIndex >= 0 ); } //------------------------------------------------------------------ public void setColumnWidth( int columnWidth ) { if ( this.columnWidth != columnWidth ) { this.columnWidth = columnWidth; resize( ); repaint( ); } } //------------------------------------------------------------------ public void setRowHeight( int rowHeight ) { if ( this.rowHeight != rowHeight ) { this.rowHeight = rowHeight; resize( ); repaint( ); } } //------------------------------------------------------------------ public void setHorizontalMargin( int horizontalMargin ) { if ( this.horizontalMargin != horizontalMargin ) { this.horizontalMargin = horizontalMargin; resize( ); repaint( ); } } //------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -