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