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 4 of 4

Thread: Need my code to update a variable

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Need my code to update a variable

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class JVideo extends JFrame implements ActionListener
    {
    	final int FAVORITE_PRICE = 3;
    	final int LEAST_FAV_PRICE = 1;
    	final int REG_PRICE = 2;
     
    	int selectedArray;
    	int totalPrice;
     
    	JTextField checkoutTotal = new JTextField(10);
     
    	String[] movieStrings = {"Reservoir Dogs", "Memento", "Oldboy", "Inglorious Bastards", "Snatch",
    				 "Slumdog Millionaire", "District 9", "Ip Man", "Gran Torino", "The Matrix"};
    	JComboBox greenBox = new JComboBox(movieStrings);
     
    	JLabel explainSelector = new JLabel("Please use the drop down menu to select a movie to rent.");
    	JLabel welcome = new JLabel("Welcome to Greenbox");
     
     
     
    	public JVideo()
    	{
    		super("Greenbox Rentals");
     
     
    		greenBox.setSelectedIndex(5);
     
     
    		add(greenBox);
    		greenBox.addActionListener(this);
    		add(welcome);
    		add(explainSelector);
    		add(checkoutTotal);
    		checkoutTotal.setText("$" + totalPrice);
    		setLayout(new FlowLayout());
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    	public void actionPerformed(java.awt.event.ActionEvent e)
    	{
    		if(greenBox.getSelectedIndex() == 7)
    			totalPrice = FAVORITE_PRICE;
    		else
    		if(greenBox.getSelectedIndex() == 6)
    			totalPrice = LEAST_FAV_PRICE;
    		else
    		if(greenBox.getSelectedIndex() == 5)
    			totalPrice = LEAST_FAV_PRICE;
    		else
    			totalPrice = REG_PRICE;
    	}
     
    	public static void main(String[] args)
    	{
    		JVideo aFrame = new JVideo();
    		final int WIDTH = 400;
    		final int HEIGHT = 600;
    		aFrame.setSize(WIDTH, HEIGHT);
    		aFrame.setTitle("Greenbox Videos");
    		aFrame.setVisible(true);
    	}
     
    }

    Im having one vital issue with my code for a video renting program. It won't tell you how much the movie you are renting costs >.<
    I have no clue why, no matter how much i stare at the code. Is there another way I have to go about detecting which index of the array is selected? Or are my if statements not actually being updated and checked or are they not performing their action? UGGHH I just dont know anymore


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

    Default Re: Need my code to update a variable

    Where does the code set the price?
    After the price is set, where does the code show the price?

    Computers do statements one after the other. The program must do things the same way.

    This code shows the price BEFORE it is set. Wait on showing the price until AFTER the user has made his selection.

    To see when that happens add a println statement at the end of the listener method that prints out the value of totalPrice.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Olympaphibian89 (December 13th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Need my code to update a variable

    I tried putting my actionPerformed method before JVideo() (I think this is what you were referring to) but it didnt change anything, then I pulled my if statements out from there and put them right after greenBox.addActionListener hoping that it would help, setting a price after the user made a selection. But it just picks up the default Index positions price and displays that but won't update if the user selects a new movie...
    Any ideas?

    --- Update ---

    !!!!!
    NEVERMIND
    !!!!

    I got it to update! Thanks for the advice. I just kept moving things around until it worked. Ill clean up the code and repost in case anyone else would like a look

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need my code to update a variable

    I tried putting my actionPerformed method before JVideo()
    The order or position of methods in a class definition doesn't effect what order they are executed in.

    Adding print statements to different locations in the code can be used to show the order of execution.

    Glad you worked it out.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  2. How to compare variable with multiple variable?
    By mharck in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2012, 09:02 AM
  3. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. Editing open source code- tracing a value set to a concrete variable
    By a_posse_ad_esse in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 21st, 2011, 05:50 PM