I have two JComboBoxes; one removes all items in the other if it's already populated and then adds a new set of items, and the second fires an event that gets information from a database using the selected item. The problem occurs after the first combo box does its thing removing the items and adding new ones; when I select any of the items in the second JComboBox, the event that fires doesn't happen anymore.
我有两个JComboBox;如果已经填充了另一项,则删除另一项中的所有项目,然后添加一组新项目,第二项将使用所选项目触发从数据库获取信息的事件。在第一个组合框删除项目并添加新项目后,会出现问题;当我选择第二个JComboBox中的任何项目时,不再发生触发的事件。
Below I have provided snippets of my code:
下面我提供了我的代码片段:
The first combo box
第一个组合框
cmbIDs.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selection = (String)cmbIDs.getSelectedItem();
if (!(selection.equals("Select an username")))//current selection in combobox is stored as string
{
comboActivate(selection);
if (!unitC.getText().equals("")){
unitC.setText("");
}
if (!lecturer.getText().equals("")){
lecturer.setText("");
}
if (!(courseD.getText().equals("Not Enrolled"))){
populateUnits(selection);
}
}
else{
JOptionPane.showMessageDialog(null,"Please select a Surname.");
}
}
});
Removing the items inside populateUnits(String selectionID):
删除populateUnits(String selectionID)中的项:
try
{
units.removeAllItems();
units.addItem("Select a Unit");
}
catch (NullPointerException npe)
{
units.addItem("Select a Unit");
}
After this instructions are sent through a client to a server where a db is queried and the server replies with information which is then added to the second JComboBox. I assure you also that the items are added to the JComboBox after removeAllItems() is used.
在此指令通过客户端发送到查询数据库的服务器之后,服务器回复信息,然后将信息添加到第二个JComboBox。我还向您保证,在使用removeAllItems()之后,项目将添加到JComboBox中。
The second jComboBox:
第二个jComboBox:
units.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ue)
{
uSelect = (String)units.getSelectedItem();
if (!(uSelect.equals("Select a Unit")))//current selection in combobox is stored as string
{
System.out.println(uSelect);
unitActivate(uSelect);
}
else
{
JOptionPane.showMessageDialog(null,"Please select a Unit.");
}
}
});
1 个解决方案
#1
0
It looks like your code never gets a fresh set of items from the database, so the user can never select anything other than "Select an Unit", which your second code block ignores.
看起来您的代码永远不会从数据库中获取一组新的项目,因此用户永远不会选择“选择一个单元”以外的任何内容,这是您的第二个代码块忽略的。
#1
0
It looks like your code never gets a fresh set of items from the database, so the user can never select anything other than "Select an Unit", which your second code block ignores.
看起来您的代码永远不会从数据库中获取一组新的项目,因此用户永远不会选择“选择一个单元”以外的任何内容,这是您的第二个代码块忽略的。