JCombobox, Editor and Renderer related

2019-06-13 18:58发布

问题:

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.