1

When I placed an array into mainJpanel it works correctly. I want to place an array into JScrollPane but it does not work. Please explain why.

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

public class Window extends JFrame {
    public  Window() {
        setLocation( 100,100);
        setSize(300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("FontView");
        setVisible(true);
        setLayout( new FlowLayout());
        JPanel mainPanel = new JPanel();
        setContentPane(mainPanel);

        Method fontMassive = new Method();

        //поехали
        JPanel[] jPanels = new JPanel[3];
        JLabel[] jLabels = new JLabel[3];
        for (int i = 0; i < 3; i++) {
            jPanels[i]=new JPanel();
            jLabels[i] = new JLabel(fontMassive.getFonts(i));
            jPanels[i].add(jLabels[i]);
        }
        JScrollPane scroll = new JScrollPane();

        for (int i = 0; i < 3; i++) {
            scroll.add(jPanels[i]);
        }
        mainPanel.add(scroll);
    }
}

placed into mainPanel

placed into scroll

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    You could add one jpanel with a suitable layout to the jscrollpane. And then add all the jpanels to that jpanel. – Just another Java programmer Sep 01 '21 at 15:55
  • This [example](https://stackoverflow.com/a/26203037/230513) adds a new `JPanel` having `GridLayout`. – trashgod Sep 01 '21 at 17:16
  • 1
    **Other tips:** 1) `setVisible(true);` This should be last. After all components are added, and immediately after calling `pack()`. 2) `Method` What is that? Post a [mre]. 3) `Window` use descriptive names for classes (methods and attributes). It will help you later, and everyone else right now. 4) Depending on what `Method` is, or does, it might be better here to put a `JList` in the scroll pane and add new information directly to the list component. – Andrew Thompson Sep 01 '21 at 17:32

1 Answers1

0

Just another Java programmer 's answer was right, i need to place an array into Panel and then place it into Srcoll.
Thank you guys for the answers.

  • 1
    One of the purposes of this Web site is for other people, with the same problem, to benefit from your solution. I think you should [edit] your answer and post your updated code that resolved your problem. Alternatively, delete this answer and simply post it as a comment to your question. – Abra Sep 02 '21 at 04:27
  • 1
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 02 '21 at 05:35