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.


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;

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");
		//added this to ensure that the Frame closes properly	
			new WindowAdapter()
				public void windowClosing(WindowEvent e)
public void setupLayout(Container pane)
		//add ActionListener to buttons
		for (int i = 0; i < btns.length; i++)
		titlePanel.setBackground(new Color(46,139,87));
		Border raisedBorder = BorderFactory.createRaisedBevelBorder();
		titlePanel.setSize(350, 35);
		Font titleFont = new Font("Arial", Font.BOLD, 15);
		mainPanel.setLayout(new GridLayout(0, 1, 0, 5));
		for (int i = 0; i < btns.length; i++)
public static void main(String args[]) 
	//creating the JFrame window
		JFrame nFrame = new Converter();

Recommended Answers

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.

Jump to Post

All 4 Replies

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 learning and sharing knowledge.