ScrollPane not showing on JLabel

When I run this program, I don't see a scrollbar on the Label. What am I missing?

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

public class Util1
{
    public static void main(String[] args)
    {
        new Util1();
    }

    public Util1()
    {
        EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ExamplePane());

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true); 
            }
        });
    }

    protected class ExamplePane extends JPanel 
    {
        public ExamplePane() 
        {
            final JPanel panel = new JPanel(new GridBagLayout());
            final JLabel message = new JLabel("<html>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello</html>");

            message.setPreferredSize(new Dimension(500, 50));
            JScrollPane scroller = new JScrollPane( message, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scroller.setViewportView(message);

            panel.add(scroller);
            add(panel);
        }            
    }        
}

Answers


To see a scrollbar wrap the "message" JLabel into JPanel and then add this JPanel to JScrollPane like bellow:

    public ExamplePane() {
        final JPanel panel = new JPanel(new GridBagLayout());
        final JLabel message = new JLabel("<html>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello<br>Hello</html>");
        message.setPreferredSize(new Dimension(500, 50));

        final JPanel messagePanel = new JPanel();
        messagePanel.add(message);

        JScrollPane scroller = new JScrollPane(messagePanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroller.setPreferredSize(new Dimension(100, 50));

        panel.add(scroller);
        add(panel);
    }

Need Your Help

How to Initialize Array with Custom Type

c# arrays

How do I initialize this array of custom types:

.NET Plug In … where to go

.net .net-3.5 plugins extensibility

I'm writing a Windows Application that connects to a webshop through it's API and reads/writes values.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.