0

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();
}
Community
  • 1
  • 1
Jimmy
  • 395
  • 1
  • 4
  • 17
  • I don't see why this was downvoted. The problem is stated, as is the code, and I spent lots of time on it before posting and still did not find a solution. If you're going to downvote, at least explain so that if there is something wrong I can fix it for next time. – Jimmy Dec 03 '13 at 13:34
  • I'm not the downvoter, but you don't have a (Short, Self Contained, Correct and Compilable, Example)[http://sscce.org/] . You have a null layout for your miniGUI. Finally, you create a GridBagConstraints for your scroll pane panel, but you never modify it for each of your miniGUI panels. – Gilbert Le Blanc Dec 03 '13 at 13:45
  • Thank you. I'll try and improve on the self-contained next time. I wasn't sure (and still am not) how to put something that is dynamically generated in an example to keep it self contained. But I wouldn't say the code is long, nor incorrect. As I said it worked for JTextArea, there's just some subtlety missing that I couldn't catch because I'm somewhat new to Java and GUIs. (And as such didn't see what's wrong with null layouts). And the GridBagConstraints is modified for each loop. – Jimmy Dec 03 '13 at 13:58

1 Answers1

0

1) You can use setPreferredSize() instead of setSize() for your MiniGUI, because setSize() works only with null layout, but you add MiniGUI with help of GridBagLayout to your pane, so replace that code and it will work.

2)Don't use this.setLayout(null); in your MiniGUI. Try to use LayoutManager, for example FlowLayout or BorderLayout and your code also will be working.

Read more about LayoutManager.

Also you needn't to revalidate() container for each adding, do that when you add all components.

alex2410
  • 10,424
  • 3
  • 22
  • 40
  • Thank you, I'll try this out as soon as I can. – Jimmy Dec 03 '13 at 13:58
  • 1
    -1, `You can use setPreferredSize() instead of setSize() for your MiniGUI`, No. Don't manually code a preferred size. The whole point of using a layout manager is to let the layout manager determine the proper size of the panel based on the number of components added to the panel. If you hard code a size then the scrollbar will never work properly. – camickr Dec 03 '13 at 16:09
  • Don't use `setPreferredSize()` when you really mean to [override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513), for [example](http://stackoverflow.com/a/14011536/230513). – trashgod Dec 03 '13 at 17:24