I have a JFrame with 2 JPanels...my problem

When I attach one of the JPanels to the JScrollPane object, it encompasses the whole JFrame, instead of just the JPanel I said to attach to. The scrollBar goes all the way up to the top of the JPanel I don't want it to be a part of, as if both JPanels are in the same JPanel, I think it does this because of the JFrame?
So when I scroll down, the top JPanel I want to stay , disappears. When I scroll back up , the top JPanel is completely gone out of site because of the scrolling, the top JPanel is with Color background and JLabel, it's a 'Title Panel', to show what this JFrame is. I know that it disappears because the scrollBar goes all the way up, so it's scrolling that JPanel to, but I don't want it to.
The JPanel I want the scrollPane to be on is the bottom one, which is just all JButtons, that go to different JPanels with setVisible(true), setVisible(false)
I've played around with the setViewport, but that doesn't do anything.

anyone?

The code:

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class Converter extends JFrame implements ActionListener, KeyListener
{
	static JPanel titlePanel = new JPanel();
	static JLabel titleLabel = new JLabel("");
	static JPanel mainPanel = new JPanel();
	static JScrollPane mainScroller = new JScrollPane(mainPanel);
	static JButton btns[] = new JButton[10];
	{
		btns[0] = new JButton("Length  >");
		btns[1] = new JButton("Volume  >");
		btns[2] = new JButton("Speed  >");
		btns[3] = new JButton("Area  >");
		btns[4] = new JButton("Weight  >");
		btns[5] = new JButton("  >");
		btns[6] = new JButton("Temperature  >");
		btns[7] = new JButton("  >");
		btns[8] = new JButton("e  >");
		btns[9] = new JButton("  >");
	}
public Converter()
	{
		super("Conversion Calculator");
		setVisible(true);
		setLocation(200,200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		pack();
	
		//added this to ensure that the Frame closes properly	
		addWindowListener(  
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{	
					System.exit(0);
				}
			}
			);
		setupLayout(getContentPane());
	}
public void setupLayout(Container pane)
	{
		//add ActionListener to buttons
		for (int i = 0; i < btns.length; i++)
			btns[i].addActionListener(this);
		
		titlePanel.setBackground(new Color(46,139,87));
		Border raisedBorder = BorderFactory.createRaisedBevelBorder();
		titlePanel.setBorder(raisedBorder);
		titlePanel.setSize(350, 35);
		titlePanel.add(titleLabel);
		titleLabel.setText("Units");
		Font titleFont = new Font("Arial", Font.BOLD, 15);
		titleLabel.setFont(titleFont);
		titleLabel.setForeground(Color.WHITE);
		add(titlePanel);
		titlePanel.setVisible(true);
		titleLabel.setVisible(true);
		mainPanel.setLayout(new GridLayout(0, 1, 0, 5));
		for (int i = 0; i < btns.length; i++)
			mainPanel.add(btns[i]);
		mainPanel.setBorder(BorderFactory.createEmptyBorder(45,45,45,45));
		
		add(mainScroller);
		mainScroller.getVerticalScrollBar().setUnitIncrement(5);
	}
public static void main(String args[]) 
	//creating the JFrame window
	{ 
		JFrame nFrame = new Converter();
		nFrame.setSize(350,400);
	}
}

It's because you used the default layout in the frame, which is FlowLayout. Try BorderLayout and put the the title panel in NORTH and the mainPanel in CENTER.

But I want the buttons to have the GridLayout? Can you have two Layouts for one JPanel?

You can only have a single layout per container, but you don't need to change your JPanel layout. I am referring to the JFrame layout.

public void setupLayout(Container pane) {
        setLayout(new BorderLayout());
...
        add(titlePanel, BorderLayout.NORTH);
...
        add(mainScroller, BorderLayout.CENTER);
...
    }

Thanks so much, it worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.