Reputation: 49
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;
class Test
{
public Test()
{
JFrame f = new JFrame();
f.setSize(800,800);
f.setLayout(new FlowLayout());
JPanel s = new JPanel();
s.setLayout(new BoxLayout(s,BoxLayout.PAGE_AXIS));
JLabel m = new JLabel("D");
JLabel m1 = new JLabel("D1");
JLabel m2 = new JLabel("D1");
JLabel m3 = new JLabel("D1");
JLabel m4 = new JLabel("D1");
JLabel m5 = new JLabel("D1");
JLabel m6 = new JLabel("D1");
JLabel m7 = new JLabel("D1");
JLabel m8 = new JLabel("D1");
s.add(m);
s.add(m1);
s.add(m2);
s.add(m3);
s.add(m4);
s.add(m5);
s.add(m6);
s.add(m7);
s.add(m8);
JScrollPane scroll = new JScrollPane(s);
//scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setViewportBorder(new LineBorder(Color.RED));
f.add(scroll);
f.setVisible(true);
}
public static void main(String args[])
{
Test t = new Test();
}
}
Everything is ok,but the Scroll is not showing (JScollPane is appearing) .. and is there a better way to do this ? I mean - the proper way to show a JScrollPane in a panel ...... ?
Upvotes: 0
Views: 1256
Reputation: 4902
Scrollbars appear when the preferred size of the component added to the scollpane is greater than the size of the scrollpane.
JScrollPane scroll = new JScrollPane(s);
scroll.setPreferredSize(new Dimension(100, 50));
Scroll pane will expand its height as you add elements into it, unless you set the preferred size.
Upvotes: 2