在选择国家字段时,会显示州字段,并且在选择州字段时,会显示城市字段
但是在下面的代码中,当我第一次选择它时它可以正常工作,但是当我选择另一个国家时,它的州会被添加到前一个国家的州中,而城市的情况也是如此。我阅读了有关 JComboBox.RemoveAll() 和 DefaultComboBoxModel 类的信息,但不知道如何在当前情况下使用它。
那么,基本上如何重置 JComboBox 项?
看看public countryCode{ } 类,
private void stateBoxActionPerformed,
private void countryBoxActionPerformed 函数
回答:这对我有用的是 stateBox.removeAllItems() 在我们从数据库中添加状态的代码之前。
这是 NetBeans 的代码 --->
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author System
*/
public class countryCode extends javax.swing.JFrame {
DefaultComboBoxModel d1=new DefaultComboBoxModel();
Connection con;
String code,q;
PreparedStatement ps;
ResultSet rs;
/**
* Creates new form countryCode
*/
public countryCode() {
initComponents();
createConnection();
}
void createConnection()
{
try
{
Class.forName("oracle.jdbc.OracleDriver");//Connection String
String r1="jdbc:oracle:thin:@localhost:1521:";//Open Connection
con=DriverManager.getConnection(r1,"system","password");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,ex);
ex.printStackTrace();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
countryBox = new javax.swing.JComboBox();
country = new javax.swing.JLabel();
stateBox = new javax.swing.JComboBox();
cityBox = new javax.swing.JComboBox();
state = new javax.swing.JLabel();
city = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
countryBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "America", "India", "Nepal", "Pakistan", "Sri Lanka", " ", " " }));
countryBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
countryBoxActionPerformed(evt);
}
});
country.setText("Country");
stateBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
stateBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stateBoxActionPerformed(evt);
}
});
state.setText("State");
city.setText("City");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(country, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
.addComponent(state, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(city, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cityBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(stateBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(countryBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 198, Short.MAX_VALUE))
.addGap(95, 95, 95))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(countryBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(country))
.addGap(18, 18, 18)
.addComponent(stateBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(state))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cityBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(city))
.addContainerGap(154, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void countryBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_countryBoxActionPerformed
try {
//GET STRING
String coname;
coname = (String)countryBox.getSelectedItem();
//STRING QUERY
q="select * from country where cname=? ";
//PREPARE STATEMENT
ps=con.prepareStatement(q);
//SET STRING
ps.setString(1,coname);
rs=ps.executeQuery();//RESULTSET
while(rs.next())
{d1.getClass();
code=rs.getString("cocode");
break;
}
/* ******************* <!------ANOTHER QUERY------> ****************************** */
//STRING QUERY
q="select * from state where cocode=? ";
//PREPARE STATEMENT
ps=con.prepareStatement(q);
//SET STRING
ps.setString(1,code);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
String code=rs.getString("stname");
stateBox.addItem(code);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_countryBoxActionPerformed
private void stateBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stateBoxActionPerformed
try {
/*---------NOW QUERY TO GET STCODE I.E STATECODE REPEAT HERE STEPS WHAT WE DID IN COUNTRY -----------*/
//GET STRING
String stname;
stname = (String)stateBox.getSelectedItem();
//STRING QUERY
q="select * from state where stname=? ";
//PREPARE STATEMENT
ps=con.prepareStatement(q);
//SET STRING
ps.setString(1,stname);
rs=ps.executeQuery();//RESULTSET
while(rs.next())
{
code=rs.getString("stcode");
break;
}
/* ******************* <!------ANOTHER QUERY------> ****************************** */
//STRING QUERY
q="select * from city where stcode=? ";
//PREPARE STATEMENT
ps=con.prepareStatement(q);
//SET STRING
ps.setString(1,code);
rs=ps.executeQuery();
while(rs.next())
{
code=rs.getString("ciname");
cityBox.addItem(code);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_stateBoxActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(countryCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(countryCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(countryCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(countryCode.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new countryCode().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel city;
private javax.swing.JComboBox cityBox;
private javax.swing.JLabel country;
private javax.swing.JComboBox countryBox;
private javax.swing.JLabel state;
private javax.swing.JComboBox stateBox;
// End of variables declaration//GEN-END:variables
}