[英]Populate combo box with table values
EDIT(NEW): 编辑(新):
Still haven't figured out how to populate the cboxCustomers
. 仍然没有弄清楚如何填充cboxCustomers
。 I've been at it for the past day or two, but without luck. 我在过去的一两天里一直在这里,但没有运气。
In case anyone can help: http://pastebin.com/e5wibRYw 万一有人可以提供帮助: http : //pastebin.com/e5wibryw
I went from cats
to customers
, btw. 顺便说一下,我从cats
到customers
。
I tried Mr. Xymon's
approach, but didn't implement it correctly since it didn't work. 我尝试Mr. Xymon's
方法,但没有正确实现,因为它不起作用。
Whatever event I used to handle the population I always got NullPointerException
for whatever control/event I was trying to use. 无论我用什么事件处理人口,我总是得到NullPointerException
用于我试图使用的任何控件/事件。
OLD: 旧:
There's a
JForm
.
有一个
JForm
。
On it, there's a single combo box.
在它上面,有一个组合框。
Also have a single table with cats -
cats
.
也有猫单个表-
cats
。
Each cat has
id
, and
catName
.
每只猫都有
id
和
catName
。
What I wanted to do was when I click on the combo box, thus expanding it, populate it with all id
of cats that are found in cats
table. 我想要做的是当我点击组合框,然后扩展它,用cats
表中找到的所有猫的id
填充它。
SLOVED. SLOVED。 Asnwer below. Asnwer在下面。 Unfortunately I receive numerous unreported exception java.sql.SQLException
from the lines I've indicated with >
: 不幸的是,我收到了许多unreported exception java.sql.SQLException
来自我用>
表示的行:
private void cboxCatsMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
// create an array list to be filled with cat's ids
ArrayList<String> cats = new ArrayList<String>();
String query = "SELECT id FROM cats ORDER BY id";
>java.sql.PreparedStatement stm = connection.prepareStatement(query);
>ResultSet rs = stm.executeQuery(query);
>while(rs.next()){
>String cat = rs.getString("id");
// add cat's ids tp the array list
cats.add(cat);
}
>rs.close();
// populate the combo box
DefaultComboBoxModel model = new DefaultComboBoxModel(cats.toArray());
cboxCats.setModel(model);
}
OLD ANSWER: 老答案:
I think I fixed it. 我想我修好了。 I just had to wrap all of the highlighted lines of code together into one big try-catch
statement that would catch SQLException
. 我只需将所有突出显示的代码行包装在一个大的try-catch
语句中,该语句将捕获SQLException
。 Problem is - the combo box doesn't get populated with id
values when I expand it. 问题是 - 当我展开它时,组合框不会填充id
值。 Why is that? 这是为什么? Am I using the wrong event? 我使用了错误的事件吗?
Isn't better to populate your combo box with cat's name instead of the id? 用cat的名称而不是id来填充你的组合框是不是更好? I came up with a different solution by directly adding field value into model instead of using ArrayList. 我通过直接将字段值添加到模型而不是使用ArrayList来提出不同的解决方案。 You have to perform it within the constructor to populate the combo box upon loading the form. 您必须在构造函数中执行它以在加载表单时填充组合框。
DefaultComboBoxModel list = new DefaultComboBoxModel();
JComboBox cbo_cats = new JComboBox(list);
// at constructor or a user-defined method that's called from constructor
try{
// assume that all objects were all properly defined
s = con.createStatement();
s.executeQuery("SELECT * FROM cats ORDER BY catName");
rs = s.getResultSet();
while(rs.next()){
//int id = rs.getInt("id");
//list.addElement(id);
String c = rs.getString("catName");
list.addElement(c);
}
}catch(Exception err){
System.out.println(err);
}
As you can see I didn't use prepared statements but you can easily change that. 正如您所看到的,我没有使用预准备语句,但您可以轻松地更改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.