简体   繁体   English

JCombobox,编辑器和渲染器相关

[英]JCombobox, Editor and Renderer related

As a JCombobox ListCellRenderer, I have a class like this one: 作为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 here, extends JTable . ZGrid在这里扩展了JTable

As a ListCellRendererComponent , I provide a JPanel which has a ZGrid inside, to the JCombobox . 作为ListCellRendererComponent ,我提供一个JPanel具有ZGrid内,到JCombobox The problem is, in its list, this ZGrid is painting properly. 问题是,在其列表中,此ZGrid正确绘制。 But it is also being painted inside the Editor of JCombobox. 但是它也在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 替代文字http://img444.imageshack.us/img444/564/soex.jpg

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. 据我了解,您正在为JComboBox实现自定义Renderer,尽管它正确地呈现了下拉菜单的内容,但它完全弄乱了组合框的当前值。

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. 您可以扩展JComboBox的UI组件,并重写paint方法以获得当前值视图的网格自定义表示。 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. 这将是一个非常快速的概念证明,但由于您需要扩展希望与应用程序一起运行的每个UI(金属,窗口,mac等),因此会带来一些问题。

  2. you can roll your own dropdown, and make it look like a JComboBox . 您可以滚动自己的下拉菜单,并使它看起来像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. 作为POC并没有那么困难,但是这里的复杂度是处理影响组合框选择和导航的不同键盘输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM