Hello forum, Vaironl here.
I have a small question, I'm making a new screen using the GridBagLayout, since my old screen was using absolute positioning.

I'm using a JScrollPane and setting a subPanel in it. THe subPanel contains 40 labels saying Ingredient.
For some reasong the Ingredient Scroller will take up the whole horizontal spaces offScreen.
Might the subPanel be causing this problem? how can I set a max size?



import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.*;

public class Frame {
	public static void main(String[] args)
		JFrame frame = new JFrame("Recipe Application");
		frame.setLayout(new FlowLayout(FlowLayout.LEFT));
		frame.add(new Panel());


Class: Panel

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.SplashScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import javax.swing.*;

public class Panel extends JPanel
	//Variables Start here
	JMenuBar bar = new JMenuBar();
	JMenu file  = new JMenu("File"),format = new JMenu("Format"),help = new JMenu("Help");
	JMenuItem close = new JMenuItem("Exit"), about = new JMenuItem("About");

	JLabel  mainLabels[] = new JLabel[5]; JTextField mainFields[] = new JTextField[2];
	JComboBox ratingBox = new JComboBox(), servingBox = new JComboBox();

	JEditorPane description = new JEditorPane();
	JScrollPane descriptionHolder = new JScrollPane(description);

	JLabel ingredientLabels[] = new JLabel[40];
	JPanel subPanel = new JPanel();
	JScrollPane ingredientScroll = new JScrollPane(subPanel);

	//variables end

	public Panel()
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		GridBagConstraints c2 = new GridBagConstraints();
		help.add(about); file.add(close);
		close.addActionListener(new closeScreen());
		about.addActionListener(new About());

		c.gridx=0; c.gridy=0; c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.NORTH;
		c.weightx = 1.0; Insets insets = new Insets(0, 0,40,0); c.insets = insets;
		add(bar, c);

		insets = new Insets(0, 40,20,0); c.insets = insets;
		for(int set=0;set<mainLabels.length;set++)
			c.gridy++;// move the next component one grid down
			mainLabels[set] = new JLabel(); 

			if(set==0){ mainLabels[set].setText("Recipe's Name:");  c.ipady=0;mainFields[set] = new JTextField(20);
			c.gridx=1; add(mainFields[set],c ); c.gridx=0;}

			else if(set==1){mainLabels[set].setText("Author's Name:"); c.gridx=1; mainFields[set] = new JTextField(20);
			add(mainFields[set], c);	c.gridx=0;}

			else if(set==2){mainLabels[set].setText("Serving Size:"); c.gridx =1; for(int items=0;items<60;items++)
				servingBox.addItem(items+1); add(servingBox,c);	c.gridx=0;}

			else if(set==3){mainLabels[set].setText("Rating:"); c.gridx=1; for(int item=0;item<5;item++){ratingBox.addItem(item+1);}
			add(ratingBox, c);	c.gridx=0;}

			else if(set==4){mainLabels[set].setText("Description:");}

			add(mainLabels[set], c); // this can cause problems


		c.ipady=100; c.ipadx=200;
		c.gridy++;	c.gridx=1; 

		c.gridy++; c.gridx=0;
		add(new JLabel("Ingredients:"),c);
		subPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

		for(int i=0;i<40;i++){subPanel.add(new JLabel("Ingredients"));}


	//start methods, actionListener, handlers etc here
	public void printThis(String s)

	private class closeScreen implements ActionListener 
		public void actionPerformed(ActionEvent e)

	private class About implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(null, "Made by: Vairon",
					"About the recipe organizer",



Edited by vaironl: n/a

Votes + Comments
nice question
5 Years
Discussion Span
Last Post by mKorbel

In Panel.java, as the new last line of the constructor, type this:

ingredientScroll.setPreferredSize(new Dimension(200, 100));

Of course, you may want to interrogate the parent (Frame) to get the dimensions and set the size of the JScrollPane appropriately.


In Panel.java, as the new last line of the constructor, type this:

ingredientScroll.setPreferredSize(new Dimension(200, 100));

Of course, you may want to interrogate the parent (Frame) to get the dimensions and set the size of the JScrollPane appropriately.

Thanks!, it worked. For some reason when the Width value is 200, it still extends offscreen. Is this why you wanted me to get the parent component?

Is there anyways to set the size using the GridBagLayout?

Nonetheless, thanks for providing me with a solution, the thread is solved.


not this is by forcing frame.setSize(900,700);, that real reason why ... use pack instead

however you can setSize()


1) don9t use reserved words as ClassName, rename

Frame ---> MyFrame
panel ---> MyPanel

2) class name start with UpperCase

3) void or method with lowerCase

4) if you want ot see Bars then you have to put JPanel to the JScrollPane, this JPanel must have wider size than Container,

5) don't setPreferredSize for JScrollpane, is useless for used LayoutManager

Edited by mKorbel: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.