我有一些由数据库表名组成的CheckBoxes
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个复选框(具有不同的名称ofc)。我怎么知道他们中的哪一个被选中了?
例如,I复选框nr 1.5.7并点击按钮"Print“。如何将它们打印出来?
System.out.println("Checked items : " +check);
发布于 2011-09-22 20:48:34
使用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();发布于 2011-09-22 21:26:27
您需要一个类似于Map的数据结构来在复选框和表之间进行映射……我通常使用SWT,您在每个GUI组件中都有一个映射来存储您想要的任何内容,但据我所知,awt中没有此映射,因此您需要手动执行此操作……(我假设您使用的是awt,尽管awt中的CheckBox类实际上是Checkbox而不是CheckBox!)底线是,您需要将一些数据绑定到GUI组件,以便稍后在代码中识别它们……我会使用地图:
Map<Checkbox, String> checkToTableId;
Map<String, Checkbox> tableIdToCheck;并在创建检查时构建它们...
https://stackoverflow.com/questions/7514788
复制相似问题