复选框监听器

发布于 2024-12-05 19:45:30 字数 1235 浏览 2 评论 0原文

我有一些由我的数据库表名称组成的复选框,

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("检查的项目:" +check);?

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.). How do I know which of them was checked?

e.g. I check box nr 1.5.7 and click on a button "Print". How do I print them out?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等你爱我 2024-12-12 19:45:30

您需要像 Map 这样的数据结构来在复选框和表格之间进行映射...我通常使用 SWT,并且每个 GUI 组件中都有一个映射来存储您想要的任何内容,但据我所知,您没有awt 中有这个,所以你需要手动执行此操作...(我假设你正在使用 awt,尽管 awt 中的 CheckBox 类实际上是 Checkbox 而不是 CheckBox!!!)
最重要的是,您需要将一些数据绑定到 GUI 组件,以便稍后可以在代码中识别它们......
我会使用地图:

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

并在创建检查时构建它们......

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<Checkbox, String> checkToTableId;
Map<String, Checkbox> tableIdToCheck;

And build them up as you create the checks...

烟凡古楼 2024-12-12 19:45:30

使用 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();

Use CheckboxGroup.

CheckboxGroup cbg=new CheckboxGroup();

add a CheckboxGroup as follows:

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文