下面列出了javax.swing.JList#getCellBounds ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
/**
* 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());
}
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;
}
}
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();
}
}
}
@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);
}
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 );
}
}
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 );
}
}
@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);
}