gpt4 book ai didi

java - 需要有关 JComboBox 上的 itemStateChanged 的​​帮助

转载 作者:行者123 更新时间:2023-12-01 18:55:54 24 4
gpt4 key购买 nike

我有三个 JComboBox,一个用于大陆,一个用于国家,另一个用于城市。所有数据均从数据库加载。

第一个 JComboBox 包含所有大陆。当我选择其中之一时,第二个 JComboBox 会加载相应的国家/地区。到目前为止都有效,因为我添加了“itemStateChanged”。

但是,当我选择一个国家/地区时,会再次调用事件“itemStateChanged”。是什么使我的第二个组合框与选定的第一个项目保持一致,或者(如果我执行“RemoveAllItems”)重复其内容?

有没有办法为每个 JComboBox 创建一个“itemStateChanged”?在 .NET 中,可以为每个组合框创建一个“SelectedIndexChanged”?

我的代码的某些部分:

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);

}

这部分代码是我单击按钮时的内容,以启用我的组件:

private void btnNovocliente_actionPerformed(ActionEvent e) {
populardistritos = true;
cbdistritocliente.removeAllItems();
preenchecbdistritos();
populardistritos = false;
}

这段代码填充了第一个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);
}
}

这是著名的事件,希望为每个 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) {
}
}
}
}

这段代码填充了第二个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);
}
}
}

最佳答案

the docs告诉您该事件包含有关发起者的信息。因此只需检查 e.getItemSelectable().equals(firstSelectBox) 等即可。

希望有帮助。

关于java - 需要有关 JComboBox 上的 itemStateChanged 的​​帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com