Using Java Swing GroupLayout to create table of data from SQL database

104 Views Asked by At

I have a project where I want to map values of an SQL database onto a JPanel using GroupLayout. I'm having trouble figuring out why exactly my code does what it does. What follows is my code for the specific class in question:


import javax.swing.*;
import java.awt.*;

public class databaseScreen extends JPanel{
    private controller controller;
    private calculator calculator = new calculator();
    private createDB createDB = new createDB();
    JLabel pid = new JLabel();
    JLabel panelName = new JLabel();
    JLabel panelEff = new JLabel();
    JLabel lastUpdated = new JLabel();
    JLabel source = new JLabel();
    public databaseScreen(controller controller){
        this.controller=controller;
        databaseParameters();
    }
        private void databaseParameters(){
            GroupLayout layout = new GroupLayout(this);
            this.setLayout(layout);
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);

            GroupLayout.ParallelGroup pidGroupH = layout.createParallelGroup(GroupLayout.Alignment.CENTER);
            GroupLayout.ParallelGroup nameGroupH = layout.createParallelGroup(GroupLayout.Alignment.CENTER);
            GroupLayout.ParallelGroup effGroupH = layout.createParallelGroup(GroupLayout.Alignment.CENTER);
            GroupLayout.ParallelGroup updGroupH = layout.createParallelGroup(GroupLayout.Alignment.CENTER);
            GroupLayout.ParallelGroup sourceGroupH = layout.createParallelGroup(GroupLayout.Alignment.CENTER);

            for(int j=0;j<createDB.getTableSize();j++){
                pid.setText(String.valueOf(createDB.getPid(j)));
                panelName.setText(createDB.getPanelName(j));
                panelEff.setText(String.valueOf(createDB.getPanelEff(j)));
                lastUpdated.setText(createDB.getLastUpdated(j));
                source.setText(createDB.getSource(j));
                pidGroupH.addComponent(pid);
                nameGroupH.addComponent(panelName);
                effGroupH.addComponent(panelEff);
                updGroupH.addComponent(lastUpdated);
                sourceGroupH.addComponent(source);
            }

            layout.setHorizontalGroup(
                    layout.createSequentialGroup()
                            .addGroup(pidGroupH)
                            .addGroup(nameGroupH)
                            .addGroup(effGroupH)
                            .addGroup(updGroupH)
                            .addGroup(sourceGroupH)
            );

            GroupLayout.SequentialGroup verticalGroups = layout.createSequentialGroup();

            for(int j=0;j<createDB.getTableSize();j++){
                pid.setText(String.valueOf(createDB.getPid(j)));
                panelName.setText(createDB.getPanelName(j));
                panelEff.setText(String.valueOf(createDB.getPanelEff(j)));
                lastUpdated.setText(createDB.getLastUpdated(j));
                source.setText(createDB.getSource(j));
                verticalGroups
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(pid)
                            .addComponent(panelName)
                            .addComponent(panelEff)
                            .addComponent(lastUpdated)
                            .addComponent(source))
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addGap(20,20,20)
                        );
            }

            layout.setVerticalGroup(
                verticalGroups
            );

        }
        
}

The get(variable) functions correctly recieve values based on the factor j, which I tested by creating a main and printing their values through the same for loop. However, when displaying the results on a screen, the results seem to stack on top of each other despite being in a different ParallelGroup in the VerticalGroup of the layout. Here is a screenshot of what the screen looks like:

https://i.stack.imgur.com/28G6b.png

Any advice would be appreciated.

0

There are 0 best solutions below