6 Replies - 347 Views - Last Post: 12 February 2012 - 05:54 PM Rate Topic: -----

#1 perhapsiam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-February 12

My code runs but the next button is goofy

Posted 12 February 2012 - 10:51 AM

For some reason my next button does 1,3,2 instead of 1,2,3. I am not getting an error, it shows up in the GUI panel when I run it. Also, not that it is a big deal, but how would I move my buttons to the bottom instead of having them to the side? Thank you for any help you can give me.

import javax.swing.*;

import java.awt.event.*;
public class Inventory5 extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2686376524379794229L;
	private Inventory i;
	private JTextArea txt;
	private int view = 0;

	public static void main(String[] args) {
		Inventory5 gui = new Inventory5();
		gui.pack();
		gui.setVisible(true);
	}
	public void showDVD() {
		txt.setText("DVD Details:\n");
			txt.append(i.get(view) + "\n");
		
		txt.append(String.format("\nTotal value = $%.2f", i.totalValue()));
		

	}
	
	public Inventory5() {
		super("DVD");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Product p1 = new Product(1, "Ice Age", 10, 19.99, "PG");
		Product p2 = new Product(2, "Uncle Buck", 7, 9.99, "PG-13");
		Product p3 = new Product(3, "Shrek", 23, 12.99, "PG");
		
		i = new Inventory(3);
		i.add(p1, 0);
		i.add(p2, 1);
		i.add(p3, 2);
		
		i.sort();

			for (int k = 0; k < 3; k++) {
			System.out.println(i.get(k));
		}
		
		System.out.println();
		
	
		System.out.printf("Total value = $%.2f", i.totalValue());
		
		
		
	
		JPanel panel = new JPanel();
		txt = new JTextArea(20,25);
		txt.setEditable(false);
		panel.add(txt);
		
		JButton next = new JButton("Next");
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (view < i.size()-1) view++; 
				showDVD();
					}
		});
		JButton previous = new JButton("Previous");
		previous.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (view > 0) view--;
				else view = i.size()-1;
				showDVD();
			}
		});
		JButton first = new JButton("First");
		first.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				view = 0;
				showDVD();
			}
		});
		JButton last = new JButton("Last");
		last.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				view = i.size()-1;
				showDVD();
			}
		});
		panel.add(next);
		panel.add(previous);
		panel.add(first);
		panel.add(last);
		
		getContentPane().add(panel);
		
		showDVD();
	}

}

This post has been edited by smohd: 12 February 2012 - 10:59 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: My code runs but the next button is goofy

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: My code runs but the next button is goofy

Posted 12 February 2012 - 10:56 AM

Too much code without code tags. Please fix or repost it correctly.
Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: My code runs but the next button is goofy

Posted 12 February 2012 - 11:41 AM

You might want to change this to avoid hitting an OutofBounds Exception


if (view < i.size()-1) view++;
                showDVD();



Instead of seeing if it is below See if it is at size()-1 and if it is reset view to 0 else increase it by 1 like you did. That way if it is on the last one then it will cycle back to the first. Change that and lets see what it is doing.

if(view >= i.size()-1) // Go ahead and use >=, just incase something funky happens and it went out of bounds for some reason. Better safe then sorry!
    // set view to equal 0
else // increase view by one
showDVD();


Also, I do not know how you are sorting it because we do not have the inventory object code but you are sorting it and i'm assuming by Alphabetical Order.
        Product p1 = new Product(1, "Ice Age", 10, 19.99, "PG");
        Product p2 = new Product(2, "Uncle Buck", 7, 9.99, "PG-13");
        Product p3 = new Product(3, "Shrek", 23, 12.99, "PG");
        i = new Inventory(3);
        i.add(p1, 0);
        i.add(p2, 1);
        i.add(p3, 2);

        i.sort();



So when you are expecting 1,2,3 - it is showing 1 , 3 , 2.. that is because it is sorting and the 2nd one entered is getting moved to the 3rd position. because S comes before U. Thus accounting for your "goofyness"

(FYI, I am talking about the DVDs entered, not index which is why I used 1(first) 2 (second) 3 (third) instead of 0,1,2. )

This post has been edited by Fuzzyness: 12 February 2012 - 11:43 AM

Was This Post Helpful? 1
  • +
  • -

#4 perhapsiam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-February 12

Re: My code runs but the next button is goofy

Posted 12 February 2012 - 04:08 PM

You are all great! I think I get it now, my program is sorting alphabetically rather than by number; duh to me :) When my GUI window pops up (I don't know how to attach a screenshot) is there anyway to get my buttons on the bottom of my panel? I hate the way it goes off to the side. I can't wait to get as good as all of you on this stuff, it is fun building applications!
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: My code runs but the next button is goofy

Posted 12 February 2012 - 05:04 PM

Sure thing, as for the button locations.. which Layout are you using? The default Border layout?

If you are using border lay out, when you add it you actually add another parameter.

Ex. container.add(button1, BorderLayout.SOUTH);

Read up on it a bit, fairly easy. This should help ye out.
Was This Post Helpful? 0
  • +
  • -

#6 perhapsiam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-February 12

Re: My code runs but the next button is goofy

Posted 12 February 2012 - 05:46 PM

Thank you so much Fuzzy! And thank you for the link; it is a fantastic resource! :clap:
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: My code runs but the next button is goofy

Posted 12 February 2012 - 05:54 PM

Sure thing buddy. Good luck in the future programs!

Happy Coding!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1