Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: File handling in a GUI!

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question File handling in a GUI!

    Hi Everyone,

    I created a GUI using Java with Buttons, Pictures and labels.
    When one of the buttons is pressed it increases the price, which can be seen by the labels increasing the necessary amount.

    What I would like to be able to do in my GUI is allow the user to record details of the sale and be able to display the sales of past customers to date, by this I mean when I click one of the buttons it will record the amount of that item into a file and when I press the another button, not the same button as the items, it will show the user the existing orders that have been taken in the past.

    Here is my Code:

     
    //Importing needed classes
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    //The Main Class
    public class CoffeeShop extends JFrame
    {
    	//Initialising the Buttons and Labels
    	private JLabel instruction1;
    	private JLabel instruction2;
    	private JLabel instruction3;
    	private JLabel label;
    	private JLabel label1;
    	private JLabel label2;
      	private JLabel label3;
      	private JLabel label4;
      	private JLabel label5;
      	private JButton button;
    	private JButton button1;
    	private JButton button2;
      	private JButton button3;
      	private JButton button4;
      	private JButton button5;
     
    	//Initialising the Result Labels
      	private JLabel resultLabel;
      	private JLabel resultLabel1;
      	private JLabel resultLabel2;
      	private JLabel resultLabel3;
      	private JLabel resultLabel4;
      	private JLabel resultLabel5;
     
      	//Initialising Items on sale
       	private double coffee;
       	private double hotchocolate;
       	private double cupcakes;
       	private double scones;
       	private double tea;
       	private double fanta;
     
    	//Initialising Images of Items
      	private ImageIcon coffee1;
        private ImageIcon hotchocolate1;
        private ImageIcon cupcakes1;
        private ImageIcon scones1;
        private ImageIcon greentea;
      	private ImageIcon fanta1;
     
    	//Initialising the Screen Width and Length
    	private static final int FRAME_WIDTH = 780;
    	private static final int FRAME_HEIGHT = 400;
     
    	//Initialising the Initial cost and Interest Rates of the items on sale
    	private static final double INITIAL_COST = 0;
    	private static final double PRICE = 1.5;
        private static final double PRICE1 = 2.0;
    	private static final double PRICE2 = 1.0;
    	private static final double PRICE3 = 2.5;
    	private static final double PRICE4 = 3.5;
    	private static final double PRICE5 = 3.0;
     
    	public CoffeeShop()
    	{
    		super( "Caroline's Cafe" );	createComponents();
    		setSize(FRAME_WIDTH, FRAME_HEIGHT);
    	}
     
    //Creating the Click Listeners
    class ClickListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
     	   coffee = coffee + PRICE;
           resultLabel.setText("Total cost of your Coffee(s): " + coffee);
       }
    }
     
    class ClickListener1 implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    	   hotchocolate = hotchocolate + PRICE1;
           resultLabel1.setText("Total cost of your Hot Chocolate(s): " + hotchocolate);
    	}
    }
     
    class ClickListener2 implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
     	   cupcakes = cupcakes + PRICE2;
           resultLabel2.setText("Total cost of your Cupcake(s): " + cupcakes);
    	}
    }
     
    class ClickListener3 implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    	   scones = scones + PRICE3;
           resultLabel3.setText("Total cost of your Scone(s) with Jam: " + scones);
    	}
    }
     
    class ClickListener4 implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    	   tea = tea + PRICE4;
           resultLabel4.setText("Total cost of your Green Tea(s): " + tea);
    	}
    }
     
    class ClickListener5 implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    	   fanta = fanta + PRICE5;
           resultLabel5.setText("Total cost of your Fanta(s): " + fanta);
    	}
    }
     
    //Creating the Components
    private void createComponents()
    {
    	coffee1 = new ImageIcon (getClass().getResource("coffee.jpg"));
    	hotchocolate1 = new ImageIcon (getClass().getResource("HotChocolate.jpg"));
    	cupcakes1 = new ImageIcon (getClass().getResource("cupcakes.jpg"));
    	scones1 = new ImageIcon (getClass().getResource("scones.jpg"));
    	fanta1 = new ImageIcon (getClass().getResource("fanta.jpg"));
    	greentea = new ImageIcon (getClass().getResource("tea.jpg"));
     
    	button = new JButton("Coffee", coffee1);
    	ActionListener listener = new ClickListener();
    	button.addActionListener(listener);
    	label = new JLabel("Price $1.50");
     
    	button1 = new JButton("Mint Hot Chocolate", hotchocolate1);
    	ActionListener listener1 = new ClickListener1();
    	button1.addActionListener(listener1);
    	label1 = new JLabel("Price:$2.00");
     
    	button2 = new JButton("Cupcakes", cupcakes1);
    	ActionListener listener2 = new ClickListener2();
    	button2.addActionListener(listener2);
    	label2 = new JLabel("Price:$1.00");
     
    	button3 = new JButton("Scones with Jam", scones1);
    	ActionListener listener3 = new ClickListener3();
    	button3.addActionListener(listener3);
    	label3 = new JLabel("Price:$2.50");
     
    	button4 = new JButton("Green Tea", greentea);
    	ActionListener listener4 = new ClickListener4();
    	button4.addActionListener(listener4);
    	label4 = new JLabel("Price:$3.50");
     
    	button5 = new JButton("Fanta", fanta1);
    	ActionListener listener5 = new ClickListener5();
    	button5.addActionListener(listener5);
    	label5 = new JLabel("Price:$3.00");
     
    	resultLabel = new JLabel("Total cost of your Coffee(s): " + coffee);
    	resultLabel1 = new JLabel("Total cost of your Hot Chocolate(s): " + hotchocolate);
    	resultLabel2 = new JLabel("Total cost of your Cupcake(s): " + cupcakes);
    	resultLabel3 = new JLabel("Total cost of your Scone(s) with Jam: " + scones);
    	resultLabel4 = new JLabel("Total cost of your Green Tea(s): " + tea);
    	resultLabel5 = new JLabel("Total cost of your Fanta(s): " + fanta);
     
    	instruction1 = new JLabel("Click the below buttons to order:");
    	instruction2 = new JLabel("Here is the total of each item you bought:");
    	instruction3 = new JLabel("The Prices of each item are here:");
     
    	//Creating the main panel
    	JPanel panel = new JPanel();
     
    	//Setting the Page Layout
    	panel.setLayout( new GridLayout(7,3));
     
    	//Creating buttons, resultLabels and Labels
    	panel.add(instruction1);
    	panel.add(instruction2);
    	panel.add(instruction3);
     
    	panel.add(button);
    	panel.add(resultLabel);
    	panel.add(label);
     
    	panel.add(button1);
    	panel.add(resultLabel1);
    	panel.add(label1);
     
    	panel.add(button2);
    	panel.add(resultLabel2);
    	panel.add(label2);
     
    	panel.add(button3);
    	panel.add(resultLabel3);
    	panel.add(label3);
     
    	panel.add(button4);
    	panel.add(resultLabel4);
    	panel.add(label4);
     
    	panel.add(button5);
    	panel.add(resultLabel5);
    	panel.add(label5);
     
     
    	add(panel);
    }
    }
     
    import javax.swing.JFrame;
     
    public class CoffeeShopViewer
    {
    		public static void main(String[]args)
    		{
    			JFrame frame = new CoffeeShop();
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setVisible(true);
    		}
    	}

    Can anyone please help me?
    Last edited by pirates12; April 28th, 2014 at 09:52 AM. Reason: Updated


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File handling in a GUI!

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. java file handling
    By MartinMc in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 16th, 2012, 04:07 PM
  2. Problem netbeans gui button event handling
    By hiepa in forum AWT / Java Swing
    Replies: 7
    Last Post: December 3rd, 2012, 05:24 PM
  3. GUI Scientific Calculator. Please help! (Event Handling)
    By BlackGazer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 9th, 2011, 07:14 AM
  4. newbie GUI/ event handling problem
    By zyspt in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 10th, 2010, 11:36 AM
  5. Regarding File Handling
    By ravjot28 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:45 PM