How to make a combobox, inside a jtree, show its menu?
java, jcombobox, jtree, swing
Solution
It basically opened the menu but when I selected one of the items it replaced the JLabel so only the combobox was visible.
Well that is what you'd excpect as thats how the `DefaultCellEditor(JComboBox jcb)` is meant to be:
import java.awt.BorderLayout;
import java.util.Properties;
import javax.swing.*;
import javax.swing.tree.TreeCellEditor;
public class TreeEditJComboBox {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Properties props = System.getProperties();
JTree tree = new JTree(props);
JComboBox comboBox = new JComboBox(new String[]{"A", "B", "C"});
TreeCellEditor editor = new DefaultCellEditor(comboBox);
tree.setEditable(true);
tree.setCellEditor(editor);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
}
You could try making your own `DefaultCellEditor` and override `getTableCellEditorComponent()` and then return a `JPanel` which holds the `JLabel` and `JComboBox`, something like:
class MyDefaultCellEditor extends DefaultCellEditor {
public MyDefaultCellEditor(JComboBox comboBox) {
super(comboBox);
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
//return custom coponent
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
then:
TreeCellEditor editor = new MyDefaultCellEditor(blockedAlternatives);
you may have to override a few other methods too. I was just showing the logic
References:
Creating a DefaultCellEditor: JComboBox
extends DefaultCellEditor
Problem
I basically have a JTree where I show certain information. In one of the "sub-trees" I got a panel which consists of a panel with GridLayout(0,2) and a JPanel as well as a combobox. I've noticed that no components in my tree react on input. This of course means that my combobox won't react when I try to click on it. I tried to implement a default cell editor, which worked but not like I wanted to. It basically opened the menu but when I selected one of the items it replaced the JLabel so only the combobox was visible. Pictures Before clicking on the box After clicking on the box Code that I tried with ``` TreeCellEditor editor = new DefaultCellEditor(blockedAlternatives); infoTree.setEditable(true); infoTree.setCellEditor(editor); ``` I obviously don't want to be able to edit the whole tree, I just want to be able to show the combobox's menu. I just took this code from the web for testing. Any ideas?