[英]Need help on itemStateChanged on JComboBox
I have three JComboBox, one for continents, one for countries and another for cities. 我有三个JComboBox,一个用于大洲,一个用于国家,另一个用于城市。 All data is loaded from a database.
所有数据均从数据库加载。
The first JComboBox has all continents. 第一个JComboBox遍及所有大洲。 When I select one of them, the second JComboBox is loaded with the respective countries.
当我选择其中一个时,第二个JComboBox会加载相应的国家/地区。 Until now works, because I added an "itemStateChanged".
直到现在有效,因为我添加了“ itemStateChanged”。
However, when I select a country, the event "itemStateChanged" is called again. 但是,当我选择一个国家时,将再次调用事件“ itemStateChanged”。 What makes my second combo box stay with the first item selected, or (if I do the "RemoveAllItems") duplicates its content?
是什么使我的第二个组合框停留在选择的第一个项目上,或者(如果我执行“ RemoveAllItems”)重复了其内容?
There is no way in which it created a "itemStateChanged" for each JComboBox? 没有办法为每个JComboBox创建一个“ itemStateChanged”吗? Of the kind .NET, in which it is possible to create a "SelectedIndexChanged" for each combo box?
在.NET中,可以在其中为每个组合框创建“ SelectedIndexChanged”吗?
Some parts of my code: 我的代码的某些部分:
public class MainFrame extends JFrame implements ItemListener
{
…
private String iddistrito="VAZIO!!!";
private String idmunicipio="VAZIO!!!";
private boolean limpa=false;
private boolean populardistritos=false;
private boolean popularmunicipios=false;
private JComboBox cbFreguesiacliente = new JComboBox();
private JComboBox cbmunicipiocliente = new JComboBox();
private JComboBox cbdistritocliente = new JComboBox();
…
private void jbInit() throws Exception {
…
cbmunicipiocliente.addItemListener(this);
cbdistritocliente.addItemListener(this);
…
}
This part of code is the content when i click on the button, to enable my components: 这部分代码是单击按钮以启用组件时的内容:
private void btnNovocliente_actionPerformed(ActionEvent e) {
populardistritos = true;
cbdistritocliente.removeAllItems();
preenchecbdistritos();
populardistritos = false;
}
This piece of code fills the contents of the first JComboBox: 这段代码填充了第一个JComboBox的内容:
private void preenchecbdistritos(){
query = "select Distrito from distritos;";
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL);
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery(query);
StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = resultset.getMetaData();
int numberOfColumns = metaData.getColumnCount();
cbdistritocliente.addItem("");
while (resultset.next()) {
for (int i =1; i<= numberOfColumns; i++) {
if (metaData.getColumnName(i).equals("Distrito")) {
cbdistritocliente.addItem(resultset.getObject(i).toString());
}
}
}
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this,"rebentou no 1º catch " + e.toString(),"Inane error", JOptionPane.ERROR_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(this,"rebentou no 2º catch " + e.toString(),"Inane error", JOptionPane.ERROR_MESSAGE);
}
}
This is the famous event, which would like to get individually for each JComboBox: 这是著名的事件,每个JComboBox都希望单独获得:
public void itemStateChanged(ItemEvent e) {
if (populardistritos == false) {
if (limpa == false) {
if ((!cbdistritocliente.getSelectedItem().toString().equals("")) && (idmunicipio.equals("VAZIO!!!")))
{
query = "select id_distrito from distritos where distrito = '" + cbdistritocliente.getSelectedItem().toString() + "';";
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL);
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery(query);
StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = resultset.getMetaData();
int numberOfColumns = metaData.getColumnCount();
while (resultset.next()) {
for (int i =1; i<= numberOfColumns; i++) {
if (metaData.getColumnName(i).equals("id_distrito")) {
iddistrito = resultset.getObject(i).toString();
}
}
}
preenchecbmunicipios();
} catch (ClassNotFoundException e2) {
} catch (SQLException e2) {
}
}
}
if (!cbmunicipiocliente.getSelectedItem().toString().equals(""))
{
query = "select id_municipio from municipios where municipio = '" + cbmunicipiocliente.getSelectedItem().toString() + "';";
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL);
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery(query);
StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = resultset.getMetaData();
int numberOfColumns = metaData.getColumnCount();
while (resultset.next()) {
for (int i =1; i<= numberOfColumns; i++) {
if (metaData.getColumnName(i).equals("id_municipio")) {
idmunicipio = resultset.getObject(i).toString();
}
}
}
} catch (ClassNotFoundException e2) {
} catch (SQLException e2) {
}
}
}
}
This piece of code fills the contents of the second JComboBox: 这段代码填充了第二个JComboBox的内容:
private void preenchecbmunicipios(){
if (!iddistrito.equals("VAZIO!!!")) {
query = "select municipio from municipios where municipios.id_distrito = " + iddistrito + ";";
populardistritos=true;
//cbmunicipiocliente.removeAllItems();
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DATABASE_URL);
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery(query);
StringBuffer results = new StringBuffer();
ResultSetMetaData metaData = resultset.getMetaData();
int numberOfColumns = metaData.getColumnCount();
cbmunicipiocliente.addItem("");
while (resultset.next()) {
for (int i =1; i<= numberOfColumns; i++) {
if (metaData.getColumnName(i).equals("municipio")) {
cbmunicipiocliente.addItem(resultset.getObject(i).toString());
}
}
}
populardistritos=false;
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this,"rebentou no 1º catch do cbmunicipio com " + e.toString(),"Inane error", JOptionPane.ERROR_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(this,"rebentou no 2º catch do cbmunicipio com " + e.toString(),"Inane error", JOptionPane.ERROR_MESSAGE);
}
}
}
You may be able to leverage the approach shown here , in which a selection in the first combo replaces the model in a second combo. 您也许可以利用此处显示的方法,其中第一个组合中的选择将替换第二个组合中的模型。 In that example,
combo1
would hold continents, and the combo2
model would be set to the one containing countries on the selected continent. 在该示例中,
combo1
将容纳各大洲,并且combo2
模型将设置为包含所选大洲的国家/地区的模型。 The example uses an array, but a List<ComboBoxModel<String>>
might be a more flexible choice. 该示例使用数组,但是
List<ComboBoxModel<String>>
可能是更灵活的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.