我是 Java 初学者,我的屏幕上已经有一个 JComboBox 控件。
在我的代码中,我试图从文件中读取数据并用它填充组合框。
我已经浏览了此处搜索的文档和示例,并尝试了以下两种方法来执行此操作:
我尝试过:
jComboBox1.removeAllItems();
jComboBox1.addItem("test string");
然后我尝试这样做:
jComboBox1.removeAllItems();
jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "test", "string" }));
运行程序时没有出现任何错误,但组合框数据没有发生任何变化,它只包含项目 1、项目 2、项目 3、项目 4 的默认值。
有人可以帮我解决我所缺少的东西吗?
已编辑 - 这是该过程的代码。该过程是从 public static void main(String args[]) 过程调用的,该过程似乎在程序启动时运行。打印到系统输出的内容显示出来,因此我知道代码块正在运行,并且没有生成错误。
第二次编辑 - 我认为问题出在组合框上,但我尝试更改标签或文本字段值,但它们也不起作用。我已经更新了代码以显示有关它的过程,我可以在其中更新标签和文本字段,但在下面的过程中它不起作用。与系统生成的程序相比,由于某种原因,我在我创建的程序中一定没有正确引用控件吗?
第三次编辑 - 好的,进行更多测试,这就是我调用第二个过程的方式,这就是问题所在。如果我从第一个程序运行第二个程序,一切都会正常。我在 JFrame 启动时运行了下面的过程,这就是问题所在。现在正在尝试找出解决方法。
NewJFrame test = new NewJFrame();
test.LoadCmbData();
private void cmdStartActionPerformed(java.awt.event.ActionEvent evt) {
if (cmdStart.getText()=="Start Game")
{
//randVar.nextInt();
//strWord=strWords[Integer.valueOf(cmbRnd.getSelectedItem().toString())];
txtOutput.setText("");
txtOutput.setEnabled(true);
txtInput.setEnabled(true);
cmdInput.setEnabled(true);
cmdStart.setText("End Game");
lblStatus.setText("Enter your first guess and click 'Check'");
intTries=0;
//jComboBox1.addItem("test string");
//lblStatus.setText("Click 'Start Game' button to begin");
}
else
{
cmdStart.setText("Start Game");
txtInput.setText("");
txtInput.setEnabled(false);
cmdInput.setEnabled(false);
//txtOutput.setText("");
txtOutput.setEnabled(false);
lblStatus.setText("Click 'Start Game' button to begin");
}
}
public void LoadCmbData()
{
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);
// Load word choices from file
try
{
File readfile = new File(s + "\\words.txt");
ArrayList linedata = readfile.OpenFile();
System.out.println("ArrayList populated");
// NewJFrame test = new NewJFrame();
// test.cmbRnd.removeAllItems();
// test.cmbRnd.addItem("test");
// test.cmbRnd.setModel(new DefaultComboBoxModel(new String[] { "test", "string", "test", "string", "one" }));
// //test.setModel(new DefaultComboBoxModel(linedata.toArray()));
// test.cmbRnd.updateUI();
// test.cmbRnd.revalidate();
// test.cmbRnd.repaint();
lblStatus.setText("test ");
txtInput.setEnabled(true);
txtInput.setText("test");
jComboBox1.removeAllItems();
jComboBox1.addItem("test string");
jComboBox1.updateUI();
jComboBox1.revalidate();
jComboBox1.repaint();
//NewJFrame test = new NewJFrame();
// test.repaint();
// test.revalidate();
//jComboBox1.removeAllItems();
//jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "test", "string" }));
//jComboBox1.updateUI();
//jComboBox1.revalidate();
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
谢谢!
最佳答案
我终于明白自己做错了什么了。作为一个完全的新手,我从“public static void main(String args[])”过程中调用该过程,认为这是加载代码的位置,因为它在我执行一些 System.out 消息时起作用。
回溯我的问题并找出上述原因后,我现在知道将该过程放入我现在已经了解的“构造函数”方法中。
我仍然不知道为什么它在其他位置不起作用,因为它显然运行了该过程,只是不更新控件。如果有人愿意向我解释这一点,那将会很有帮助。
谢谢!
关于java - 在 Java 中向 JComboBox 添加项目的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34321379/