Manu
Manu

Reputation: 65

Unable to get JScrollPane working on JFrame

Please look into the small code below. The scroll pane appears, but the sliders do not. Even if I resize the frame the sliders do not. Please help.

import javax.swing.*;
public class sample {
    static JFrame frame;
    public static void main(String[] args) {
        String Msg = "Sample Message To Test Scrolling";
        frame = new JFrame("Sample Program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        JPanel panel = new JPanel();
        panel.setLayout(null);

        for (int ypos = 0, i = 0; i < 40; i++) {
            JLabel label = new JLabel("" + i + " " + Msg);
            label.setFont(new Font("Courier", Font.BOLD, 12));
            panel.add(label);
            label.setBounds(10, ypos + 5,
                            label.getPreferredSize().width,
                            label.getPreferredSize().height);
            ypos += label.getPreferredSize().height;
        }
        JScrollPane scroll = new JScrollPane(panel,
                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        frame.setLayout(new BorderLayput());
        frame.add(scroll);
        frame.setVisible(true);
    }
}

Upvotes: 4

Views: 2527

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

The sliders will only appear if and when the component contained by the JScrollPane's viewport is larger than the viewport. Based on your posted code, I don't see why your component would be larger than the viewport as the panel's size will be based on its preferredSize, something that will never change since for one, you're adding components to it with it using a null layout.

As an aside you should almost never use null layout.

For example:

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.*;

public class Sample2 {
   private static final int PREF_W = 600;
   private static final int PREF_H = PREF_W;
   private static final int MAX_ROWS = 400;
   private static final String TEXT_BODY = "Sample Message To Test Scrolling";;

   private static void createAndShowGui() {
      JPanel panel = new JPanel(new GridLayout(0, 1));
      panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      for (int i = 0; i < MAX_ROWS; i++) {
         String text = String.format("%03d %s", i, TEXT_BODY);
         JLabel label = new JLabel(text);
         panel.add(label);
      }

      JScrollPane scrollPane = new JScrollPane(panel);
      scrollPane.setPreferredSize(new Dimension(PREF_W, PREF_H));

      JFrame frame = new JFrame("Sample2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(scrollPane);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Upvotes: 4

Related Questions