JCombobox、编辑器和渲染器相关

发布于 2024-08-16 11:35:20 字数 971 浏览 3 评论 0原文

作为 JCombobox ListCellRenderer,我有一个这样的类:

class ZComboBoxRenderer extends JPanel implements ListCellRenderer{
private ZGrid grid;
public ZComboBoxRenderer(ZGrid grid) {
    setLayout(new BorderLayout());
    this.grid = grid;
    add(new JScrollPane(grid), BorderLayout.CENTER);
}
public ZGrid getGrid(){
    return grid;
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
    grid.fetchSQL();
    return this;
}
}

这里的 ZGrid 扩展了 JTable

作为 ListCellRendererComponent,我向 JCombobox 提供一个内部有 ZGrid 的 JPanel。问题是,在它的列表中,这个 ZGrid 正在正确绘制。但它也在 JCombobox 的编辑器内进行绘制。我上传了一张图片以更好地展示这一点。

有没有办法将编辑器与列表分开?


替代文本 http://img444.imageshack.us/img444/564/soex.jpg< /a>

As a JCombobox ListCellRenderer, I have a class like this one:

class ZComboBoxRenderer extends JPanel implements ListCellRenderer{
private ZGrid grid;
public ZComboBoxRenderer(ZGrid grid) {
    setLayout(new BorderLayout());
    this.grid = grid;
    add(new JScrollPane(grid), BorderLayout.CENTER);
}
public ZGrid getGrid(){
    return grid;
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
    grid.fetchSQL();
    return this;
}
}

ZGrid here, extends JTable.

As a ListCellRendererComponent, I provide a JPanel which has a ZGrid inside, to the JCombobox. The problem is, in its list, this ZGrid is painting properly. But it is also being painted inside the Editor of JCombobox. I have uploaded an image to show this better.

Is there a way to separate Editor from List?


alt text http://img444.imageshack.us/img444/564/soex.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2024-08-23 11:35:20

据我了解,您正在为 JComboBox 实现一个自定义渲染器,尽管它正确地渲染了下拉列表的内容,但它完全弄乱了组合框的当前值。

我看到您可以使用两个选项:

  1. 您可以扩展 JComboBox 的 UI 组件并重写 paint 方法来获取当前网格的自定义表示形式价值观。这将是一个非常快速的概念证明,但它会带来问题,因为您需要扩展您希望应用程序运行的每个 UI(金属、Windows、Mac 等)。

  2. 您可以滚动自己的下拉列表,并使其看起来像 JComboBox。作为 POC,这也不是那么困难,但这里的复杂性是处理影响组合框周围的选择和导航的不同键盘输入。

From what I understand, you are implementing a custom Renderer for your JComboBox, and though it correctly renders the contents of your dropdown, it completely messes up the current value of the combo box.

I see two options at your disposal:

  1. you can extend the UI component for your JComboBox and override the paint method to get a custom representation of your grid for your current value view. This would be a pretty quick proof of concept, but it poses issues as you would need to extend every UI (metal, windows, mac, etc) that you expect your app to be running with.

  2. you can roll your own dropdown, and make it look like a JComboBox. This would not be that difficult to do as a POC as well, but the complexity here is to handle the different keyboard inputs that influence the selection and navigation around the combo box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文