I'm supposed to be making an inventory program with a logo. I can get the inventory part working fine in a jframe, and I can get the logo working fine in jpanel, but if I combine them it starts acting really weird. Every time I click a button, it generates a new series of buttons and labels...(Sorry about the mess, I'm trying to mesh two pieces together)

logo.java

import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import javax.swing.JPanel;
import java.util.Random;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;


public class logo extends JPanel
{
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	private JLabel label4;
	private JLabel label5;
	private JButton buttonFirst;
	private JButton buttonLast;
	private JButton buttonPrevious;
	private JButton buttonNext;
	private Container container;
	private FlowLayout layout;
	private int counter;

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;
	/*	GeneralPath shape = new GeneralPath();

		shape.moveTo(xPoints[0], yPoints[0]);

		for (int count = 1; count < xPoints.length; count++)
			shape.lineTo(xPoints[count], yPoints[count]);

		shape.closePath();

		g2d.translate(200, 200);

		for (int count = 1; count <= 20; count++)
		{
			g2d.rotate(Math.PI/10.0);

			g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

			g2d.fill(shape);
		}*/

		g2d.setPaint(new GradientPaint(5, 30, Color.BLUE, 35, 100, Color.YELLOW, true));
		g2d.fill(new Ellipse2D.Double(5, 30, 65, 100));

		setLayout(new FlowLayout());

    	final DetailedDvd dvds[] = new DetailedDvd[3];
    	dvds[0] = new DetailedDvd(168769,"DVD", 16, 5.99, "LOTR 1"); //initialize data with the constructor
    	dvds[2] = new DetailedDvd(165770,"VHS", 10, 9.99, "LOTR 2");
    	dvds[1] = new DetailedDvd(168770,"Blu", 19, 7.99, "LOTR 3");
 		Dvd.sortArray(dvds);

		String s = new String();
    	s=s.format("Total value of inventory with %%5 restocking fee is: $%.2f", DetailedDvd.getValue());

		label1 = new JLabel("Name  Price  Stock   Prod. #   Value+%5   Title");//create heading label
		add (label1);

		label2 = new JLabel(dvds[0].returnString());//create the information labels
		add (label2);

		//**********I"m unsure if the entire inventory is supposed to be displaye.  If it is, just uncomment the
		//following code and it should all show up on the first screen**********

		/*label4 = new JLabel(dvds[1].returnString());
		add (label4);
		label5 = new JLabel(dvds[2].returnString());
		add (label5);*/

		label3 = new JLabel();//create total inventory label
		label3.setText(s+"          ");
		add (label3);

		buttonFirst = new JButton("<<");
		add(buttonFirst);
		buttonFirst.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					counter = 0;
					label2.setText(dvds[counter].returnString());
				}
			}
			);

		buttonPrevious = new JButton("<");
		add(buttonPrevious);
		buttonPrevious.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if (counter > 0)
					{
						counter-=1;
						label2.setText(dvds[counter].returnString());
					}
					else if (counter == 0)
					{
						counter = 2;
						label2.setText(dvds[counter].returnString());
					}
				}
			});

		buttonNext = new JButton(">");
		add(buttonNext);
		buttonNext.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if (counter < 2)
					{
						counter+=1;
						label2.setText(dvds[counter].returnString());
					}
					else if (counter == 2)
					{
						counter = 0;
						label2.setText(dvds[counter].returnString());
					}
				}
			});

		buttonLast = new JButton(">>");
		add(buttonLast);
 		buttonLast.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					counter = 2;
					label2.setText(dvds[counter].returnString());
				}
			}
			);

	}
}

DetailedDvd.java

class DetailedDvd extends Dvd
{
	private String title;

	public DetailedDvd(int num, String Name, int Units, double Price, String Title)
	{
		super(num, Name, Units, Price);
		setTitle(Title);
	}

	public void setTitle(String Title)
	{
		title = Title;
	}
	public String getTitle()
	{
		return title;
	}

	public void print()
	{
		System.out.printf("%s %5s%.2f %6d %9d %5s%.2f %8s\n", getName(), "$", getPrice(), getUnits(), getItemNumber(), "$", valueInventory(), getTitle());

	}

 	public double valueInventory()
 	{
 		return getUnits()*getPrice()*1.05;
 	}

 	public String returnString()
 	{
 		String s = new String();
 		return s.format("%s %5s%.2f %6d %9d %5s%.2f %8s\n", getName(), "$", getPrice(), getUnits(), getItemNumber(), "$", valueInventory(), getTitle());
 	}
}

Dvd.java

public class Dvd
 {
 	private int itemNumber;  //variable to store the item number
 	private String name;  //Contains the name of the item
 	private int units;  //Contains how many units are in stock
 	private double price;  //Contains the price of the object in stock
 	private static double value;//store value of the entire inventory

 	public Dvd(int num, String Name, int Units, double Price)//construcor to initialize the variables
 	{
 		setItemNumber(num);
 		setName(Name);
 		setUnits(Units);
 		setPrice(Price);
 		setValue(getValue()+valueInventory());
 	}
	//Methods to set all the variables
 	public void setItemNumber(int num)
 	{
 		itemNumber = num;
 	}

 	public void setName(String Name)
 	{
 		name = Name;
 	}

 	public void setUnits(int amount)
 	{
 		units=amount;
 	}

 	public void setPrice(double Price)
 	{
 		price = Price;
 	}
 	public static void setValue(double v)
 	{
 		value = v;
 	}
 	public static double getValue()
 	{
 		return value;
 	}
	//Methods to return all the variables
 	public int getItemNumber()
 	{
 		return itemNumber;
 	}

 	public String getName()
 	{
 		return name;
 	}

 	public int getUnits()
 	{
 		return units;
 	}

 	public double getPrice()
 	{
 		return price;
 	}
	//method to calculate the value of the inventory
 	public double valueInventory()
 	{
 		return getUnits()*getPrice();
 	}
 	//sort the array
 	public static void sortArray(Dvd b[])
 	{
	Dvd holder;

	for(int i=0;i<=2;i++)
		for (int j=i+1;j<=2;j++)
			if (b[i].getName().compareToIgnoreCase(b[j].getName())>0)
			{
				holder=b[i];
				b[i]=b[j];
				b[j]=holder;
			}
 	}
	//Print out the inventory
 	public void print()
 	{

 		System.out.printf("%s %5s%.2f %6d %9d %5s%.2f\n", getName(), "$", getPrice(), getUnits(), getItemNumber(), "$", valueInventory());
 	}
  	public String returnString()
  	{
  		String s = new String();
		return s.format("%s %5s%.2f %6d %9d %5s%.2f\n", getName(), "$", getPrice(), getUnits(), getItemNumber(), "$", valueInventory());
  	}
 }

Inventorygui.java

/**
 * @(#)Inventorygui.java
 *
 * Inventorygui application
 *
 * @author
 * @version 1.00 2011/11/24
 */
import javax.swing.JFrame;

public class Inventorygui {

    public static void main(String[] args) {/*
    	DetailedDvd dvds[] = new DetailedDvd[3];
    	dvds[0] = new DetailedDvd(168769,"DVD", 16, 5.99, "LOTR 1"); //initialize data with the constructor
    	dvds[2] = new DetailedDvd(165770,"VHS", 10, 9.99, "LOTR 2");
    	dvds[1] = new DetailedDvd(168770,"Blu", 19, 7.99, "LOTR 3");
 		System.out.printf("%s %8s %7s %10s %5s %8s\n","Name","Price","Stock","Product #", "Value+%5", "Title");//print heading
 		Dvd.sortArray(dvds);

    	for (DetailedDvd dvd : dvds)
    	{
    		dvd.print();//print out the contents of dvd
    	}
    	System.out.printf("Total value of inventory with %%5 restocking fee is: $%.2f", DetailedDvd.getValue());*/
		/*logo l= new logo();
    	gui Gui = new gui(l);
    	Gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	Gui.setSize(400, 250);
    	Gui.setVisible(true);*/
    	logo l = new logo();
    	JFrame frame = new JFrame();
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setSize(400, 400);
    	frame.add(l);
    	frame.setVisible(true);

    }
}

Thanks

With the code you posted it looks like you lost a } when you commented out all that code from line 40 in logo. It now looks like you have all your GUI creation code inside the paintComponent method, so every time it repaints it creates new stuff.

commented: Really helpful +2

I was trying to stick it all into the brackets between 36-158. Lines 60-61 shouls be unbracketed (I'll change it now). How should it be split up?

I don't have time now to understand the whole structure of your code, but at a guess you have two methods there, a paintComponent that should finish around line 62 and an initialisation method that includes all the following code.

Thanks so much! I fixed it up as you said and it works beautifully.

import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import javax.swing.JPanel;
import java.util.Random;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;


public class logo extends JPanel
{
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	private JLabel label4;
	private JLabel label5;
	private JButton buttonFirst;
	private JButton buttonLast;
	private JButton buttonPrevious;
	private JButton buttonNext;
	private Container container;
	private FlowLayout layout;
	private int counter;

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;
	/*	GeneralPath shape = new GeneralPath();

		shape.moveTo(xPoints[0], yPoints[0]);

		for (int count = 1; count < xPoints.length; count++)
			shape.lineTo(xPoints[count], yPoints[count]);

		shape.closePath();

		g2d.translate(200, 200);

		for (int count = 1; count <= 20; count++)
		{
			g2d.rotate(Math.PI/10.0);

			g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

			g2d.fill(shape);
		}*/

		g2d.setPaint(new GradientPaint(5, 30, Color.BLUE, 35, 100, Color.YELLOW, true));
		g2d.fill(new Ellipse2D.Double(5, 30, 65, 100));
	}
	public logo()
	{

		setLayout(new FlowLayout());

    	final DetailedDvd dvds[] = new DetailedDvd[3];
    	dvds[0] = new DetailedDvd(168769,"DVD", 16, 5.99, "LOTR 1"); //initialize data with the constructor
    	dvds[2] = new DetailedDvd(165770,"VHS", 10, 9.99, "LOTR 2");
    	dvds[1] = new DetailedDvd(168770,"Blu", 19, 7.99, "LOTR 3");
 		Dvd.sortArray(dvds);

		String s = new String();
    	s=s.format("Total value of inventory with %%5 restocking fee is: $%.2f", DetailedDvd.getValue());

		label1 = new JLabel("Name  Price  Stock   Prod. #   Value+%5   Title");//create heading label
		add (label1);

		label2 = new JLabel(dvds[0].returnString());//create the information labels
		add (label2);

		//**********I"m unsure if the entire inventory is supposed to be displaye.  If it is, just uncomment the
		//following code and it should all show up on the first screen**********

		/*label4 = new JLabel(dvds[1].returnString());
		add (label4);
		label5 = new JLabel(dvds[2].returnString());
		add (label5);*/

		label3 = new JLabel();//create total inventory label
		label3.setText(s+"          ");
		add (label3);

		buttonFirst = new JButton("<<");
		add(buttonFirst);
		buttonFirst.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					counter = 0;
					label2.setText(dvds[counter].returnString());
				}
			}
			);

		buttonPrevious = new JButton("<");
		add(buttonPrevious);
		buttonPrevious.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if (counter > 0)
					{
						counter-=1;
						label2.setText(dvds[counter].returnString());
					}
					else if (counter == 0)
					{
						counter = 2;
						label2.setText(dvds[counter].returnString());
					}
				}
			});

		buttonNext = new JButton(">");
		add(buttonNext);
		buttonNext.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if (counter < 2)
					{
						counter+=1;
						label2.setText(dvds[counter].returnString());
					}
					else if (counter == 2)
					{
						counter = 0;
						label2.setText(dvds[counter].returnString());
					}
				}
			});

		buttonLast = new JButton(">>");
		add(buttonLast);
 		buttonLast.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					counter = 2;
					label2.setText(dvds[counter].returnString());
				}
			}
			);

	}
}

One last quustion, how can I ensure that the logo is either on top or below the buttons?

Never mind, I got it...

Not exactly sure what you mean... is that like in front of/behind, or higher than/lower than (ie not overlapping logo & buttons). If its the second, then you could create 2 JPanels - one with the logo and the other containing all the buttons, and place one of these panels above (higher than) the other.

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.