CheckBox Receiver

I have some CheckBox that are made from the names of my database table

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 I will say that I get 10 flags (with different names c.), how do I know which one was checked?

eg. I check the box nr 1.5.7 and click the "Print" button, how to print them?

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

+4
source share
2 answers

Use CheckboxGroup .

 CheckboxGroup cbg=new CheckboxGroup(); 

add 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 will get the selected value.

 String value = cbg.getSelectedCheckbox().getLabel(); 
+2
source

You need a data structure such as a map to display between your checkboxes and tables ... I usually work with SWT and you have a map in each component of the GUI to store everything you want, but as far as I know, you donโ€™t you need to do this in awt, so you need to do it manually ... (I assume you use awt, although the CheckBox class in awt is actually Checkbox not CheckBox !!!) Bottom line: you need to bind some data to your graphics components interface to subsequently recognize them in your code ... I would use a map:

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

And create them when creating checks ...

+3
source

All Articles