views:

324

answers:

2

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

A: 

Here is an example of using a JComboBox as an editor it a JTable. Here is a similar example using a custom JCheckBox.

trashgod
Thanks for the answer, but I'm not trying to use JComboBox as a cell editor, I am interested in using a JTable derivative as JComboBox's ListCellRenderer, and seperating the editor from it.
Erkan Haspulat
Ah, I see. You may be able to implement a suitable ComboBoxEditor to complement your existing ListCellRenderer.
trashgod
A: 

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.

akf
It's been a while since I asked this question, and I have changed my design about this problem. Thank you for the answer though.
Erkan Haspulat