JCombobox, Editor and Renderer related

孤街浪徒 提交于 2019-12-10 11:56:19

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/1966637/jcombobox-editor-and-renderer-related

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!