繁体   English   中英

复选框侦听器

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM