vaironl 11 Junior Poster in Training

Hello forum, Vaironl here.

I have a little problem, in a fairly big piece of code.

I'm making a recipe application, which is working fine, but there is one minor error. If I minimize the screen and restore it, some of the components inside it are somehow re sizing of the JFrame. any ideas here is the code.

Main class/Frame

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.io.ObjectInputStream.GetField;

import javax.swing.*;

public class Frame {
	private static final int WIDTH=900;
	private static final int HEIGHT=800;
	private static JFrame frame = new JFrame("Recipe Application");
	private static final Panel panel = new Panel();
	public static void main(String[] args)
	{
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(WIDTH,HEIGHT);
		frame.setLayout(new FlowLayout(FlowLayout.LEFT));
		frame.add(panel);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		
		
	}

}

Panel class

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
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.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

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


	private JTextField recipeField, authorField;
	private JTextField ingredientField[] = new JTextField[40], amountField[] = new JTextField[40],
			unitField[] = new JTextField[40];
	private JComboBox ratingBox = new JComboBox(), servingBox = new JComboBox();

	private JEditorPane descriptionEditor = new JEditorPane();
	private JScrollPane descriptionHolder = new JScrollPane(descriptionEditor);

	private JPanel subPanel = new JPanel();
	private JScrollPane ingredientScroll = new JScrollPane(subPanel);

	private Recipe recipe = new Recipe();

	//variables end

	public Panel()
	{
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		bar.add(file);bar.add(format);bar.add(help);
		help.add(about); file.add(open); file.add(save); file.add(close); 

		close.addActionListener(new closeAction()); save.addActionListener(new saveAction());
		open.addActionListener(new openAction());

		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<40;set++)
		{
			c.gridy++;// move the next component one grid down

			if(set==0){add( new JLabel("Recipe's Name:"),c);  c.ipady=0; recipeField = new JTextField(20);
			c.gridx=1; add(recipeField,c ); c.gridx=0;}

			else if(set==1){add(new JLabel("Author's Name:"),c); c.gridx=1; authorField = new JTextField(20);
			add(authorField, c); c.gridx=0;}

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

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

			else if(set==4){add(new JLabel("Description:"),c);
			c.ipady=150; c.ipadx=200;
			c.gridx=1; 
			add(descriptionHolder,c);}


		}

		c.gridy++; c.gridx=1;

		subPanel.setLayout(new GridBagLayout());
		GridBagConstraints c2 = new GridBagConstraints();

		c2.gridx =0; c2.gridy++;
		add(ingredientScroll,c);
		ingredientScroll.setPreferredSize(new Dimension(this.getWidth(), 150));

		for(int set=0;set<40;set++)
		{
			ingredientField[set] = new JTextField(15); amountField[set] = new JTextField(4);
			unitField[set] = new JTextField(4);

			subPanel.add(new JLabel("Ingredient "+(set+1)+":"), c2); c2.gridx++;
			subPanel.add(ingredientField[set],c2); c2.gridx++;

			subPanel.add(new JLabel("Amount:"), c2);c2.gridx++;
			subPanel.add(amountField[set],c2); c2.gridx++;

			subPanel.add(new JLabel("Unit:"), c2);c2.gridx++;
			subPanel.add(unitField[set],c2); c2.gridx++;

			c2.gridx=0;
			c2.gridy++;
		}


	}

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

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

	private class saveAction implements ActionListener 
	{
		public void actionPerformed(ActionEvent e)
		{
			boolean allowable = true;

			allowable = setRecipeVariables(recipe);


			if(allowable == true)
			{
				JFileChooser sc = new JFileChooser();
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
						"Text Files", "txt");
				sc.setFileFilter(filter);

				int returnValue = sc.showSaveDialog(null);


				if(returnValue == JFileChooser.APPROVE_OPTION)
				{
					try {
						FileWriter writer;
						String blankLine = System.getProperty("line.separator");

						if(String.valueOf(sc.getSelectedFile()).contains(".txt"))
						{
							writer = new FileWriter(sc.getSelectedFile());
						}
						else
						{
							writer = new FileWriter(sc.getSelectedFile()+".txt");
						}

						writer.write(recipe.getRecipeName() + "%" + recipe.getAuthor() + "%" + recipe.getServing()
								+ "%" + recipe.getRating() + "%" + blankLine);

						for(int current=0;current<recipe.getIngredients().length;current++)
						{

							if(current == 0)
							{
								writer.write(recipe.getIngredients()[current]);	
							}
							else if(current == recipe.getIngredients().length-1)
							{
								if(recipe.getIngredients()[current].isEmpty() == false)
								{
									writer.write("," + blankLine + recipe.getIngredients()[current] + "%" );
								}
								else
								{
									writer.write("%" + blankLine);
								}
							}

							else
							{
								if(recipe.getIngredients()[current].isEmpty() == false)
								{
									writer.write("," + blankLine + recipe.getIngredients()[current] );
								}
							}

						}
						
						for(int current=0;current<recipe.getAmounts().length;current++)
						{

							if(current == 0)
							{
								writer.write(recipe.getAmounts()[current].toString());	
							}
							else if(current == recipe.getAmounts().length-1)
							{
								if(recipe.getAmounts()[current].equals(0.0) == false)
								{
									writer.write("," + blankLine + recipe.getAmounts()[current] + "%" );
								}
								else
								{
									writer.write("%" + blankLine);
								}
							}

							else
							{
								if(recipe.getAmounts()[current].equals(0.0) == false)
								{
									writer.write("," + blankLine + recipe.getAmounts()[current]);
								}
							}

						}
						
						for(int current=0;current<recipe.getUnits().length;current++)
						{

							if(current == 0)
							{
								writer.write(recipe.getUnits()[current].toString());	
							}
							else if(current == recipe.getUnits().length-1)
							{
								if(recipe.getUnits()[current].isEmpty() == false)
								{
									writer.write("," + blankLine + recipe.getUnits()[current] + "%" );
								}
								else
								{
									writer.write("%" + blankLine);
								}
							}

							else
							{
								if(recipe.getUnits()[current].isEmpty() == false)
								{
									writer.write("," + blankLine + recipe.getUnits()[current]);
								}
							}

						}
						
						writer.write(recipe.getDescription() + "%");

						writer.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}

		}
	}

	private class openAction implements ActionListener 
	{
		public void actionPerformed(ActionEvent e)
		{	
			JFileChooser sc = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter(
					"Text Files", "txt");
			sc.setFileFilter(filter);

			int returnValue = sc.showOpenDialog(null);

			if(returnValue == JFileChooser.APPROVE_OPTION)
			{
				try {
					Scanner scanner = new Scanner(new File(String.valueOf(sc.getSelectedFile())));
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	private boolean setRecipeVariables(Recipe recipe)
	{
		boolean allowed = true;

		if(recipeField.getText().isEmpty()|| authorField.getText().isEmpty() || 
				descriptionEditor.getText().isEmpty() || ingredientField[0].getText().isEmpty() ||
				amountField[0].getText().isEmpty() || unitField[0].getText().isEmpty())
		{
			JOptionPane.showMessageDialog(null, "One or more of the required fields are empty"); 
			allowed = false;
		}
		else
		{
			
			recipe.setRecipeName(recipeField.getText());

			recipe.setAuthorName(authorField.getText());

			recipe.setServing((Integer) servingBox.getSelectedItem());
			
			recipe.setRating(Integer.valueOf(String.valueOf(ratingBox.getSelectedItem())));

			recipe.setRecipeDescription(descriptionEditor.getText());

			for(int set=0;set<recipe.getIngredients().length;set++)
			{
				if(ingredientField[set].getText().isEmpty() == false)
				{
					recipe.setIngredients(ingredientField[set].getText(), set);
				}
				else{
					recipe.setIngredients("", set);
				}
			}

			for(int set=0;set<recipe.getAmounts().length;set++)
			{
				if(amountField[set].getText().isEmpty() == false)
				{
					try{
						recipe.setIngredientAmounts(Double.valueOf(amountField[set].getText()), set);
					}
					catch(NumberFormatException ex)
					{
						JOptionPane.showMessageDialog(null,"The amount fields must be numbers, decimals are\n accepted");
						allowed = false;
						break;
					}
				}
				else{
					recipe.setIngredientAmounts(0.0, set);
				}
			}

			for(int set=0;set<recipe.getUnits().length;set++)
			{

				if(unitField[set].getText().isEmpty() == false)
				{
					recipe.setIngredientUnits(unitField[set].getText(), set);
				}
				else{
					recipe.setIngredientUnits("", set);
				}

			}

		}

		return allowed;
	}

}

Recipe class

public class Recipe {
	private String recipeName,authorName,recipeDescription;
	private String[] ingredientName = new String [40], ingredientUnits = new String [40];
	private Double[] ingredientAmounts = new Double [40];
	private int ratingValue,servingSize;
	
	public Recipe()
	{
		recipeName = "";
		authorName = "";
		ratingValue= 0;
		servingSize= 0;
		recipeDescription = "";
		
		for(int init=0;init<ingredientName.length;init++)
		{
			ingredientName[init] = "";
			ingredientUnits[init] = "";
			ingredientAmounts[init] = 0.0;
		}
	}
	
	public void setRecipeName(String rName)
	{recipeName = rName;}
	
	public void setAuthorName(String aName)
	{authorName = aName;}
	
	public void setIngredients(String ingredients, int location)
	{ingredientName[location] = ingredients;}

	public void setIngredientUnits(String uNames, int location)
	{ingredientUnits[location] = uNames;}
	
	public void setIngredientAmounts(Double aValue, int location)
	{ingredientAmounts[location] = aValue;}
	
	public void setRecipeDescription(String rDescription)
	{recipeDescription = rDescription;}
	
	public void setRating(int rValue)
	{ratingValue = rValue;}
	
	public void setServing(int sSize)
	{servingSize = sSize;}
	
	public String getRecipeName()
	{return recipeName;}
	
	public String getAuthor()
	{return authorName;}
	
	public String[] getIngredients()
	{return ingredientName;}
	
	public String[] getUnits()
	{return ingredientUnits;}
	
	public Double[] getAmounts()
	{return ingredientAmounts;}
	
	public String getDescription()
	{return recipeDescription;}
	
	public int getRating()
	{return ratingValue;}
	
	public int getServing()
	{return servingSize;}
	
}
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.