Hello all! Again thanks for viewing my thread. The past couple of days I have been struggling with trying to add JPanels to a JScrollPane. Basically I have data to display that comes in from another application in the form of a JPanel with regular updates. I am trying to come up with a solution that will allow me to scroll down the JFrame assuming that I have more JPanels that will fit on the screen if that makes sense. I am not even sure if my solution will work.

Anyhow, the code below is just the JFrame class that I have been trying to get to work. In this case I am just trying to add multiple buttons to a JPanel then display the JPanel inside of a JScrollPane so I can scroll down to see all the buttons. So far this does not work. Any help will be grateful. I think I have been on a caffeine high that past couple of days because of this problem. Thanks!

Dan

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.border.*;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.ScrollPaneConstants;
import javax.swing.GroupLayout.SequentialGroup;
import javax.xml.ws.Action;
import javax.swing.JToolBar;
import java.io.File;


public class GuiWork extends JFrame {

	JPanel leftPane = new JPanel();
	JPanel rightPane = new JPanel();
	JPanel topPane = new JPanel();
	JScrollPane scrollPaneLeft = new JScrollPane();
	JScrollPane scrollPaneRight = new JScrollPane();
	JButton nButton = new JButton("North");
	JButton sButton = new JButton("South");
	JButton eButton = new JButton("East");
	JButton wButton = new JButton("West");
	JButton wButton2 = new JButton("West");
	JButton wButton3 = new JButton("West");
	JButton wButton4 = new JButton("West");
	JButton wButton5 = new JButton("West");
	JButton wButton6 = new JButton("West");
	JButton wButton7 = new JButton("West");
	JButton wButton8 = new JButton("West");
	JButton wButton9 = new JButton("West");
	
	
	
	public GuiWork(){
		super("Gui Work!");
		setSize(1000,750);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		
     //GOAL:::PLACE MULTIPLE JPANELS INSIDE JSCROLLPANE !

        //Size TopPane on North Border
		topPane.setMaximumSize(new java.awt.Dimension(1000, 200));
		topPane.setMinimumSize((new java.awt.Dimension(1000, 200)));
		topPane.setPreferredSize(new java.awt.Dimension(1000, 200));
		topPane.setBorder(BorderFactory.createLineBorder(Color.black));
		
		
		//Size LeftPane on West Border
		leftPane.setMaximumSize(new java.awt.Dimension(500, 350));
		leftPane.setMinimumSize((new java.awt.Dimension(500, 350)));
		leftPane.setPreferredSize(new java.awt.Dimension(500, 350));
		leftPane.setBorder(BorderFactory.createLineBorder(Color.black));
		
		
		//Size RightPane on East Border
		rightPane.setMaximumSize(new java.awt.Dimension(500, 350));
		rightPane.setMinimumSize((new java.awt.Dimension(500, 350)));
		rightPane.setPreferredSize(new java.awt.Dimension(500, 350));
		rightPane.setBorder(BorderFactory.createLineBorder(Color.black));
		

		scrollPaneLeft.setMaximumSize(new java.awt.Dimension(500, 350));
		scrollPaneLeft.setMinimumSize((new java.awt.Dimension(500, 350)));
		scrollPaneLeft.setPreferredSize(new java.awt.Dimension(500, 350));
		scrollPaneLeft.setBorder(BorderFactory.createLineBorder(Color.black));
		scrollPaneLeft.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPaneLeft.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        		
		//Place Components in Container
		wButton.setSize(50,175);
		wButton.setPreferredSize(new java.awt.Dimension(300, 300));
		wButton2.setPreferredSize(new java.awt.Dimension(300, 300));
		wButton3.setPreferredSize(new java.awt.Dimension(300, 300));

		//leftPane.add(scrollPaneLeft);
		
		//List jbutton;
		//jbutton - new ArrayList();
		
		//Policy
	    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
	    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
	   // scrollPaneLeft = new JScrollPane(leftPane, v, h);
		//add(scrollPaneLeft, BorderLayout.WEST);
		add(rightPane, BorderLayout.EAST);
		
		
		
		scrollPaneLeft.add(wButton);
		scrollPaneLeft.add(wButton2);
		scrollPaneLeft.add(wButton3);
		scrollPaneLeft.add(wButton4);
		scrollPaneLeft.add(nButton);
		scrollPaneLeft.validate();
		leftPane.add(scrollPaneLeft);
		scrollPaneLeft.validate();
		
		
		rightPane.add(eButton);
		topPane.add(nButton);
		topPane.add(sButton);
		add(topPane, BorderLayout.NORTH);
		add(leftPane, BorderLayout.WEST);
		//add(scrollPaneLeft, BorderLayout.WEST);
		scrollPaneLeft.validate();
		//getContentPane().add(scrollPaneLeft, BorderLayout.WEST);
		//scrollPaneLeft.setViewportView(leftPane);
		//scrollPaneLeft.setViewportPort(leftPane);
		
		
		

		/*
		 jt = new JTree();
		   jsp = new JScrollPane();

		   getContentPane().add(jsp);
		   jsp.setViewportView(jt);
		*/
		
	}
	
	public static void main(String[] arguments){
		GuiWork gui = new GuiWork();
		gui.setVisible(true);
	}
	
}

Ok, there were just a few issues and you got pretty close to the solution based upon the commented-out things you tried. Here's an updated version of your constructor and component setup code with some comments added

public GuiWork(){
super("Gui Work!");
    setSize(1000, 750);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());

    //GOAL::LACE MULTIPLE JPANELS INSIDE JSCROLLPANE !

    //Size TopPane on North Border
    topPane.setMaximumSize(new java.awt.Dimension(1000, 200));
    topPane.setMinimumSize((new java.awt.Dimension(1000, 200)));
    topPane.setPreferredSize(new java.awt.Dimension(1000, 200));
    topPane.setBorder(BorderFactory.createLineBorder(Color.black));
    topPane.add(nButton);
    topPane.add(sButton);
    // add to contentPane - not JFrame itself
    getContentPane().add(topPane, BorderLayout.NORTH);


    //Size RightPane on East Border
    rightPane.setMaximumSize(new java.awt.Dimension(500, 350));
    rightPane.setMinimumSize((new java.awt.Dimension(500, 350)));
    rightPane.setPreferredSize(new java.awt.Dimension(500, 350));
    rightPane.setBorder(BorderFactory.createLineBorder(Color.black));
    rightPane.add(eButton);
    getContentPane().add(rightPane, BorderLayout.EAST);


    // Size scrollPaneLeft - leftPane size doesn't matter since it's the scrollable view
    scrollPaneLeft.setMaximumSize(new java.awt.Dimension(500, 350));
    scrollPaneLeft.setMinimumSize((new java.awt.Dimension(500, 350)));
    scrollPaneLeft.setPreferredSize(new java.awt.Dimension(500, 350));
    scrollPaneLeft.setBorder(BorderFactory.createLineBorder(Color.black));
    scrollPaneLeft.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPaneLeft.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    // set leftPane as the scrollable component
    scrollPaneLeft.setViewportView(leftPane);

    //Place Components in Container
    wButton.setSize(50, 175);
    wButton.setPreferredSize(new java.awt.Dimension(300, 300));
    wButton2.setPreferredSize(new java.awt.Dimension(300, 300));
    wButton3.setPreferredSize(new java.awt.Dimension(300, 300));

    leftPane.add(wButton);
    leftPane.add(wButton2);
    leftPane.add(wButton3);
    leftPane.add(wButton4);
    // important to call revalidate here so leftPane will notify scrollPaneLeft to update accordingly
    leftPane.revalidate();
    getContentPane().add(scrollPaneLeft, BorderLayout.WEST);
}

The main things to note:
- Add components to the ContentPane - not the root of JFrame
- The scroll pane is essentially the component you size and place in your layout.
- The scrollable component that the scroll pane is viewing, the JPanel "leftPane" in your code, is the one that you add components to and it's size can vary (hence the reason for scroll bars). You set that component as the ViewportView of the scroll pane.
- When you add things that change the size of the scrollable component (the "view"), you need to call revalidate() on that component so that it will in turn notify the scroll pane to adjust accordingly.

- Add components to the ContentPane - not the root of JFrame

Not sure if it was 1.5 or 1.6, but this annoyance has now been fixed and ...

As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:
frame.add(child);
And the child will be added to the contentPane.

(API ref for JFrame 1.6)

Oh, hehe! I never knew of that change and was stuck in my old school ways. Thanks for the update! :)

Hey Ezzaral!

Thanks for the help. You are the first person who has explained to me what I was doing wrong after I posted a problem on here. I appreciate that. I just have one last quick question. I would like the scrollpane to display objects vertically from top to bottom and not left to right. I changed some code to try to get it to work but it did not.

I tried:
scrollPaneLeft.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

and I also tried to set the size of the left pane which contains the buttons to something long and narrow.
leftPane.setSize(100, 2000);

That did not work. So I am a little confused again. Anyhow, thanks a lot for your reply.

Dan

i have a small problem with srollpane. i want to add one pannel(named MainPanel) created in other class to the frame. i added like this(shown below), and initiate this class from my main method containing class Main.

then the frame opens but it won't show the scroll bars. the content which fits to my monitor display is only visible.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;


public class Application extends JFrame {

    private MainPanel currentpanel;
    public Application()
    {
        currentpanel = new MainPanel();     
        setupFrame();
    }

    private void setupFrame()
    {
        JScrollPane scrollPane = new JScrollPane(currentpanel);
        this.add(scrollPane, BorderLayout.CENTER);
        JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);


        this.setVisible(true);
        this.setSize(600,400);

    }

}

Edited 2 Years Ago by ramesh midatana

Ramesh: welcome to DaniWeb.
do understand that this thread is almost five years old, and is not (really) related to your question.
please don't hijack/revive dead threads. just create a new one, in which you explain what your problem is and your code.

This article has been dead for over six months. Start a new discussion instead.