-1

I am Creating a Messenger like App just for learning. I set the layout of Parent JPanel to GridLayout for Grid.

Now i want JPanel to show ScrollBar when i add more JPanels which exceeds the size of Parent JPanel.

I tried adding it to ScrollPane but its not working Bellow is an Example image,

Sorry if my Question is not fully explained. I will try to explain it more if needed.

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65
Ijaz Ur Rahim
  • 368
  • 4
  • 18
  • You have to set size of the jpanel which added to scrollpane to make it scrollable. Try set size of jpanel bigger tha the size of the scrollpane. – mstfyldz Jan 29 '19 at 07:38
  • [How to Use Scroll Panes](https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) – MadProgrammer Jan 29 '19 at 07:43
  • 1
    "I will try to explain it more if needed." The question is clear. Please post your code to see what you tried. – c0der Jan 29 '19 at 08:17
  • See an example of using `JScrollpane` [here](https://stackoverflow.com/a/50421615/3992939) – c0der Jan 29 '19 at 08:34
  • BTW - on seeing that image, the first thing I think is `JList`. Use an appropriate rendering component and it could display `Message` objects easily. – Andrew Thompson Jan 29 '19 at 08:39

2 Answers2

0

The way i see it, in your case a BoxLayout with Y_AXIS inside a BorderLayout (with a small "trick") would be more elegant. Take a look at the following SSCCE: There is a line to comment in order to answer your question, with a grid layout.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.LineBorder;

public class ChatFrame extends JFrame {
    static int x = 1;

    public ChatFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        setLocationRelativeTo(null);

        getContentPane().setLayout(new BorderLayout());
        JPanel gridPanel = new JPanel(new GridLayout(0, 1));
        // Comment the following line to see behavior with grid layout
        gridPanel.setLayout(new BoxLayout(gridPanel, BoxLayout.Y_AXIS));

        final JScrollPane sp = new JScrollPane(gridPanel);
        getContentPane().add(sp);

        final String helloWorld = "Hello world, ";
        Timer t = new Timer(1000, e -> {
            ChatPanel cp = new ChatPanel(helloWorld + (x++));
            gridPanel.add(cp);
            // Scroll to the last chat.
            sp.getVerticalScrollBar().setValue(sp.getVerticalScrollBar().getMaximum());
            gridPanel.repaint();
            gridPanel.revalidate();
        });
        t.start();
    }

    private static class ChatPanel extends JPanel {
        public ChatPanel(String chat) {
            super(new BorderLayout());
            setBorder(new LineBorder(Color.red));
            JLabel chatLabel = new JLabel(chat);
            add(chatLabel, BorderLayout.CENTER);
            // a kind of "trick"
            setMaximumSize(new Dimension(getMaximumSize().width, getPreferredSize().height));
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ChatFrame().setVisible(true));
    }
}

Preview:

enter image description here

George Z.
  • 6,643
  • 4
  • 27
  • 47
0

Everyone tried to solve my problem, but in my case it doesn't help me. So i tried every possible thing i could.

As i saw the scrollBar of JScrollPanel appears according to the size of JPanel so i tried to increase the size of JPanel and this solved my problem. Below is an example and explanation of what i said.

After adding all the JPanels into the parent JPanel, i configured the height of parent JPanel to {height of parent JPanel + (height of child JPanel * number of msgs)}

consider the following code:

msgPanel.setPreferredSize(new Dimension(268,418+(new msg().getPreferredSize().height*(msgs.length-4)));

here the -4 is the default number of JPanels that do can fit in default size of parent JPanel

Ijaz Ur Rahim
  • 368
  • 4
  • 18