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

Thread: having some issues displaying objects in my GUI

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default having some issues displaying objects in my GUI

    As of right now I am using eclipse to do everything with my program.

    I am not using the Java WebBuilder app, and at this time I don't want to. (learning practices)


    I cannot find very much support on how to get my application to call and display my objects..


    My application is suppose to work with a Next and Previous button and display the objects 1 at a time. It should allow me to navigate through the list with the button.


    ** Really I beleive I have most of everything correct. I am just having a pesky issue that has gotten me to the point of tearing my hair out. I am sure that once I get it i will feel stupid, BUT AS LONG AS I GET IT! I will be cool with it.


    ALSO please note that I take criticism very well and I am open to any other suggestions on top of what my main issue is.

    at this time I know my program may need a little more improvement and fluff taken out but I just need to get this to display my objects so i can continue refining this application.


    So far my program will give me these compiler errors.

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The constructor CDs(Inventory[]) is undefined

    at inventory.CDs.main(CDs.java:136)


    BEFORE YOU READ MY CODE

    THANKS! - I hope you all can give me a helping hand in my understanding!


    CDs Class
     
    package inventory;
     
    import java.util.*;
     
    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
     
     
     
    public class CDs extends JFrame{
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L; // serial version
     
     
    // declares var for currentIndex for button functionality
    	public int currentIndex = 0;
     
    	// creates cars for the GUI build
    	private JLabel labelName;
    	private JLabel labelID;
    	private JLabel labelStock;
    	private JLabel labelGenre;
    	private JLabel labelPrice;
    	private JLabel labelReStock;
    	private JLabel labelFval;
    	private JLabel labelTval;
     
    	public CDs(){// sets the GUI 
     
     
    		//creates the panel and title fields
     
    		JPanel panel = new JPanel();
    		panel.setLayout(null);
     
    		/// fields numbered 1-8 for easy matching with other functions and adding
    		labelName = new JLabel ("CD Name: ");//1
    		labelName.setFont(new Font("Serif", Font.BOLD, 14));
    		labelName.setBounds(0, 0, 200, 50);
     
     
    		labelID = new JLabel ("CD ID: ");//2
    		labelID.setFont(new Font("Serif", Font.BOLD, 14));
    		labelID.setBounds(0, 0, 200, 100);
     
    		labelGenre = new JLabel ("CD genre: ");//3
    		labelGenre.setFont(new Font("Serif", Font.BOLD, 14));
    		labelGenre.setBounds(0, 0, 200, 150);
     
    		labelStock = new JLabel ("Stock amount: ");//4
    		labelStock.setFont(new Font("Serif", Font.BOLD, 14));
    		labelStock.setBounds(0, 0, 200, 200);
     
    		labelPrice = new JLabel ("CD Price: ");//5
    		labelPrice.setFont(new Font("Serif", Font.BOLD, 14));
    		labelPrice.setBounds(0, 0, 200, 250);
     
    		labelReStock = new JLabel ("ReStock value: ");//6
    		labelReStock.setFont(new Font("Serif", Font.BOLD, 14));
    		labelReStock.setBounds(0, 0, 200, 300);
     
    		labelFval = new JLabel ("CD stock value: ");//7
    		labelFval.setFont(new Font("Serif", Font.BOLD, 14));
    		labelFval.setBounds(0, 0, 200, 350);
     
    		labelTval = new JLabel ("Total inventory value: ");//8
    		labelTval.setFont(new Font("Serif", Font.BOLD, 14));
    		labelTval.setBounds(125, 0, 200, 500);
     
    		// adds the fields
    		panel.add(labelName);//1
    		panel.add(labelID);//2
    		panel.add(labelGenre);//3
    		panel.add(labelStock);//4
    		panel.add(labelPrice);//5
    		panel.add(labelReStock);//6
    		panel.add(labelFval);//7
    		panel.add(labelTval);//8
     
    		getContentPane().add(panel);
     
    		/// creates the buttons
    		JButton previous = new JButton("Previous");
    		previous.setBounds(110, 300, 85, 20);
    		panel.add(previous);
     
    		JButton next = new JButton("Next");
    		next.setBounds(230, 300, 85, 20);
    		panel.add(next);
     
    		// sets the values for the window
    		setTitle("Your Inventory Program");
    		setSize(500,550);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
     
     
    		///// these are the active listeners for the button functions
    			previous.addActionListener(new ActionListener() {
    				@Override
    				public void actionPerformed(ActionEvent e) {
    					showPrevious();
    				}
    			});
    		next.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				showNext();
    			}
    		});
     
    		updateGUI(); /// calls the GUI display for object results
     
    	}// sets GUI
     
     
     
    	public static void main(String[] args){ // start main
     
     
    		Inventory[] CD = new Inventory[5]; // creates the variable CD for the array
    		/// CD inventory objects
    			CD[0] = new InvMore("Red Hot Chili Peppers", "Alternative", 1188, 25, 16.99);
    			CD[1] = new InvMore("Nirvana", "Classic Rock", 1177, 35, 16.99);
    			CD[2] = new InvMore("Styx", "Classic Rock", 1166, 31, 16.99);
    			CD[3] = new InvMore("Jay Z", "Hip Hop/Rap", 1155, 45, 16.99);
    			CD[4] = new InvMore("Nelly", "Hip Hop/Rap", 1144, 35, 16.99);
     
    		new CDs(CD).setVisible(true); // this should call the GUI
     
    	}// end main
     
     
    	// calls and prints the objects by specifics
    	public void updateGUI() {
    		InvMore item = CDs.get(currentIndex);
    	labelName.setText(String.valueOf(item.getName()));//1
    	labelID.setText(String.valueOf(item.getID()));//2
    	labelGenre.setText(String.valueOf(item.getGenre()));//3
    	labelStock.setText(String.valueOf(item.getStock()));//4
    	labelPrice.setText(String.format("$%.2f",item.getPrice()));//5
    	labelReStock.setText(String.format("$%.2f",item.getReStock()));//6
    	labelFval.setText(String.format("$%.2f",item.getcdVal()));//7
    	labelTval.setText(String.format("$%.2f",item.getcdVal()));//8
     
    	}// ends updateGUI
     
     
    	private void showNext() {  // method for the next button
    		if (currentIndex < 4 - 1) {
    			currentIndex++;
    			updateGUI();
    		}
    	}
     
    	private void showPrevious() { // method for the previous button
    		if (currentIndex > 0) {
    			currentIndex--;
    			updateGUI();
    		}
    	}
    }// end class


    Inventory Class
     
    package inventory;
     
    public class Inventory implements Comparable<Inventory>{ 
    // this is still set from when my application was not implementing GUI 
    //but displaying the objects through a sort method and a for statement
     
     
     
    		// variables
    		public String Name;
    		public int ID;
    		public int Stock;
    		public double Price;
    		public double ReStock;
    		public String Genre;
    		public double cdVal;
     
     
    			public Inventory(String Name, String Genre, int ID, int Stock, double Price) { // constructor
    				this.Name = Name;
    				this.ID = ID;
    				this.Stock = Stock;
    				this.Price = Price;
     
    			}// end constructor
     
     
    			public void setName (String Name){ // setter for name
    				this.Name = Name;
    			} // end setter
    			public String getName(){ // getter for name
    				return Name;
    			} // end getter
     
    			public void setID (int ID){ // setter for id
    				this.ID = ID;
    			}// end setter
    			public int getID(){ // getter for ID
    				return ID;
    			} // end getter
     
     
    			public void setStock (int Stock){ // setter for stock
    				this.Stock = Stock;
    			}// end setter
    			public int getStock(){ // getter for stock
    				return Stock;
    			}// end getter
     
     
    			public void setPrice (double Price){ // setter for stock
    				this.Price = Price;
    			}// end setter
    			public double getPrice(){ // getter for stock
    				return Price;
    			}// end getter
     
     
    			public double getValStock(){ // getter for stock
    				return (Price * .05) * Stock + (Stock * Price);
    			}// end getter
     
     
    			/// this WAS the sort method used for my program before it needed a GUI
    			public int compareTo(Inventory other) {
    				if (getName().compareTo(other.getName()) > 0){
    					return 1;
    				}else if (getName().compareTo(other.getName()) < 0){
    					return -1;
    				}else
    					return 0;
    			}
     
     
     
     
     
    	}// end class


    InvMore Class
     
    package inventory;
     
    public class InvMore extends Inventory {
    	public Object testValue;
    	public InvMore(String Name, String Genre, int ID, int Stock, double Price) {
    		super(Name, Genre, ID, Stock, Price);
    		this.Genre = Genre;
    		this.ReStock = ReStock;	
    		this.cdVal = cdVal;
     
    	}
     
    	public void setGenre (String Genre){ // setter for name
    		this.Genre = Genre;
    	} // end setter
    	public String getGenre(){ // getter for name
    		return Genre;
    	} // end getter
     
    	public double getReStock(){ // getter for stock
    		return (Price * .05);
    	}// end getter
     
     
    	// getters and setters for cdVal
    	public void setcdVal(){
    		this.cdVal = cdVal;
    	}
    	public double getcdVal(){
     
    		double cdVal = getPrice() * getStock();
     
    		return(cdVal);
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: having some issues displaying objects in my GUI

    Just as it says, the source of the error is this line in method CDs.main():

    new CDs( CD ).setVisible( true ); // this should call the GUI

    And the error is caused by the parameter CD which is of type Inventory[]. There is no CDs() constructor that accepts that as a parameter. You could remove the 'CD' from the call to the CDs() constructor, or you could change the existing CDs() constructor to accept that type as a parameter.

    Think about the best option to use while I look at the rest of the code.

    Edit: You're also missing a CDs.get() method which is called in the updateGUI() method. I'm also curious why you'd be calling that method statically, even if it did exist.

    Edit2: I commented out all calls to updateGUI() and the updateGUI() method entirely and got the GUI to display. You could try that to feel the triumph of seeing your code do something and then go back and fix the code that was commented out.

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

    travis07 (December 8th, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: having some issues displaying objects in my GUI

    Okay so I took the CD out of the CDs() in my main method.

    I then changed the part of calling CDs.Get() statically. (at least I believe so.)

    I did just as you said and commented out the updateGUI calls and got the GUI to display.


    EDIT: I guess from here I am lost on creating the function method for the get(currentIndex), as in what is it suppose to be doing for me?
    Not to mention. Am I creating the method correctly?

     
    package inventory;
     
    import java.util.*;
     
     
     
     
    public class CDs extends JFrame{
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L; // serial version
     
     
    // declares var for currentIndex for button functionality
    	public int currentIndex = 0;
     
    	// creates cars for the GUI build
    	private JLabel labelName;
    	private JLabel labelID;
    	private JLabel labelStock;
    	private JLabel labelGenre;
    	private JLabel labelPrice;
    	private JLabel labelReStock;
    	private JLabel labelFval;
    	private JLabel labelTval;
     
    	public CDs(){// sets the GUI 
     
     
    		//creates the panel and title fields
     
    		JPanel panel = new JPanel();
    		panel.setLayout(null);
     
    		/// fields numbered 1-8 for easy matching with other functions and adding
    		labelName = new JLabel ("CD Name: ");//1
    		labelName.setFont(new Font("Serif", Font.BOLD, 14));
    		labelName.setBounds(0, 0, 200, 50);
     
     
    		labelID = new JLabel ("CD ID: ");//2
    		labelID.setFont(new Font("Serif", Font.BOLD, 14));
    		labelID.setBounds(0, 0, 200, 100);
     
    		labelGenre = new JLabel ("CD genre: ");//3
    		labelGenre.setFont(new Font("Serif", Font.BOLD, 14));
    		labelGenre.setBounds(0, 0, 200, 150);
     
    		labelStock = new JLabel ("Stock amount: ");//4
    		labelStock.setFont(new Font("Serif", Font.BOLD, 14));
    		labelStock.setBounds(0, 0, 200, 200);
     
    		labelPrice = new JLabel ("CD Price: ");//5
    		labelPrice.setFont(new Font("Serif", Font.BOLD, 14));
    		labelPrice.setBounds(0, 0, 200, 250);
     
    		labelReStock = new JLabel ("ReStock value: ");//6
    		labelReStock.setFont(new Font("Serif", Font.BOLD, 14));
    		labelReStock.setBounds(0, 0, 200, 300);
     
    		labelFval = new JLabel ("CD stock value: ");//7
    		labelFval.setFont(new Font("Serif", Font.BOLD, 14));
    		labelFval.setBounds(0, 0, 200, 350);
     
    		labelTval = new JLabel ("Total inventory value: ");//8
    		labelTval.setFont(new Font("Serif", Font.BOLD, 14));
    		labelTval.setBounds(125, 0, 200, 500);
     
    		// adds the fields
    		panel.add(labelName);//1
    		panel.add(labelID);//2
    		panel.add(labelGenre);//3
    		panel.add(labelStock);//4
    		panel.add(labelPrice);//5
    		panel.add(labelReStock);//6
    		panel.add(labelFval);//7
    		panel.add(labelTval);//8
     
    		getContentPane().add(panel);
     
    		/// creates the buttons
    		JButton previous = new JButton("Previous");
    		previous.setBounds(110, 300, 85, 20);
    		panel.add(previous);
     
    		JButton next = new JButton("Next");
    		next.setBounds(230, 300, 85, 20);
    		panel.add(next);
     
    		// sets the values for the window
    		setTitle("Your Inventory Program");
    		setSize(500,550);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
     
     
    		///// these are the active listeners for the button functions
    			previous.addActionListener(new ActionListener() {
    				@Override
    				public void actionPerformed(ActionEvent e) {
    					showPrevious();
    				}
    			});
    		next.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				showNext();
    			}
    		});
     
    		updateGUI(); /// calls the GUI display for object results
     
    	}// sets GUI
     
     
     
    	public static void main(String[] args){ // start main
     
     
    		Inventory[] CD = new Inventory[5]; // creates the variable CD for the array
    		/// CD inventory objects
    			CD[0] = new InvMore("Red Hot Chili Peppers", "Alternative", 1188, 25, 16.99);
    			CD[1] = new InvMore("Nirvana", "Classic Rock", 1177, 35, 16.99);
    			CD[2] = new InvMore("Styx", "Classic Rock", 1166, 31, 16.99);
    			CD[3] = new InvMore("Jay Z", "Hip Hop/Rap", 1155, 45, 16.99);
    			CD[4] = new InvMore("Nelly", "Hip Hop/Rap", 1144, 35, 16.99);
     
    		new CDs().setVisible(true); // this should call the GUI
     
    	}// end main
     
     
    	// calls and prints the objects by specifics
    	public void updateGUI() {
    		InvMore item = get(currentIndex);
    	labelName.setText(String.valueOf(item.getName()));//1
    	labelID.setText(String.valueOf(item.getID()));//2
    	labelGenre.setText(String.valueOf(item.getGenre()));//3
    	labelStock.setText(String.valueOf(item.getStock()));//4
    	labelPrice.setText(String.format("$%.2f",item.getPrice()));//5
    	labelReStock.setText(String.format("$%.2f",item.getReStock()));//6
    	labelFval.setText(String.format("$%.2f",item.getcdVal()));//7
    	labelTval.setText(String.format("$%.2f",item.getcdVal()));//8
     
    	}// ends updateGUI
     
     
     
     
     
    	private InvMore get(int currentIndex) {// method for get(currentIndex
     
    		return null;
    	}
     
     
     
    	private void showNext() {  // method for the next button
    		if (currentIndex < 4 - 1) {
    			currentIndex++;
    			updateGUI();
    		}
    	}
     
    	private void showPrevious() { // method for the previous button
    		if (currentIndex > 0) {
    			currentIndex--;
    			updateGUI();
    		}
    	}
    }// end class

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: having some issues displaying objects in my GUI

    I dunno. I was going to ask you the same thing. What is the get() method supposed to do?

    I'm guessing that the updateGUI() method fills the GUI with the details of the CD[] element at the index currentIndex. There are only 5 CD elements, so instead of this line:

    InvMore item = CDs.get( currentIndex );

    I think it should be:

    InvMore item = CD[currentIndex];

    But that would require the CD[] array to be declared as an instance variable and initialized in the constructor instead of the main() method AND as type InvMore rather than Inventory. After making those changes, uncomment the code you commented out to get the GUI to display, compile, and voila!

    If you can follow all of that and do it correctly, then your GUI should display AND function. If you can't follow the directions or you're just unsure why I've suggested the changes I have, then ask about what you don't understand or can't accomplish.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    travis07 (December 8th, 2013)

  7. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: having some issues displaying objects in my GUI

    Awesome. Thank you so much! I will go work on that, I will come back and touch bases with you.

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: having some issues displaying objects in my GUI

    You're welcome.

    Even though you say you're writing your GUI code by hand and I don't doubt you, it looks like you've studied code generated by a graphical programming tool like Netbeans' GUI Builder or Eclipse's Window Builder as a guide for your own code. Rather than using null layouts and locating each component by coordinates, you should learn how to use layout managers. The Oracle Tutorials are pretty good at presenting the layout managers and providing example code, but it's a lot to work through and will take some time. I recommend starting there.

    You should also be aware that Swing applications should be started on the Event Dispatch Thread (EDT) by the main() method. I won't go into the details now, but look for that in the Oracle Tutorials.

    Keep coding, and good luck!

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    travis07 (December 8th, 2013)

  10. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: having some issues displaying objects in my GUI

    Okay so I just got done doing some research and trying to figure out how to declare my object array as an instance variable.
    Is it like the way I created my instances for the getters and setters on my inventory class?
    At this point I may have over thought this process and made it much more difficult.. But I am just lost as to how I need to declare the object arrays as an instance variable.
    Is there any type of example you can lead me to by chance?

  11. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: having some issues displaying objects in my GUI

    Didn't mean you to struggle with it, but it's good you did some research. What it would look like is:
        // . . . excerpted from class CDs
        private JLabel labelTval;
     
        // this declares CD as an array of type InvMore
        private InvMore[] CD;
     
        public CDs() // sets the GUI
        {
            CD = new InvMore[5]; // creates the variable CD for the array
            /// CD inventory objects (moved from the main() method)
            CD[0] = new InvMore( "Red Hot Chili Peppers", "Alternative", 1188, 25, 16.99 );
            CD[1] = new InvMore( "Nirvana", "Classic Rock", 1177, 35, 16.99 );
            // . . . . etc.

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    travis07 (December 8th, 2013)

  13. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: having some issues displaying objects in my GUI

    dang. Your right, it works like a charm now. I have it displaying and I can now confidently say, MY BUTTONS DO WORK! I was worried on the side of if my buttons would function with the active listeners and methods i made.

    Of course I have a lot of little things to change.

    The list will show the total CD stock value and the display even lower than that will (should be) displaying the entire stock value.

    These are all things I should be able to accomplish from here on out.


    Thanks again for all of your help. I hope if I run into any big issues later down the road you will be around for some assistance

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    You're welcome.

    Even though you say you're writing your GUI code by hand and I don't doubt you, it looks like you've studied code generated by a graphical programming tool like Netbeans' GUI Builder or Eclipse's Window Builder as a guide for your own code. Rather than using null layouts and locating each component by coordinates, you should learn how to use layout managers. The Oracle Tutorials are pretty good at presenting the layout managers and providing example code, but it's a lot to work through and will take some time. I recommend starting there.

    You should also be aware that Swing applications should be started on the Event Dispatch Thread (EDT) by the main() method. I won't go into the details now, but look for that in the Oracle Tutorials.

    Keep coding, and good luck!
    you are probably right.. I will look into that because I was just following some examples to figure out how to manipulate my GUI. My goal is to be able to code a good GUI without using something like WindowsBuilder in eclipse..

    Thanks for the good tip, I am going to the Oracle site now

Similar Threads

  1. Creating a Library, abstract class created issues. Also need GUI help.
    By rplee18 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 10th, 2013, 03:36 PM
  2. Displaying database results on a GUI in a JTextArea with button click
    By Norm in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 27th, 2013, 09:05 PM
  3. Displaying database results on a GUI in a JTextArea with button click
    By tomb1992 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2013, 08:20 PM
  4. Help displaying a new string to GUI
    By DaAznSmurf in forum What's Wrong With My Code?
    Replies: 18
    Last Post: February 1st, 2012, 07:42 AM
  5. Displaying things in gui
    By KrisTheSavage in forum AWT / Java Swing
    Replies: 2
    Last Post: March 29th, 2010, 12:21 PM