Вместо добавления JTree
к JComboBox
, вы можете добавить DefaultMutableTreeNode
и использовать TreeCellRenderer
, чтобы нарисовать ячейки JComboBox
.
import java.awt.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
public final class MainPanel {
private Component makeUI() {
DefaultMutableTreeNode firstRoot = new DefaultMutableTreeNode("First Root Node");
DefaultMutableTreeNode firstChild = new DefaultMutableTreeNode("First Child Node");
DefaultMutableTreeNode secondChild = new DefaultMutableTreeNode("Second Child Node");
firstRoot.add(firstChild);
firstRoot.add(secondChild);
DefaultMutableTreeNode secondRoot = new DefaultMutableTreeNode("Second Root Node");
DefaultMutableTreeNode childOfSecondNode = new DefaultMutableTreeNode("Child of Second Node");
secondRoot.add(childOfSecondNode);
DefaultMutableTreeNode hiddenRoot = new DefaultMutableTreeNode("Root");
hiddenRoot.add(firstRoot);
hiddenRoot.add(secondRoot);
JPanel p = new JPanel(new BorderLayout());
p.add(new TreeComboBox(new DefaultTreeModel(hiddenRoot)), BorderLayout.NORTH);
p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new MainPanel().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class TreeComboBox extends JComboBox<DefaultMutableTreeNode> {
public TreeComboBox(TreeModel model) {
super();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
DefaultComboBoxModel<DefaultMutableTreeNode> m = new DefaultComboBoxModel<>();
Collections.list((Enumeration<?>) root.preorderEnumeration()).stream()
.filter(DefaultMutableTreeNode.class::isInstance)
.map(DefaultMutableTreeNode.class::cast)
.filter(n -> !n.isRoot())
.forEach(m::addElement);
setModel(m);
}
@Override
public void updateUI() {
super.updateUI();
JTree dummyTree = new JTree();
TreeCellRenderer renderer = dummyTree.getCellRenderer();
ListCellRenderer<? super DefaultMutableTreeNode> r = getRenderer();
setRenderer((list, value, index, isSelected, cellHasFocus) -> {
Component c = r.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value == null) {
return c;
}
if (index < 0) {
String txt = Arrays.stream(value.getPath())
.filter(DefaultMutableTreeNode.class::isInstance)
.map(DefaultMutableTreeNode.class::cast)
.filter(n -> !n.isRoot())
.map(Objects::toString)
.collect(Collectors.joining(" / "));
((JLabel) c).setText(txt);
return c;
} else {
boolean leaf = value.isLeaf();
JLabel l = (JLabel) renderer.getTreeCellRendererComponent(
dummyTree, value, isSelected, true, leaf, index, false);
int indent = Math.max(0, value.getLevel() - 1) * 16;
l.setBorder(BorderFactory.createEmptyBorder(1, indent + 1, 1, 1));
return l;
}
});
}
}