我正在构建一个动态菜单系统供我们公司内部使用。
其中一项功能是我使用JButton
和ActionListener
打开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>
有什么想法吗?
此致 文森特