简体   繁体   English

复选框侦听器

[英]CheckBox Listener

I have some CheckBoxes which are made of my database table names我有一些由我的数据库表名组成的复选框

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);
}

now lets say I get 10 checkboxes (with different names ofc.).现在可以说我得到 10 个复选框(具有不同的名称)。 How do I know which of them was checked?我怎么知道哪些被检查了?

eg I check box nr 1.5.7 and click on a button "Print".例如,我选中 nr 1.5.7 框并单击“打印”按钮。 How do I print them out?我如何打印它们?

System.out.println("Checked items : " +check); ? ?

You need a data structure like a Map to map between your checkboxes and the tables... I usually work with SWT and you have a map in each GUI component to store whatever you want, but as far as I know, you don't have this in awt, so you need to do this manually... (I assume you are using awt, although CheckBox class in awt is actually Checkbox not CheckBox!!!) Bottom line is, you need to bind some data to your GUI components so you can recognize them later in your code... I would use a map:你需要一个像 Map 这样的数据结构来映射你的复选框和表......我通常使用 SWT 并且你在每个 GUI 组件中有一个映射来存储你想要的任何东西,但据我所知,你没有在awt中有这个,所以你需要手动做这个......(我假设你正在使用awt,虽然awt中的CheckBox类实际上是Checkbox而不是CheckBox!!!)底线是,你需要将一些数据绑定到你的GUI组件,以便您稍后可以在代码中识别它们......我会使用地图:

Map<Checkbox, String> checkToTableId;
Map<String, Checkbox> tableIdToCheck;

And build them up as you create the checks...并在您创建支票时建立它们......

Use CheckboxGroup .使用CheckboxGroup

CheckboxGroup cbg=new CheckboxGroup();

add a CheckboxGroup as follows:添加一个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););
}

and you get the selected value.并获得选定的值。

String value = cbg.getSelectedCheckbox().getLabel();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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