Java Code Examples for javax.swing.JList#getCellBounds()

The following examples show how to use javax.swing.JList#getCellBounds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the given point is within the actual bounds of the
 * JList item at index (not just inside the cell).
 */
private static <T> boolean pointIsInActualBounds(JList<T> list, int index,
                                            Point point) {
    ListCellRenderer<? super T> renderer = list.getCellRenderer();
    T value = list.getModel().getElementAt(index);
    Component item = renderer.getListCellRendererComponent(list,
                      value, index, false, false);
    Dimension itemSize = item.getPreferredSize();
    Rectangle cellBounds = list.getCellBounds(index, index);
    if (!item.getComponentOrientation().isLeftToRight()) {
        cellBounds.x += (cellBounds.width - itemSize.width);
    }
    cellBounds.width = itemSize.width;

    return cellBounds.contains(point);
}
 
Example 2
Source File: AbstractPageListPopupListener.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Show popup menu in response to a key event.
 * 
 * @param e Event.
 */
@Override
protected void showPopup(KeyEvent e) {

  // Retrieve information
  if (!(e.getComponent() instanceof JList)) {
    return;
  }
  JList tmpList = (JList) e.getComponent();
  int position = tmpList.getSelectedIndex();
  if (position < 0) {
    return;
  }
  Object object = tmpList.getModel().getElementAt(position);
  if (!(object instanceof Page)) {
    return;
  }
  Page link = (Page) object;
  Rectangle rect = tmpList.getCellBounds(position, position);
  showPopup(tmpList, link, (int) rect.getMinX(), (int) rect.getMaxY());
}
 
Example 3
Source File: JideCheckBoxListItemElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public static boolean clicksInCheckBox(Point e, JList list) {
    int index = list.locationToIndex(e);
    int hotspot = new JCheckBox().getPreferredSize().width;
    Rectangle bounds = list.getCellBounds(index, index);
    if (bounds != null) {
        if (list.getComponentOrientation().isLeftToRight()) {
            return e.getX() < bounds.x + hotspot;
        } else {
            return e.getX() > bounds.x + bounds.width - hotspot;
        }
    } else {
        return false;
    }
}
 
Example 4
Source File: ViewTooltips.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void showJList (JScrollPane view, Point pt) {
    JList list = (JList) view.getViewport().getView();
    Point p = SwingUtilities.convertPoint(view, pt.x, pt.y, list);
    int row = list.locationToIndex(p);
    if (row == -1) {
        hide();
        return;
    }
    Rectangle bds = list.getCellBounds(row, 
            row);
    //GetCellBounds returns a width that is the
    //full component width;  we want only what
    //the renderer really needs.
    ListCellRenderer ren = list.getCellRenderer();
    Dimension rendererSize = 
            ren.getListCellRendererComponent(list, 
            list.getModel().getElementAt(row), 
            row, false, false).getPreferredSize();
    
    bds.width = rendererSize.width;
    if (bds == null || !bds.contains(p)) {
        hide();
        return;
    }
    if (setCompAndRow (list, row)) {
        Rectangle visible = getShowingRect (view);
        Rectangle[] rects = getRects (bds, visible);
        if (rects.length > 0) {
            ensureOldPopupsHidden();
            painter.configure(
                    list.getModel().getElementAt(row), 
                    view, list, row);
            showPopups (rects, bds, visible, list, view);
        } else {
            hide();
        }
    }
}
 
Example 5
Source File: AquaAudioListUI.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public Point getLocation(JList list, Object value, int row,
		boolean isSelected, boolean cellHasFocus) {
	Rectangle r = list.getCellBounds(row, row);
	return new Point(r.width / 2 - icon.getIconWidth() / 2, r.height
			/ 2 - icon.getIconHeight() / 2 - 10);
}
 
Example 6
Source File: ComboBoxAutoCompleteSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void matchSelection( DocumentEvent e ) {
    if( isIgnoreSelectionEvents( combo ) )
        return;
    try {
        setIgnoreSelectionEvents( combo, true );
        if( !combo.isDisplayable() )
            return;
        String editorText;
        try {
            editorText = e.getDocument().getText( 0, e.getDocument().getLength() );
        } catch( BadLocationException ex ) {
            //ignore
            return;
        }

        if( null != combo.getSelectedItem() && combo.getSelectedItem().toString().equals(editorText) )
            return;

        if( !combo.isPopupVisible() ) {
            combo.showPopup();
        }

        JList list = getPopupList( combo );
        if( null == list )
            return;

        int matchIndex = findMatch( combo, editorText );

        if( matchIndex >= 0 ) {
            list.setSelectedIndex( matchIndex );
            Rectangle rect = list.getCellBounds(matchIndex, matchIndex);
            if( null != rect )
                list.scrollRectToVisible( rect );
        } else {
            list.clearSelection();
            list.scrollRectToVisible( new Rectangle( 1, 1 ) );
        }
    } finally {
        setIgnoreSelectionEvents( combo, false );
    }
}
 
Example 7
Source File: ComboBoxAutoCompleteSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void matchSelection( DocumentEvent e ) {
    if( isIgnoreSelectionEvents( combo ) )
        return;
    try {
        setIgnoreSelectionEvents( combo, true );
        if( !combo.isDisplayable() )
            return;
        String editorText;
        try {
            editorText = e.getDocument().getText( 0, e.getDocument().getLength() );
        } catch( BadLocationException ex ) {
            //ignore
            return;
        }

        if( null != combo.getSelectedItem() && combo.getSelectedItem().toString().equals(editorText) )
            return;

        if( !combo.isPopupVisible() ) {
            combo.showPopup();
        }

        JList list = getPopupList( combo );
        if( null == list )
            return;

        int matchIndex = findMatch( combo, editorText );

        if( matchIndex >= 0 ) {
            list.setSelectedIndex( matchIndex );
            Rectangle rect = list.getCellBounds(matchIndex, matchIndex);
            if( null != rect )
                list.scrollRectToVisible( rect );
        } else {
            list.clearSelection();
            list.scrollRectToVisible( new Rectangle( 1, 1 ) );
        }
    } finally {
        setIgnoreSelectionEvents( combo, false );
    }
}
 
Example 8
Source File: CloseDecoration.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
public Point getLocation(JList list, Object value, int row,
		boolean isSelected, boolean cellHasFocus) {
	Rectangle r = list.getCellBounds(row, row);
	return new Point(r.width - normalIcon.getIconWidth() - 2, 2);
}