I am trying to build a table look with the GridBagLayout, but it keeps centering everything. I don't know why it is doing this. My insets are set at 5, yet it makes them look huge.
It started off fine, but somewhere it changed to do this. I cant ctrl z to go back.
Here is my code:
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
panelInfo = new JPanel();
scrollPane.setViewportView(panelInfo);
GridBagLayout gbl_panelInfo = new GridBagLayout();
gbl_panelInfo.columnWidths = new int[]{0, 0, 0, 0, 0};
gbl_panelInfo.rowHeights = new int[] {0, 30, 0};
gbl_panelInfo.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0};
gbl_panelInfo.rowWeights = new double[]{0.0, 0.0, 0.0};
panelInfo.setLayout(gbl_panelInfo);
JLabel lblColumnName1 = new JLabel("No.");
GridBagConstraints gbc_lblColumnName1 = new GridBagConstraints();
gbc_lblColumnName1.insets = new Insets(5, 0, 5, 5);
gbc_lblColumnName1.gridx = 0;
gbc_lblColumnName1.gridy = 0;
panelInfo.add(lblColumnName1, gbc_lblColumnName1);
JLabel lblColumnName2 = new JLabel("Name");
GridBagConstraints gbc_lblColumnName2 = new GridBagConstraints();
gbc_lblColumnName2.insets = new Insets(5, 0, 5, 5);
gbc_lblColumnName2.gridx = 1;
gbc_lblColumnName2.gridy = 0;
panelInfo.add(lblColumnName2, gbc_lblColumnName2);
JLabel lblColumnName3 = new JLabel("Number");
GridBagConstraints gbc_lblColumnName3 = new GridBagConstraints();
gbc_lblColumnName3.insets = new Insets(5, 0, 5, 5);
gbc_lblColumnName3.gridx = 2;
gbc_lblColumnName3.gridy = 0;
panelInfo.add(lblColumnName3, gbc_lblColumnName3);
JLabel lblColumnName4 = new JLabel("Credits");
GridBagConstraints gbc_lblColumnName4 = new GridBagConstraints();
gbc_lblColumnName4.insets = new Insets(5, 0, 5, 5);
gbc_lblColumnName4.gridx = 3;
gbc_lblColumnName4.gridy = 0;
panelInfo.add(lblColumnName4, gbc_lblColumnName4);
JLabel lblColumnName5 = new JLabel("Grade");
GridBagConstraints gbc_lblColumnName5 = new GridBagConstraints();
gbc_lblColumnName5.insets = new Insets(5, 0, 5, 0);
gbc_lblColumnName5.gridx = 4;
gbc_lblColumnName5.gridy = 0;
panelInfo.add(lblColumnName5, gbc_lblColumnName5);
ArrayList<Course> courseList = user.getCourseList();
if(courseList.size() == 0) lblMessage.setText("Student has never enrolled in any course");
int i = 1, x= 0;
Double total = 0d;
if(courseList.size() > 0) {
for(; i < courseList.size() 1; i ) {
JLabel lblRowData1 = new JLabel(Integer.toString(i));
GridBagConstraints gbc_lblRowData1 = new GridBagConstraints();
gbc_lblRowData1.insets = new Insets(0, 0, 5, 5);
gbc_lblRowData1.gridx = 0;
gbc_lblRowData1.gridy = i;
panelInfo.add(lblRowData1, gbc_lblRowData1);
JLabel lblRowData2 = new JLabel(courseList.get(i-1).getName());
GridBagConstraints gbc_lblRowData2 = new GridBagConstraints();
gbc_lblRowData2.insets = new Insets(0, 0, 5, 5);
gbc_lblRowData2.gridx = 1;
gbc_lblRowData2.gridy = i;
panelInfo.add(lblRowData2, gbc_lblRowData2);
JLabel lblRowData3 = new JLabel(courseList.get(i-1).getNumber());
GridBagConstraints gbc_lblRowData3 = new GridBagConstraints();
gbc_lblRowData3.insets = new Insets(0, 0, 5, 5);
gbc_lblRowData3.gridx = 2;
gbc_lblRowData3.gridy = i;
panelInfo.add(lblRowData3, gbc_lblRowData3);
JLabel lblRowData4 = new JLabel(Integer.toString(courseList.get(i-1).getCredits()));
GridBagConstraints gbc_lblRowData4 = new GridBagConstraints();
gbc_lblRowData4.insets = new Insets(0, 0, 5, 5);
gbc_lblRowData4.gridx = 3;
gbc_lblRowData4.gridy = i;
panelInfo.add(lblRowData4, gbc_lblRowData4);
Double grade = user.getGrades().get(i-1);
total = grade;
x ;
JLabel lblRowData5 = new JLabel(String.format("%.2f", grade));
GridBagConstraints gbc_lblRowData5 = new GridBagConstraints();
gbc_lblRowData5.insets = new Insets(0, 0, 5, 0);
gbc_lblRowData5.gridx = 4;
gbc_lblRowData5.gridy = i;
panelInfo.add(lblRowData5, gbc_lblRowData5);
}
}
JLabel lblGPA = new JLabel("GPA: ");
GridBagConstraints gbc_lblGPA = new GridBagConstraints();
gbc_lblGPA.insets = new Insets(0, 0, 5, 5);
gbc_lblGPA.gridx = 3;
gbc_lblGPA.gridy = i 1;
panelInfo.add(lblGPA, gbc_lblGPA);
JLabel lblGPAvalue = new JLabel(String.format("%.2f", total/x));
GridBagConstraints gbc_lblGPAvalue = new GridBagConstraints();
gbc_lblGPAvalue.insets = new Insets(0, 0, 5, 0);
gbc_lblGPAvalue.gridx = 4;
gbc_lblGPAvalue.gridy = i 1;
panelInfo.add(lblGPAvalue, gbc_lblGPAvalue);
numOfCoursesDisplayed = x;
I have the exact same code in another class of the same project, over there its fine.
CodePudding user response:
Set the anchor
field in GridBagConsraints
to TOP
;
If you want the look of the table, why not just use a JTable
? It would be a much cleaner look and easier to deal with.
CodePudding user response:
I was able to solve the issue by applying the following changes:
GridBagLayout gbl_panelInfo = new GridBagLayout();
gbl_panelInfo.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
gbl_panelInfo.rowHeights = new int[]{0, 0, 0, 0};
gbl_panelInfo.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
gbl_panelInfo.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
panelInfo.setLayout(gbl_panelInfo);