import java.sql.DriverManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Combo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class DLC {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
DLC window = new DLC();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell(SWT.CLOSE);
shell.setSize(336, 74);
shell.setText("DLC-数据联动下拉框");
Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setAlignment(SWT.RIGHT);
lblNewLabel.setBounds(10, 10, 54, 12);
lblNewLabel.setText("主要分类");
Label label = new Label(shell, SWT.NONE);
label.setText("次要分类");
label.setAlignment(SWT.RIGHT);
label.setBounds(173, 10, 54, 12);
final Combo combo = new Combo(shell, SWT.READ_ONLY);
combo.setBounds(70, 10, 87, 20);
final Combo subcombo = new Combo(shell, SWT.READ_ONLY);
subcombo.setBounds(233, 10, 87, 20);
subcombo.setEnabled(false);
try {
Conn MySqlLink = new Conn();
MySqlLink.conn();
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/BuBuDB","root","123456");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from mcc order by id asc");
int i=0;
while (rs.next()) {
combo.add(rs.getString("MainClass"),i);
i+=1;
}
combo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
subcombo.removeAll();
Conn aa = new Conn();
aa.conn();
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/BuBuDB","root","20050819");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from scc where MainClass='"+ combo.getText() +"' order by id asc");
int i=0;
while (rs.next()) {
subcombo.add(rs.getString("SubClass"),i);
i+=1;
}
subcombo.setEnabled(true);}
catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
catch (Exception e) {
System.out.print("get data error!");
e.printStackTrace();
}
}
}
SWT combobox 联动菜单
4星 · 超过85%的资源 需积分: 41 164 浏览量
2013-07-08
13:37:03
上传
评论
收藏 7KB RAR 举报
landkim
- 粉丝: 0
- 资源: 7