简体   繁体   English

如何在jtree中制作组合框,显示其菜单?

[英]How to make a combobox, inside a jtree, show its menu?

I basically have a JTree where I show certain information. 我基本上有一个JTree,我在那里显示某些信息。 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. 在其中一个“子树”中,我得到了一个面板,其中包含一个带有GridLayout(0,2)的面板和一个JPanel以及一个组合框。

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. 它基本上打开了菜单,但当我选择其中一个项目时,它取代了JLabel,因此只有组合框可见。

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? 有任何想法吗?

It basically opened the menu but when I selected one of the items it replaced the JLabel so only the combobox was visible. 它基本上打开了菜单,但当我选择其中一个项目时,它取代了JLabel,因此只有组合框可见。

Well that is what you'd excpect as thats how the DefaultCellEditor(JComboBox jcb) is meant to be: 那就是你所怀疑的,就像DefaultCellEditor(JComboBox jcb)的意思一样:

    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: 您可以尝试制作自己的DefaultCellEditor并覆盖getTableCellEditorComponent() ,然后返回一个包含JLabelJComboBoxJPanel ,类似于:

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: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM