在另一个菜单项上执行setText后,JComboBox停止工作

时间:2011-12-15 02:00:01

标签: java swing jcombobox jtextfield

我正在构建一个动态菜单系统供我们公司内部使用。

其中一项功能是我使用JButtonActionListener打开JFileChooser来选择文件。当您选择文件并点击APPROVE_OPTION时,我会使用所选文件的路径更新JTextField旁边的JButton的内容:

jtf_hex.setText(jfc.getSelectedFile().getPath());

这样可以正常使用,但出于某种原因,在setText上执行JTextField可以完全阻止我网页上的其他元素工作,JComboBox

我已经进行了一系列测试,并且已经确定,当且仅当我执行setText时,JComboBox才会停止工作。

以下是一些代码信息来展示我在做什么:

optionspane = new JPanel(new FlowLayout(FlowLayout.LEFT));
optionspane.setLayout(new GridLayout(0,1));

Panel rp = new Panel(new FlowLayout(FlowLayout.LEFT));
optionspane.add(rp);
// Create the uneditable text box
jtf_hex = new JTextField("", 20);
jtf_hex.setEditable(false);
rp.add(jtf_hex);

// Create a file select button and add it to the panel.
fwHexFileButton = new JButton("Select File");
fwHexFileButton.addActionListener(this);    // Add action listener so something will happen when user clicks on the button.
rp.add(fwHexFileButton);                    // Add the button to the panel.

rp = new Panel(new FlowLayout(FlowLayout.LEFT));        // Create the next row
optionspane.add(rp);                        // Add the row to the window (grid).
rp.add(new JLabel("microSpider device type:"));
jdevicetypes = new JComboBox(deviceTypes());// Create dropdown box of device types.
jdevicetypes.setSelectedIndex(0);           // Default to the first entry in the drop-down.
rp.add(jdevicetypes);

..然后在我的动作监听器中:                 JComponent buttonObj =(JComponent)event.getSource();

    // Handle any on-page events:

    if(buttonObj == (JComponent)fwHexFileButton)
    {
        jfc = new JFileChooser(new File("."));
        if(jtf_hex.getText().length() > 0)
        {   // If we already have a file selected, default to opening file chooser in this location.
            jfc.setCurrentDirectory(new File(jtf_hex.getText()));
        }
        jfc.addChoosableFileFilter(new JtregFilter());
        int retVal = jfc.showDialog(null, "Select");
        if (retVal == JFileChooser.APPROVE_OPTION)
        {
            jtf_hex.setText(jfc.getSelectedFile().getPath());
        }
    }

注意:JComboBox完全正常工作,直到执行setText。如果我打开JFileChooser并选择Cancel,则JComboBox会继续有效。 如果我在点击APPROVE后执行其他操作,即:显示一些警告框,那么它可以正常工作 - 似乎出于某种原因JTextField.setText()核武JComboBox功能。

注意2:执行setText后面板上的所有其他元素继续正常工作。但是我没有任何其他JComboBox元素 - 只是一堆复选框,按钮等等。

注意3:我的JComboBox没有ActionListener,因为我认为不需要。{/ p>

有什么想法吗?

此致 文森特

0 个答案:

没有答案