Frage

I'm trying to create jlabels and jtextfields dynamically and add then to a jPanel where the user can insert data into a table in a database.

I retrieve from the database the name of all the table attributes and then I have to create a jlabel and a jtext for each of these names. The result of it should be something like shown below (the label 5 should only be created if there is no more space in the bottom of the jpanel): Expected Result

I tried to used GroupLayout and SequentialGroup but I couldn't achieve anything similar. I achieved something completely awkward that I can't understand and solve it.

Achieved result

The label called "ESTADO" should be under the "SIGLA" label and the label with the "inserting table is:" text should be in the left hand side of the screen, as in the first image.

My code is:

        ArrayList<String[]> tableAtts = sql.getTableAttributeNames(table);

    javax.swing.GroupLayout jPDataInsertionLayout = new javax.swing.GroupLayout(jPDataInsertion);
    jPDataInsertion.setLayout(jPDataInsertionLayout);

    GroupLayout.SequentialGroup shgp = jPDataInsertionLayout.createSequentialGroup();
    GroupLayout.SequentialGroup svgp = jPDataInsertionLayout.createSequentialGroup();

    jPDataInsertionLayout.setHorizontalGroup(
            jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
            .addGroup(shgp));

    jPDataInsertionLayout.setVerticalGroup(
            jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
            .addGroup(svgp));


    //shgp.addContainerGap();
    svgp.addContainerGap().addComponent(jLInsertingTable);

    for (String[] values : tableAtts) {

        JLabel jLTableAtt = new JLabel(values[0]);
        JTextField jTFTableAtt = new JTextField();
        jTFTableAtt.setColumns(13);

        shgp.addComponent(true,jLTableAtt)
                .addGap(18, 18, 18)
                .addComponent(jTFTableAtt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE);

        svgp.addGap(18, 18, 18)
                .addGroup(jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLTableAtt, javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTFTableAtt,javax.swing.GroupLayout.Alignment.BASELINE ,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
    }

    shgp.addComponent(jLInsertingTable);

EDIT:

Using what #rcook said, I change the groups and managed to create what I wanted. Here is the code I manage to create:

ArrayList<String[]> tableAtts = sql.getTableAttributeNames(table);

    ArrayList<JLabel> attLabels = new ArrayList<>();
    ArrayList<JTextField> attFields = new ArrayList<>();

    javax.swing.GroupLayout jPDataInsertionLayout = new javax.swing.GroupLayout(jPDataInsertion);
    jPDataInsertion.setLayout(jPDataInsertionLayout);

    GroupLayout.SequentialGroup hsg = jPDataInsertionLayout.createSequentialGroup();

    ArrayList<String[]> tempTableAtt = new ArrayList<>(tableAtts);

    for(int i = 0; (!tempTableAtt.isEmpty()) && i < COLUMN_SIZE ; i++){

        GroupLayout.ParallelGroup innerhpg = jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false);

        for(int j = 0; (!tempTableAtt.isEmpty()) && j < ROW_SIZE ; j++){
            String[] att = tempTableAtt.get(0);
            GroupLayout.SequentialGroup innerhsg = jPDataInsertionLayout.createSequentialGroup();

            JLabel jLTableAtt = new JLabel(att[0]);
            JTextField jTFTableAtt = new JTextField();
            jTFTableAtt.setColumns(13);

            attLabels.add(jLTableAtt);
            attFields.add(jTFTableAtt);

            innerhsg.addComponent(jLTableAtt)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jTFTableAtt);

            innerhpg.addGroup(javax.swing.GroupLayout.Alignment.LEADING, innerhsg);

            tempTableAtt.remove(att);
        }

        hsg.addGap(18,18,18).addGroup(innerhpg);
    }

    jPDataInsertionLayout.setHorizontalGroup(
        jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPDataInsertionLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLInsertingTable)
                .addGroup(hsg))));

    GroupLayout.SequentialGroup vsg = jPDataInsertionLayout.createSequentialGroup();

    vsg.addContainerGap()
       .addComponent(jLInsertingTable);

    for(int i = 0; i < tableAtts.size() && i < ROW_SIZE; i++){
        GroupLayout.ParallelGroup vpg = jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE);

        vpg.addComponent(attLabels.get(i))
            .addComponent(attFields.get(i), javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE);

        for(int j = 0; (tableAtts.size() > ROW_SIZE) && ((i+(ROW_SIZE*(j+1))) < tableAtts.size()) && j < COLUMN_SIZE; j++){
            vpg.addComponent(attLabels.get(i+(ROW_SIZE*(j+1))))
               .addComponent(attFields.get(i+(ROW_SIZE*(j+1))), javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE);
        }

        vsg.addGap(18, 18, 18).addGroup(vpg);
    }

   jPDataInsertionLayout.setVerticalGroup(
           jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(vsg));

I don't know if that is the best solution but it works.

War es hilfreich?

Lösung

In order for your first column of labels to be one under the other, then all those labels need to be in one parallel group. Your code creates parallel groups for each label/field combo, and adds those groups separately, so they do not appear in the same vertical (or horizontal) columns (or rows) as each other.

Your code is going to need to determine how many label/field pairs you want to display in each column, and create one parallel group that contains them all.

(alternately, I supposed you could have a panel that contained the label and field, and put a group of those panels in the same parallel group. I think it would be easier to align all the labels and fields, however, if they were in their own columns.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top