我正在尝试学习 Java,并且已经从 PHP 走了很长一段路,我在创建代码时尝试应用相同的心态。但正如你们中的许多人已经知道的那样,它并没有那么容易奏效。
话虽如此,我有一个问题。我想从 ArrayList
中的项目创建一个下拉列表。我喜欢使用 JOptionPane.showInputDialog()
方法来尝试这个想法。
这是我目前拥有的,但我收到一条错误消息,告诉我没有找到适合 showInputDialog 的方法
ArrayList<String> projectList = new ArrayList<String>();
while(results.next())
projectList.add(results.getString("project"));
String inputDialog = (String)JOptionPane.showInputDialog(this, "Choose project to open", "Open Project", JOptionPane.PLAIN_MESSAGE, null, projectList, "--");
我知道问题是当我将 ArrayList
作为数组对象传递时,我抛出了这个错误。但是如果我做了类似的事情
Object[] projectList = {"one", "two"};
然后它按预期工作,然后我尝试执行此操作并将 projects
作为我的数组对象传递。
Object[] projects = {projectList.toString()};
这有点管用,但随后输出看起来像 "one, two"
在下拉列表中作为 1 行项目。
最佳答案
如果需要,您可以使用 List.toArray() 将 ArrayList
转换为 Object[]
,即:
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
Object[] result = list.toArray();
关于java - ArrayList 和 JOptionPane.showInputDialog(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16051342/