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

时间:2021-04-20 12:33:44

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.

我基本上有一个JTree,我在那里显示某些信息。在其中一个“子树”中,我得到了一个面板,其中包含一个带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 如何在jtree中制作组合框,显示其菜单?

在点击框之前

After clicking on the box 如何在jtree中制作组合框,显示其菜单?

点击后框

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?

我显然不希望能够编辑整个树,我只是想能够显示组合框的菜单。我刚把这个代码从网上拿来进行测试。有任何想法吗?

1 个解决方案

#1


4  

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(),然后返回一个包含JLabel和JComboBox的JPanel,类似于:

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:

参考文献:

#1


4  

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(),然后返回一个包含JLabel和JComboBox的JPanel,类似于:

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:

参考文献: