1

问题:作为 Swing 应用程序的一部分,我有一个组合框需要放入固定空间。但是,它的内容可能很长。我希望盒子本身是固定大小的,截断内容。但是,当单击向下箭头时,我希望它的行为类似于 HTML 选择,并显示一个足够长的框以容纳最长的条目作为下拉菜单。ListCellRenderer 可能是这种方式;我不知道。

也可能是我需要某种 javax.swing.plaf.basic.ComboPopup 和我自己的 ComboBoxUI 的实现。我已经深入研究了 SwingUtilities 代码以了解 DefaultListCellRenderer 如何进行计算。它使用 JLabel,BasicLabelUI 调用 SwingUtilities.layoutCompoundLabel(最终在调用堆栈中)进行剪辑。BasicComboPopup,我正在研究的 Java 6 代码中 ComboPopup 的唯一实现,似乎委托给:JList.computeVisibleRect(Component c, Rectangle visibleRect)

有没有人这样做过?任何指针?

4

2 回答 2

1

这是一个很好的起点:)

SteppedComboBox – http://web.archive.org/web/20070607203953/http://www.crionics.com/products/opensource/faq/swing_ex/JComboBoxExamples1.html

于 2009-08-24T20:33:16.610 回答
1

匿名的 JComboBox,具有可变宽度的下拉菜单。请注意,这只是金属 LAF。

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;
import javax.swing.plaf.basic.*;

/**
 * @version 1.0 12/12/98
 * updated 2012-02-18 to include @Overrides and other Java needs
 */
class SteppedComboBoxUI extends MetalComboBoxUI {
  @SuppressWarnings("serial")
@Override
  protected ComboPopup createPopup() {
    BasicComboPopup popup = new BasicComboPopup( comboBox ) {

      @Override
    public void show() {
        Dimension popupSize = ((SteppedComboBox)comboBox).getPopupSize();
        popupSize.setSize( popupSize.width,
          getPopupHeightForRowCount( comboBox.getMaximumRowCount() ) );
        Rectangle popupBounds = computePopupBounds( 0,
          comboBox.getBounds().height, popupSize.width, popupSize.height);
        scroller.setMaximumSize( popupBounds.getSize() );
        scroller.setPreferredSize( popupBounds.getSize() );
        scroller.setMinimumSize( popupBounds.getSize() );
        list.invalidate();            
        int selectedIndex = comboBox.getSelectedIndex();
        if ( selectedIndex == -1 ) {
          list.clearSelection();
        } else {
          list.setSelectedIndex( selectedIndex );
        }            
        list.ensureIndexIsVisible( list.getSelectedIndex() );
        setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );

        show( comboBox, popupBounds.x, popupBounds.y );
      }
    };
    popup.getAccessibleContext().setAccessibleParent(comboBox);
    return popup;
  }
}


@SuppressWarnings("serial")
public class SteppedComboBox extends JComboBox {
  protected int popupWidth;

  public SteppedComboBox(ComboBoxModel aModel) {
    super(aModel);
    setUI(new SteppedComboBoxUI());
    popupWidth = 0;
  }

  public SteppedComboBox(final Object[] items) {
    super(items);
    setUI(new SteppedComboBoxUI());
    popupWidth = 0;
  }

  @SuppressWarnings("unchecked")
public SteppedComboBox(Vector items) {
    super(items);
    setUI(new SteppedComboBoxUI());
    popupWidth = 0;
  }


  public void setPopupWidth(int width) {
    popupWidth = width;
  }

  public Dimension getPopupSize() {
    Dimension size = getSize();
    if (popupWidth < 1) popupWidth = size.width;
    return new Dimension(popupWidth, size.height);
  }
}
于 2012-02-18T22:57:25.113 回答