0

How can I add checkboxes, depending on the number of columns that a table has?.

I have a ComboBox where I can choose one table from my database, and the program will make a select depending on what check box you checked. I know how to get the number of columns, but I dont know how to make as many check boxes as that number. The number is different depending on the item selected on the comboBox (the table) The checkBox Using JFrame:

JComboBox<String> tabla = new JComboBox();
tabla.setBounds(10, 650, 200, 25);

    tabla.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (tabla.countComponents() > 0) {
                item_sel = tabla.getSelectedItem().toString();
                try {
                    ResultSet rs1 = stmt.executeQuery("select * from "+item_sel);
                    ResultSetMetaData rsmd = (ResultSetMetaData) rs1.getMetaData();
                    int columnsNumber = rsmd.getColumnCount();
                    for(int i = 0; i<= columnsNumber-1; i++) {
                        //here I add comboBox
                    }
                } catch (SQLException e2) {
                    // TODO Auto-generated catch block
                    System.out.println(e2);
                }


            }

        }
    });

Also: How do I set the bounds different in each comboBox?

  • 1
    Why not simply something like `for(int i = 0; i < numberOfColumns; ++i) // create a checkbox`? – Ben Jun 04 '18 at 10:41
  • Ok, I know how to know the number of columns using sql, but... How can I save That number into a variable?. I mean: ResultSet rs1 = stmt.executeQuery("select count(*) from Information_Schema.Columns where Table_Name = '" + item_sel +"'"); but now how do I save that resultSet? int number = rs1.getInt(Something);? – Jose Lizarraga Jun 04 '18 at 10:56
  • So your problem is more about how to get the number of row or column from the database ? – AxelH Jun 04 '18 at 11:13
  • Possible duplicate of [How to get the number of columns from a JDBC ResultSet?](https://stackoverflow.com/questions/2614416/how-to-get-the-number-of-columns-from-a-jdbc-resultset) – AxelH Jun 04 '18 at 11:13
  • No, what I mean is that I know how to get that number, but i dont know how to create as many check boxes as that number – Jose Lizarraga Jun 04 '18 at 11:14
  • Using the result set metadata, on most db I tried, you can `select * from table where 1 = 2` and the metadata will gave you the correct schema but without the need to retrieve any data. – AxelH Jun 04 '18 at 11:14
  • Show us how you are creating your check box for the moment please. A [mcve] would be nice. PS: you really we don't know what GUI technology you are using. – AxelH Jun 04 '18 at 11:16
  • The first difficulty I see is that you don't use a layout manager but set the bounds... you will need to calculate the bounds yourself with that. – AxelH Jun 04 '18 at 11:25
  • Yes I know, and I have to set diffentent names to each other, that is what I don't really knpw how to do – Jose Lizarraga Jun 04 '18 at 11:45

1 Answers1

0

If its oracle, you can get the list of column names from user_tab_columns table by passing the table_name in where clause. Iterate over the list and your check boxes can be created with column names.

For database independent solution, you can use JDBC ResultSet to get the column count. Refer get columns number from ResultSetMetaData

Hope it helps

Mohit
  • 126
  • 1
  • 9
  • Ok, I got it, now.. Using a for loop as Marlin said.. how can I set different names for each check box? Or I dont really need different names? – Jose Lizarraga Jun 04 '18 at 11:09
  • That completely depends what you need. if you want to show the name of column against each checkbox, simply initialize the new JCheckbox with the name of column inside the loop. – Mohit Jun 04 '18 at 11:17