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?

Code:

Class:Frame

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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(900,700);
		frame.setLayout(new FlowLayout(FlowLayout.LEFT));
		frame.add(new Panel());
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
	}

}

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();
		
		bar.add(file);bar.add(format);bar.add(help);
		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; 
		add(descriptionHolder,c);

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

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



	}

	//start methods, actionListener, handlers etc here
	public void printThis(String s)
	{
		System.out.println(s);
	}

	private class closeScreen implements ActionListener 
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
	}

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

	}

}
mKorbel commented: nice question +11

Recommended Answers

All 3 Replies

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

but

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

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.