如何更改 JComboBox 下拉列表的宽度? [英] How can I change the width of a JComboBox dropdown list?

查看:56
本文介绍了如何更改 JComboBox 下拉列表的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可编辑的 JComboBox,其中包含一个单字母值列表.因此,组合框非常小.

I have an editable JComboBox which contains a list of single letter values. Because of that the combobox is very small.

每个字母都有一个特殊的含义,对于很少使用的字母,用户有时会不清楚.因此,我创建了一个自定义 ListCellRenderer,它显示了下拉列表中每个字母的含义.

Every letter has a special meaning which sometimes isn't clear to the user in case of rarely used letters. Because of that I've created a custom ListCellRenderer which shows the meaning of each letter in the dropdown list.

不幸的是,这个解释不适合下拉菜单,因为它太小了,因为它与组合框的宽度相同.

Unfortunately this explanation doesn't fit into the dropdown because it is to small, because it has the same width as the combobox.

有没有办法让下拉列表比组合框更宽?

Is there any way to make the dropdown list wider than the combobox?

这就是我想要实现的:

 ---------------------
| Small JCombobox | V |
 --------------------------------------------
| "Long item 1"                              |
 --------------------------------------------
| "Long item 2"                              |
 --------------------------------------------
| "Long item 3"                              |
 --------------------------------------------

我无法更改组合框的宽度,因为该应用程序是旧遗留应用程序的重新创建,其中某些内容必须与以前完全一样.(在这种情况下,组合框必须不惜一切代价保持小尺寸)

I cannot change the width of the combobox because the application is a recreation of an old legacy application where some things have to be exactly as they were before. (In this case the combobox has to keep it's small size at all costs)

推荐答案

我相信使用公共 API 做到这一点的唯一方法是编写自定义 UI(有 两个 错误 处理此问题.

I believe the only way to do this with the public API is to write a custom UI (there are two bugs dealing with this).

如果你只是想要一些快速而肮脏的东西,我发现这种使用实现细节的方法来做到这一点(此处):

If you just want something quick-and-dirty, I found this way to use implementation details to do it (here):

public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
    JComboBox box = (JComboBox) e.getSource();
    Object comp = box.getUI().getAccessibleChild(box, 0);
    if (!(comp instanceof JPopupMenu)) return;
    JComponent scrollPane = (JComponent) ((JPopupMenu) comp).getComponent(0);
    Dimension size = new Dimension();
    size.width = box.getPreferredSize().width;
    size.height = scrollPane.getPreferredSize().height;
    scrollPane.setPreferredSize(size);
    //  following line for Tiger
    // scrollPane.setMaximumSize(size);
}

将其放入 PopupMenuListener可能适合你.

或者您可以使用第一个链接错误中的代码:

Or you could use the code from the first linked bug:

class StyledComboBoxUI extends BasicComboBoxUI {
  protected ComboPopup createPopup() {
    BasicComboPopup popup = new BasicComboPopup(comboBox) {
      @Override
      protected Rectangle computePopupBounds(int px,int py,int pw,int ph) {
        return super.computePopupBounds(
            px,py,Math.max(comboBox.getPreferredSize().width,pw),ph
        );
      }
    };
    popup.getAccessibleContext().setAccessibleParent(comboBox);
    return popup;
  }
}

class StyledComboBox extends JComboBox {
  public StyledComboBox() {
    setUI(new StyledComboBoxUI());
  }
}

这篇关于如何更改 JComboBox 下拉列表的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆