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