想实现选择第一个列表框中的选项,第二个列表框中自动出现该选项对应的子选项,如选了浙江,第二个列表框就出现杭州,温州 所有数据都在数据库了,现在第一个列表框成功读取了,但是第二个列表框没有子选项,麻烦大家看看
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.event.*;
import java.util.*; public class DelCategorySecond extends JFrame implements ActionListener,ItemListener{
int index;
int index1;
int flag=0;
JLabel lb=new JLabel("选择主类:");
JLabel lb1=new JLabel("二级类:");
JButton btOk=new JButton("确定");
JButton btCancle=new JButton("取消");
String str=new String();
String str1=new String();
Vector item=new Vector();
Vector item1=new Vector();
JComboBox cb=new JComboBox();
JComboBox cb1=new JComboBox(); // 驱动程序名
String driver = "com.mysql.jdbc.Driver"; // URL指向要访问的数据库名sport
String url = "jdbc:mysql://localhost:3306/sport"; // MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "123"; public DelCategorySecond() {
try {
// 加载驱动程序
Class.forName(driver); // 连续数据库
Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!"); // statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from categories";
// 结果集
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
str=rs.getString("category");
item.addElement(str);
}
rs.close();
conn.close();
}
catch(ClassNotFoundException w) {
System.out.println("Sorry,can`t find the Driver!");
w.printStackTrace();
} catch(SQLException w) {
w.printStackTrace();
} catch(Exception w) {
w.printStackTrace();
}
cb=new JComboBox(item);
cb1=new JComboBox(item1);
setSize(330,300);
setResizable(false); //设置对话框大小固定
setLayout(null);
setTitle("删除二级类");
add(lb);
lb.setBounds(50,50,70,30);
add(cb);
cb.setBounds(140,50,130,30);
cb.addItemListener(this);
add(lb1);
lb1.setBounds(50,100,70,30);
add(cb1);
cb1.setBounds(140,100,130,30);
add(btOk);
btOk.setBounds(50,180,80,30);
add(btCancle);
btCancle.setBounds(180,180,80,30);
btOk.addActionListener(this);
btCancle.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btCancle)
dispose();
if(e.getSource()==btOk)
{
index=cb.getSelectedIndex();
index1=cb1.getSelectedIndex();
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password); // statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql1 = "delete from "+item.elementAt(index)+" where category='"+item1.elementAt(index1)+"'";
JOptionPane.showMessageDialog(null, "二级类:"+item1.elementAt(index1)+"已经删除!" ); //cb1.removeItem(cb1.getSelectedItem());
statement.executeUpdate(sql1);
conn.close();
}
catch(ClassNotFoundException w) {
System.out.println("Sorry,can`t find the Driver!");
w.printStackTrace();
} catch(SQLException w) {
w.printStackTrace();
} catch(Exception w) {
w.printStackTrace();
}
}
}
public void itemStateChanged(ItemEvent e)
{
index=cb.getSelectedIndex();
for(int i=0;i<=item.size();i++)
{
if(index==i)
{
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password); // statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql2 = "select * from "+item.elementAt(index);
ResultSet rs2= statement.executeQuery(sql2);
while(rs2.next()) {
str1=rs2.getString("category");
item1.addElement(str1);
}
cb1=new JComboBox(item1);
rs2.close();
} catch(ClassNotFoundException w) {
System.out.println("Sorry,can`t find the Driver!");
w.printStackTrace();
} catch(SQLException w) {
w.printStackTrace();
} catch(Exception w) {
w.printStackTrace();
}
} }
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.event.*;
import java.util.*; public class DelCategorySecond extends JFrame implements ActionListener,ItemListener{
int index;
int index1;
int flag=0;
JLabel lb=new JLabel("选择主类:");
JLabel lb1=new JLabel("二级类:");
JButton btOk=new JButton("确定");
JButton btCancle=new JButton("取消");
String str=new String();
String str1=new String();
Vector item=new Vector();
Vector item1=new Vector();
JComboBox cb=new JComboBox();
JComboBox cb1=new JComboBox(); // 驱动程序名
String driver = "com.mysql.jdbc.Driver"; // URL指向要访问的数据库名sport
String url = "jdbc:mysql://localhost:3306/sport"; // MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "123"; public DelCategorySecond() {
try {
// 加载驱动程序
Class.forName(driver); // 连续数据库
Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!"); // statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from categories";
// 结果集
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
str=rs.getString("category");
item.addElement(str);
}
rs.close();
conn.close();
}
catch(ClassNotFoundException w) {
System.out.println("Sorry,can`t find the Driver!");
w.printStackTrace();
} catch(SQLException w) {
w.printStackTrace();
} catch(Exception w) {
w.printStackTrace();
}
cb=new JComboBox(item);
cb1=new JComboBox(item1);
setSize(330,300);
setResizable(false); //设置对话框大小固定
setLayout(null);
setTitle("删除二级类");
add(lb);
lb.setBounds(50,50,70,30);
add(cb);
cb.setBounds(140,50,130,30);
cb.addItemListener(this);
add(lb1);
lb1.setBounds(50,100,70,30);
add(cb1);
cb1.setBounds(140,100,130,30);
add(btOk);
btOk.setBounds(50,180,80,30);
add(btCancle);
btCancle.setBounds(180,180,80,30);
btOk.addActionListener(this);
btCancle.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btCancle)
dispose();
if(e.getSource()==btOk)
{
index=cb.getSelectedIndex();
index1=cb1.getSelectedIndex();
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password); // statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql1 = "delete from "+item.elementAt(index)+" where category='"+item1.elementAt(index1)+"'";
JOptionPane.showMessageDialog(null, "二级类:"+item1.elementAt(index1)+"已经删除!" ); //cb1.removeItem(cb1.getSelectedItem());
statement.executeUpdate(sql1);
conn.close();
}
catch(ClassNotFoundException w) {
System.out.println("Sorry,can`t find the Driver!");
w.printStackTrace();
} catch(SQLException w) {
w.printStackTrace();
} catch(Exception w) {
w.printStackTrace();
}
}
}
public void itemStateChanged(ItemEvent e)
{
index=cb.getSelectedIndex();
for(int i=0;i<=item.size();i++)
{
if(index==i)
{
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password); // statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql2 = "select * from "+item.elementAt(index);
ResultSet rs2= statement.executeQuery(sql2);
while(rs2.next()) {
str1=rs2.getString("category");
item1.addElement(str1);
}
cb1=new JComboBox(item1);
rs2.close();
} catch(ClassNotFoundException w) {
System.out.println("Sorry,can`t find the Driver!");
w.printStackTrace();
} catch(SQLException w) {
w.printStackTrace();
} catch(Exception w) {
w.printStackTrace();
}
} }
}
}
for循环里加上
cb1.addItem(str1)