[英]CheckBox Listener
我有一些由我的數據庫表名組成的復選框
if(event.getSource()==connect){
CheckBox check;
Connection connection = null;
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/db";
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
// Gets the metadata of the database
DatabaseMetaData dbmd = connection.getMetaData();
String[] types = {"TABLE"};
ResultSet rs = dbmd.getTables(null, null, "%", types);
while (rs.next()) {
String tableCatalog = rs.getString(1);
String tableSchema = rs.getString(2);
String tableName = rs.getString(3);
check = new CheckBox(tableName);
Tables.addComponent(check);
}
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Tables.addComponent(generate);
}
現在可以說我得到 10 個復選框(具有不同的名稱)。 我怎么知道哪些被檢查了?
例如,我選中 nr 1.5.7 框並單擊“打印”按鈕。 我如何打印它們?
System.out.println("Checked items : " +check);
?
你需要一個像 Map 這樣的數據結構來映射你的復選框和表......我通常使用 SWT 並且你在每個 GUI 組件中有一個映射來存儲你想要的任何東西,但據我所知,你沒有在awt中有這個,所以你需要手動做這個......(我假設你正在使用awt,雖然awt中的CheckBox類實際上是Checkbox而不是CheckBox!!!)底線是,你需要將一些數據綁定到你的GUI組件,以便您稍后可以在代碼中識別它們......我會使用地圖:
Map<Checkbox, String> checkToTableId;
Map<String, Checkbox> tableIdToCheck;
並在您創建支票時建立它們......
使用CheckboxGroup
。
CheckboxGroup cbg=new CheckboxGroup();
添加一個CheckboxGroup
如下:
while (rs.next()) {
String tableCatalog = rs.getString(1);
String tableSchema = rs.getString(2);
String tableName = rs.getString(3);
Tables.addComponent(new Checkbox(tableName,cbg,false););
}
並獲得選定的值。
String value = cbg.getSelectedCheckbox().getLabel();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.