Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 19th, 2009, 08:54 AM
Authorized User
Join Date: Feb 2009
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Java JFrame, Add JPanels to JScrollPane Issue

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!


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!");
setLayout(new BorderLayout());


//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(C olor.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.createLineB order(Color.black));
scrollPaneLeft.setHorizontalScrollBarPolicy(JScrol lPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneLeft.setVerticalScrollBarPolicy(JScrollP ane.VERTICAL_SCROLLBAR_ALWAYS);

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


//List jbutton;
//jbutton - new ArrayList();

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);


add(topPane, BorderLayout.NORTH);
add(leftPane, BorderLayout.WEST);
//add(scrollPaneLeft, BorderLayout.WEST);
//getContentPane().add(scrollPaneLeft, BorderLayout.WEST);

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



public static void main(String[] arguments){
GuiWork gui = new GuiWork();

Old June 25th, 2009, 05:59 AM
Friend of Wrox
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post

Java JFrame, Add JPanels to JScrollPane Issue
- Rakesh

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Constructor issue pyr0b0y Java Basics 2 February 26th, 2009 03:47 PM
java library path issue elixiradeel Apache Tomcat 0 June 5th, 2008 07:59 AM
java security certificate issue getsreenuk Apache Tomcat 0 March 3rd, 2007 05:49 AM
XSLT issue to add commas to values 2BOrNot2B XSLT 3 October 31st, 2006 12:04 PM
help with JFrame.setBackground(...) dzungrockx Java GUI 0 March 15th, 2006 12:19 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.