I have a JScrollPane, call it jsp. I want to populate this scroll pane with a list of strings generated dynamically. To do this, I'm creating a JPanel 'pane', and inserting into it, using a GridBagLayout (for vertical-only list), several MiniGUI's that extend JPanel (formatted like I want the output to look).
When I load up the program, jsp appears empty. I was populating it before in the same way using JTextAreas (instead of MiniGUI's) and it was working fine, but JTextArea proved terrible for formatting. I need to insert some small images to the MiniGUI's (later), which needless to say is impossible to do using JTextAreas, so I can't get rid of the MiniGUIs.
I can't seem to find the answer online, so if anyone could help it would be great. I checked this question out: How do I make JScrollPane work properly with nested JPanels?, but shrinking the size of the MiniGUI didn't work, and I'm already using revalidate.
I've posted my code. The JScrollPane is about 250x500.
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class MiniGUI extends JPanel {
public MiniGUI(String str1, String str2) {
this.setLayout(null);
this.setSize(new Dimension(250, 95));
this.setVisible(true);
JLabel lbl1 = new JLabel(str1);
lbl1.setOpaque(true);
lbl1.setVerticalAlignment(SwingConstants.TOP);
lbl1.setBackground(Color.WHITE);
lbl1.setBounds(0, 0, 234, 64);
this.add(lbl1);
JLabel lbl2 = new JLabel(str2);
lbl2.setHorizontalAlignment(SwingConstants.RIGHT);
lbl2.setOpaque(true);
lbl2.setBackground(Color.WHITE);
lbl2.setVerticalAlignment(SwingConstants.TOP);
lbl2.setBounds(86, 63, 148, 21);
this.add(lbl2);
this.setBackground(Color.WHITE);
// To be set later:
// ImagePanel panel = img; //img passed as parameter, image panel is a class i made
// if (img == null)
// return;
// panel.setBounds(0, 0, 108, 89);
// this.add(panel);
}
And the function that fills the scroll pane jsp:
public void fillScrollPane() {
JPanel pane = new JPanel();
GridBagLayout expLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
pane.setLayout(expLayout);
ArrayList<Triplet> content = this.C2.getAL(); //C2 is another class i have, getAL returns ArrayList, Triplet is just 3 strings
for (int i = 0; i < content.size(); i += 1) {
MiniGUI x = new MiniGUI(content.get(i).first,
content.get(i).second + " " + content.get(i).third); //setVisible is in minigui class
pane.add(x, c);
pane.revalidate();
c.gridy += 1;
}
MiniGUI end = new MiniGUI("", "");
c.weighty = 1;
pane.setBackground(Color.WHITE);
pane.add(end, c);
pane.setVisible(true);
jsp.setViewportView(pane);
jsp.revalidate();
}