javax.swing.ListCellRenderer类的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(93)

本文整理了Java中javax.swing.ListCellRenderer类的一些代码示例,展示了ListCellRenderer类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ListCellRenderer类的具体详情如下:
包路径:javax.swing.ListCellRenderer
类名称:ListCellRenderer

ListCellRenderer介绍

暂无

代码示例

代码示例来源:origin: org.netbeans.api/org-openide-awt

/**
 * Paint one List cell: compute the relevant state, get the "rubber stamp"
 * cell renderer component, and then use the CellRendererPane to paint it.
 * Subclasses may want to override this method rather than paint().
 *
 * @see #paint
 */
private void paintCell(Graphics g, int index) {
  Object value = getModel().getElementAt(index);
  boolean cellHasFocus = hasFocus() && (index == getSelectionModel().getLeadSelectionIndex());
  boolean isSelected = getSelectionModel().isSelectedIndex(index);
  Component renderer = getCellRenderer().getListCellRendererComponent(
      this, value, index, isSelected, cellHasFocus
    );
  renderer.setSize(fixedCellWidth, fixedCellHeight);
  renderer.paint(g);
}

代码示例来源:origin: org.netbeans.api/org-openide-explorer

@Override
public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) {
  ListCellRenderer renderer = comboBox.getRenderer();
  //Fix for an obscure condition when renderer may be null -
  //can't figure how this can happen unless the combo box is
  //painted before installUI() has completed (which is called
  //by the superclass constructor calling updateUI().  Only
  //happens when opening an individual Properties window.  Maybe
  //the window is constructed off the AWT thread?
  if ((listBox == null) || (renderer == null)) {
    return;
  }
  Component c;
  c = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(), -1, false, false);
  c.setFont(comboBox.getFont());
  c.setForeground(comboBox.isEnabled() ? comboBox.getForeground() : PropUtils.getDisabledForeground());
  c.setBackground(comboBox.getBackground());
  boolean shouldValidate = false;
  if (c instanceof JPanel) {
    shouldValidate = true;
  }
  currentValuePane.paintComponent(
    g, c, comboBox, bounds.x, bounds.y, bounds.width, bounds.height, shouldValidate
  );
}

代码示例来源:origin: stackoverflow.com

JList<?> list = getList();
 if (Objects.nonNull(list)) {
  list.removeMouseListener(cbml);
  list.removeMouseMotionListener(cbml);
setRenderer(new ButtonsRenderer<E>(this));
JList<?> list = getList();
if (Objects.nonNull(list)) {
 cbml = new CellButtonsMouseListener();
 list.addMouseListener(cbml);
JList list = (JList) e.getComponent();
Point pt = e.getPoint();
int index = list.locationToIndex(pt);
if (!list.getCellBounds(index, index).contains(pt)) {
 if (prevIndex >= 0) {
  Rectangle r = list.getCellBounds(prevIndex, prevIndex);
  listRepaint(list, r);
Container c = (Container) list.getCellRenderer().getListCellRendererComponent(
 if (m.getSize() > 1) {
if (index < 0 || m.getSize() - 1 <= 0) {
 setOpaque(false);
 deleteButton.setVisible(false);

代码示例来源:origin: stackoverflow.com

JComboBox comboBox = new JComboBox();
  comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX");
  comboBox.addItem(new Double(1));
  comboBox.addItem(new Double(2.25));
  comboBox.addItem(new Double(3.5));
public Component getListCellRendererComponent(JList list, Object value,
    int index, boolean isSelected, boolean cellHasFocus) {
  Component c = defaultRenderer.getListCellRendererComponent(
      list, value, index, isSelected, cellHasFocus);
  if (c instanceof JLabel) {
    c.setBackground(Color.red);
  } else {
    c.setBackground(Color.red);
    c = super.getListCellRendererComponent(
        list, value, index, isSelected, cellHasFocus);

代码示例来源:origin: UISpec4J/UISpec4J

private Component getComponent(int index) {
 ListCellRenderer renderer = jList.getCellRenderer();
 return renderer.getListCellRendererComponent(jList,
                        jList.getModel().getElementAt(index),
                        index, false, false);
}

代码示例来源:origin: com.haulmont.thirdparty/swingx-core

private static boolean pointIsInActualBounds(JList list, int index,
    Point point) {
  ListCellRenderer renderer = list.getCellRenderer();
  ListModel model = list.getModel();
  Object element = model.getElementAt(index);
  Component comp = renderer.getListCellRendererComponent(list, element,
      index, false, false);
  Dimension prefSize = comp.getPreferredSize();
  Rectangle cellBounds = list.getCellBounds(index, index);
  if (!(comp.getComponentOrientation().isLeftToRight())) {
    cellBounds.x += cellBounds.width - prefSize.width;
  }
  cellBounds.width = prefSize.width;
  return cellBounds.contains(point);
}

代码示例来源:origin: stackoverflow.com

m.addElement("Element "+i);
JList<String> list = new JList<String>(m);
list.setDropMode(DropMode.ON_OR_INSERT);
list.setDragEnabled(true);
list.setCellRenderer(new DefaultListCellRenderer() {
    if (cellHasFocus == false && isSelected == false) { 
      if (index % 2 == 0) {
        listCellRendererComponent.setBackground(Color.RED);
      } else if (index % 3==0) {
        listCellRendererComponent.setBackground(Color.GREEN);
      } else {
        listCellRendererComponent.setBackground(Color.BLUE);
    for(int idx: selectedIndices) {
      ListCellRenderer<? super String> cellRenderer = sourceList.getCellRenderer();
      String valueAt = sourceList.getModel().getElementAt(idx);
      Component c = cellRenderer.getListCellRendererComponent(sourceList, valueAt, idx, false, false);
      Rectangle itemCellBounds = sourceList.getCellBounds(idx, idx);

代码示例来源:origin: pentaho/pentaho-reporting

/**
 * Return the component that should be added to the tree hierarchy for this editor
 */
public Component getEditorComponent() {
 final Component listCellRendererComponent = renderer.getListCellRendererComponent
  ( new JList(), comboBox.getSelectedItem(), comboBox.getSelectedIndex(), false, comboBox.hasFocus() );
 if ( listCellRendererComponent instanceof JComponent ) {
  final JComponent jc = (JComponent) listCellRendererComponent;
  jc.setBorder( new LineBorder( Color.BLACK ) );
 }
 return listCellRendererComponent;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-kenai-ui

private boolean redispatchComponent(MouseEvent e) {
  Point p = e.getPoint();
  int index = list.locationToIndex(p);
  if( index < 0 || index >= list.getModel().getSize() )
    return false;
  ListCellRenderer renderer = list.getCellRenderer();
  if( null == renderer )
    return false;
  Component renComponent = renderer.getListCellRendererComponent(list, list.getModel().getElementAt(index), index, false, false);
  if( null == renComponent )
    return false;
  if( null == rect )
    return false;
  renComponent.setBounds(0,0,rect.width, rect.height);
  renComponent.doLayout();
  Point p3 = rect.getLocation();
       e.isPopupTrigger(),
               MouseEvent.NOBUTTON );
    dispatchComponent.dispatchEvent(newEvent);
    list.repaint(rect);
    e.consume();

代码示例来源:origin: info.aduna.commons/aduna-commons-swing

public void mouseMoved(MouseEvent e) {
  Point location = new Point(e.getX(), e.getY());
  row = list.locationToIndex(location);
  bounds = list.getCellBounds(row, row);
    Rectangle visibleRect = list.getVisibleRect();
    ListCellRenderer renderer = list.getCellRenderer();
    Object value = list.getModel().getElementAt(row);
    Component rendererComponent = renderer.getListCellRendererComponent(list, value, row, false, false);
    Dimension size = rendererComponent.getPreferredSize();
    bounds.width = size.width;

代码示例来源:origin: stackoverflow.com

JComboBox comboBox = new JComboBox(data);
comboBox.setPreferredSize(comboBox.getPreferredSize());
comboBox.setRenderer(new HighLightRowRenderer(comboBox.getRenderer()));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  Component component = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  Dimension size = component.getPreferredSize();
  if (index == 0) {
    component.setBackground(Color.red);
    if (component instanceof JLabel) {
      ((JLabel) component).setHorizontalTextPosition(JLabel.CENTER);

代码示例来源:origin: JetBrains/jediterm

@Override
  public void actionPerformed(final ActionEvent e) {
    if (!(e.getSource() instanceof JComboBox)) return;
    final JComboBox comboBox = (JComboBox) e.getSource();
    final String text;
    final Object selectedItem = comboBox.getSelectedItem();
    if (selectedItem instanceof String) {
      text = (String) selectedItem;
    } else {
      final Component component =
          comboBox.getRenderer().getListCellRendererComponent(new JList(), selectedItem, 0, false, false);
      if (component instanceof JLabel) {
        text = ((JLabel) component).getText();
      } else if (component != null) {
        final String str = component.toString();
        // skip default Component.toString and handle SimpleColoredComponent case
        text = str == null || str.startsWith(component.getClass().getName() + "[") ? null : str;
      } else {
        text = null;
      }
    }
    if (text != null) {
      final JTextField textField = new JTextField(text);
      textField.selectAll();
      textField.copy();
    }
  }
});

代码示例来源:origin: org.bidib.jbidib.swinglabs.swingx/swingx-core

int dataModelSize = dataModel.getSize();
ListCellRenderer renderer = list.getCellRenderer();
    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);

代码示例来源:origin: io.ultreia.java4all.jaxx/jaxx-widgets-extra

public Component getRendererComponent() {
  Object item = list.getModel().getElementAt(rowIndex);
  boolean isSelected = list.isSelectedIndex(rowIndex);
  boolean isFocussed = list.hasFocus() && rowIndex == list.getLeadSelectionIndex();
  ListCellRenderer renderer = list.getCellRenderer();
  return renderer.getListCellRendererComponent(list, item, rowIndex, isSelected, isFocussed);
}

代码示例来源:origin: com.fifesoft/autocomplete

protected void updateLayoutState() {
  ListModel model = list.getModel();
  int itemCount = model.getSize();
  ListCellRenderer<Object> renderer = (ListCellRenderer<Object>)list.getCellRenderer();
  cellWidth = list.getWidth();
  if (list.getParent() instanceof JViewport) { // Always true for us
    cellWidth = list.getParent().getWidth();
    Object value = model.getElementAt(0);
    Component c = renderer.getListCellRendererComponent(list,
                      value, 0, false, false);
    rendererPane.add(c);
    Dimension cellSize = c.getPreferredSize();
    cellHeight = cellSize.height;
    cellWidth = Math.max(cellWidth, cellSize.width);

代码示例来源:origin: org.java.net.substance/substance

ListCellRenderer cellRenderer, ListModel dataModel,
  ListSelectionModel selModel, int leadIndex) {
Object value = dataModel.getElementAt(row);
boolean cellHasFocus = list.hasFocus() && (row == leadIndex);
boolean isSelected = selModel.isSelectedIndex(row);
    .getListCellRendererComponent(list, value, row, isSelected,
        cellHasFocus);
      .min(cw, rendererComponent.getPreferredSize().width + 4);
  if (!isLeftToRight) {
    cx += (cw - w);
g2d.setComposite(LafWidgetUtilities.getAlphaComposite(list, g));
if (!isWatermarkBleed) {
  Color background = rendererComponent.getBackground();
      && (!list.getBackground().equals(background) || this.updateInfo.isInDecorationArea)) {
    g2d.setColor(background);
    g2d.fillRect(cx, cy, cw, ch);
      rendererComponent.getBackground(), new Rectangle(cx, cy,
          cw, ch));
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null && !dropLocation.isInsert()
    && dropLocation.getIndex() == row) {

代码示例来源:origin: com.synaptix/SynaptixTattoo

int minRowCount = Math.min(maxRowCount, comboBox.getItemCount());
int height = 0;
ListCellRenderer renderer = list.getCellRenderer();
Object value = null;
  value = list.getModel().getElementAt(i);
  Component c = renderer.getListCellRendererComponent(list, value, i,
      false, false);
  height += c.getPreferredSize().height;
  height = comboBox.getHeight();

代码示例来源:origin: stackoverflow.com

ListCellRenderer cellRenderer, ListModel dataModel,
  ListSelectionModel selModel, int leadIndex) {
Object value = dataModel.getElementAt(row);
boolean cellHasFocus = list.hasFocus() && (row == leadIndex);
boolean isSelected = selModel.isSelectedIndex(row);
    .getListCellRendererComponent(list, value, row, isSelected,
        cellHasFocus);
      .min(cw, rendererComponent.getPreferredSize().width + 4);
  if (!isLeftToRight) {
    cx += (cw - w);

代码示例来源:origin: net.sf.tinylaf/tinylaf

ListCellRenderer renderer = comboBox.getRenderer();
Object prototypeValue = comboBox.getPrototypeDisplayValue();
if(prototypeValue != null)  {
  result = getSizeForComponent(renderer.getListCellRendererComponent(
    listBox, prototypeValue, -1, false, false));
  ComboBoxModel model = comboBox.getModel();
  int modelSize = model.getSize();
  Dimension d;
      d = getSizeForComponent(renderer.getListCellRendererComponent(
        listBox, model.getElementAt(i), -1, false, false));
      result.width = Math.max(result.width,d.width);
      result.height = Math.max(result.height,d.height);
if(comboBox.isEditable()) {
  Dimension d = editor.getPreferredSize();
  result.width = Math.max(result.width, d.width);
  result.height = Math.max(result.height, d.height);

代码示例来源:origin: stackoverflow.com

final JComboBox box = new JComboBox(new Object[] {1, 2, 3, 4, 5, 6, 6, 34,3,3});
 PopupMenuListener l = new PopupMenuListener() {
     Component comp = renderer.getListCellRendererComponent(popup.getList(), 1, 0, false, false);
     int pref = comp.getPreferredSize().height;
     return pref;

相关文章