participate


Swing - create a simple GUI to display array objects
<<   Back to Forum  |   Give us Feedback
9 Duke Stars available
This topic has 6 replies on 1 page.
sammyboy78
Posts:6
Registered: 6/29/07
create a simple GUI to display array objects   
Jun 29, 2007 5:00 PM

 
I need to add a GUI to my current program that displays data from an array in a GUI frame. Nothing fancy, just a frame with all the data displayed. The program in its current state displays the information in console window.

Can anyone give me a hint on where to begin? I haven't used swing or created a GUI before this and I am completely new to java. I'm not sure howe to get the GUI to read the information from my array of objects. The objects contain strings and double values.

I have a CD class and a a subclass that represents a CD object with it's methods for calculation and output. Then I have an Inventory class that populates the array with objects and executes the methods of the CD/CD2 class. I have renamed my CDInventory class and attempted to create a GUI using JTextArea but it won't let me reference my static methods in the CD class. When I try to compile GUICDInventory it gives me this error:

C:\Documents and Settings\Sam\CDInventory2.java:30: 'void' type not allowed here
textArea.append(CD.displayTotalInventory( completeCDInventory ));
^
C:\Documents and Settings\Sam\CDInventory2.java:33: 'void' type not allowed here
textArea.append(CD.displayTotalInventory( completeCDInventory ));
^
2 errors


Here's my code (GUICDInventory is a modified CDInventory):

// GUICDInventory.java
// uses CD class
import java.awt.*;
import javax.swing.*;
 
public class GUICDInventory extends JFrame
{
 
 	public static void main( String args[] )
 	{
		new GUICDInventory();
 
	}// end main
 
 	public GUICDInventory()
 	{
		CD completeCDInventory[] = new CD2[ 5 ]; // creates a new 5 element array
 
    	// populates array with objects that implement CD
    	completeCDInventory[ 0 ] = new CD2( "Sixpence None the Richer" , "D121401" , 12 , 11.99, 1990 );
    	completeCDInventory[ 1 ] = new CD2( "Clear" , "D126413" , 10 , 10.99, 1998 );
    	completeCDInventory[ 2 ] = new CD2( "NewsBoys: Love Liberty Disco" , "2438-51720-2" , 10 , 12.99, 1999 );
    	completeCDInventory[ 3 ] = new CD2( "Skillet: Hey You, I Love Your Soul" , "D122966" , 9 , 9.99, 1998 );
    	completeCDInventory[ 4 ] = new CD2( "Michael Sweet: Real" , "020831-1376-204" , 15 , 12.99, 1995 );
 
   		double totalInventoryValue = CD.calculateTotalInventory( completeCDInventory ); //declares totalInventoryValue variable
 
    	JTextArea textArea = new JTextArea();
 
    	textArea.append(CD.displayTotalInventory( completeCDInventory ));
    	textArea.append("\nTotal Inventory Value is: " + totalInventoryValue+ "\n");
 
    	textArea.append(CD.displayTotalInventory( completeCDInventory ));
    	textArea.append("\nTotal Inventory Value is: "+totalInventoryValue +"\n");
 
    	JFrame frame = new JFrame();
 
    	frame.getContentPane().add(new JScrollPane(textArea));
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.pack();
    	frame.setLocationRelativeTo(null);
    	frame.setVisible(true);
 
	}//end main
 
} // end class GUICDInventory
 
 
[\code]
 
[code]// CDInventory.java
// uses CD class
 
public class CDInventory
{
 
    // executes application
    public static void main( String args[])
    {
 
        CD completeCDInventory[] = new CD2[ 5 ]; // creates a new 5 element array
 
        // populates array with objects that implement CD
        completeCDInventory[ 0 ] = new CD2( "Sixpence None the Richer" , "D121401" , 12 , 11.99, 1990 );
        completeCDInventory[ 1 ] = new CD2( "Clear" , "D126413" , 10 , 10.99, 1998 );
        completeCDInventory[ 2 ] = new CD2( "NewsBoys: Love Liberty Disco" , "2438-51720-2" , 10 , 12.99, 1999 );
        completeCDInventory[ 3 ] = new CD2( "Skillet: Hey You, I Love Your Soul" , "D122966" , 9 , 9.99, 1998 );
        completeCDInventory[ 4 ] = new CD2( "Michael Sweet: Real" , "020831-1376-204" , 15 , 12.99, 1995 );
 
        double totalInventoryValue = CD.calculateTotalInventory( completeCDInventory ); //declares totalInventoryValue variable
 
        CD.displayTotalInventory( completeCDInventory ); //calls CD's display method
 
        System.out.println("Total Inventory Value is: " + totalInventoryValue);
 
        CD.sortedCDInventory( completeCDInventory ); // calls CD's sortedCDInventory method
 
		System.out.println("Total Inventory Value is: " + totalInventoryValue);
 
    } // end main
 
} // end class CDInventory


 // CD2.java
// subclass of CD
 
public class CD2 extends CD
{
	protected int copyrightDate; // CDs copyright date variable declaration
	private double price2;
 
	// constructor
	public CD2( String title, String prodNumber, double numStock, double price, int copyrightDate )
	{
		// explicit call to superclass CD constructor
		super( title, prodNumber, numStock, price );
 
		this.copyrightDate = copyrightDate;
 
	}// end constructor
 
	public double getInventoryValue() // modified subclass method to add restocking fee
    {
        price2 = price + price * 0.05;
        return numStock * price2;
 
    } //end getInventoryValue
 
    public void displayInventory() // modified subclass display method
	    {
 
	        System.out.printf( "\n%-22s%s\n%-22s%d\n%-22s%s\n%-22s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n  \n" , 
	        	"CD Title:", title, "Copyright Date:", copyrightDate, "Product Number:", prodNumber , "Number in Stock:", 
	        	numStock , "CD Price:" , "$" , price , "Restocking fee (5%):", "$", price*0.05, "Inventory Value:" , "$" , 
	        	getInventoryValue() );
 
	    } // end method
 
 
}//end class CD2
 
 


// CD.java
// Represents a compact disc object
import java.util.Arrays;
 
class CD implements Comparable
{
    protected String title; // CD title (name of product)
    protected String prodNumber; // CD product number
    protected double numStock; // CD stock number
    protected double price; // price of CD
    protected double inventoryValue; //number of units in stock times price of each unit
 
 
    // constructor initializes CD information
    public CD( String title, String prodNumber, double numStock, double price )
    {
        this.title = title; // Artist: album name
        this.prodNumber = prodNumber; //product number
        this.numStock = numStock; // number of CDs in stock
        this.price = price; //price per CD
 
    } // end constructor
 
    public double getInventoryValue()
    {
        return numStock * price;
 
    } //end getInventoryValue
 
    public void displayInventory()
    {
 
        System.out.printf( "\n%s%35s\n%s%12s\n%s%9.2f\n%s%12s%.2f\n%s%.2f\n%s%5s%.2f\n  \n" , 
        	"CD Title:", title, "Product Number:", prodNumber , "Number in Stock:", numStock , 
        	"CD Price:" , "$" , price , "Restocking fee (5%):", price*0.05, "Inventory Value:" , 
        	"$" , getInventoryValue() );
 
    } // end method
 
    public static double calculateTotalInventory( CD completeCDInventory[] )
    {
        double totalInventoryValue = 0;
 
        for ( int count = 0; count < completeCDInventory.length; count++ )
        {
             totalInventoryValue += completeCDInventory[count].getInventoryValue();
 
        } // end for
 
        return totalInventoryValue;
 
    } // end calculateTotalInventory
 
 
    public static void displayTotalInventory( CD completeCDInventory[] )
    {
        System.out.printf( "\n%s\n" ,"Inventory of CDs (unsorted):" );
 
        for ( int count = 0; count < completeCDInventory.length; count++ )
        {
            System.out.printf( "%s%d", "Item# ", count + 1 );
 
            completeCDInventory[count].displayInventory();
 
        }// end for
 
    }// end displayTotalInventory
 
	public int compareTo( Object obj ) //overlaod compareTo method
	{
		CD tmp = ( CD )obj;
 
		if( this.title.compareTo( tmp.title ) < 0 )
		{
			return -1; //instance lt received
		}
		else if( this.title.compareTo( tmp.title ) > 0 )
		{
			return 1; //instance gt received
		}
 
		return 0; //instance == received
 
		}// end compareTo method
 
    public static void sortedCDInventory( CD completeCDInventory[] )
    {
		   System.out.printf( "\n%s\n" ,"Inventory of CDs (sorted by title):" );
 
		   Arrays.sort( completeCDInventory ); // sort array
 
		   for( int count = 0; count < completeCDInventory.length; count++ )
		   {
				System.out.printf( "%s%d", "Item# ", count + 1 );
 
               	completeCDInventory[count].displayInventory();}
		   }
 
 
} // end class CD
 


Message was edited by:
sammyboy78

Message was edited by:
sammyboy78

Message was edited by:
sammyboy78

Message was edited by:
sammyboy78

Message was edited by:
sammyboy78

Message was edited by:
sammyboy78
 
Michael_Dunn
Posts:7,727
Registered: 17/08/03
Re: create a simple GUI to display array objects      
Jun 29, 2007 6:48 PM (reply 1 of 6)  (In reply to original post )

 
classmates?

http://forum.java.sun.com/thread.jspa?threadID=5187734&tstart=0
 
sammyboy78
Posts:6
Registered: 6/29/07
Re: create a simple GUI to display array objects   
Jun 29, 2007 7:04 PM (reply 2 of 6)  (In reply to #1 )

 
Looks like an assignment I'll have in 2 weeks. He probably goes to the same school I do. There's thousands of us! Muah ah ah! I actually found a posting here that helped me somewhat but due to the different style of programming by each student it can only help me to a certain point. Now I'm stuck again and sinking fast through the unrelenting quicksand of time.
 
sammyboy78
Posts:6
Registered: 6/29/07
Re: create a simple GUI to display array objects   
Jun 30, 2007 12:08 PM (reply 3 of 6)  (In reply to #2 )

 
Anyone?
 
Michael_Dunn
Posts:7,727
Registered: 17/08/03
Re: create a simple GUI to display array objects   
Jun 30, 2007 12:18 PM (reply 4 of 6)  (In reply to #3 )

 
the earlier link would (should) have taken you to this link
http://forum.java.sun.com/thread.jspa?threadID=5185444

which is identical to your question.

all you need to do is modify the code in the reply to suit your program.
 
sammyboy78
Posts:6
Registered: 6/29/07
Re: create a simple GUI to display array objects   
Jun 30, 2007 1:24 PM (reply 5 of 6)  (In reply to #4 )

 
Yeah that is exactly where I got the code to putthe JTextArea in my Inventory class but like I said I got stuck there.
But no matter now, I got it figured out with someone else's help. I had to modify my methods in CD and CD2 class to return Strings and had to make them non-static.
 
Michael_Dunn
Posts:7,727
Registered: 17/08/03
Re: create a simple GUI to display array objects   
Jun 30, 2007 3:35 PM (reply 6 of 6)  (In reply to #5 )

 
give the textArea a size (not significant, but will cause a 'visual' problem later)
//JTextArea textArea = new JTextArea();
JTextArea textArea = new JTextArea(10,30);

textArea.append(CD.displayTotalInventory( completeCDInventory ));

the method you are trying to append does not return anything, the return value
is void (because the method prints to the console). You need the method to
prepare 'what is to be printed' and return that, so it can be added to the textArea.
(you could redirect System.out to the textArea, but not wise to do so at the moment)
    //public static void displayTotalInventory( CD completeCDInventory[] )
    public static String displayTotalInventory( CD completeCDInventory[] )//<-----
    {
        //System.out.printf( "\n%s\n" ,"Inventory of CDs (unsorted):" );
        String textAreaText = "\nInventory of CDs (unsorted):\n";
 
        for ( int count = 0; count < completeCDInventory.length; count++ )
        {
            //System.out.printf( "%s%d", "Item# ", count + 1 );
            textAreaText += "Item# "+ (count + 1)+"\n";
            //completeCDInventory[count].displayInventory();
            textAreaText += completeCDInventory[count].displayInventory()+"\n\n";
        }// end for
        return textAreaText;//<----------
    }// end displayTotalInventory

as this method calls CD's displayInventory(), you have the same problem
    //public void displayInventory()
    public String displayInventory()
    {
        //System.out.printf( "\n%s%35s\n%s%12s\n%s%9.2f\n%s%12s%.2f\n%s%.2f\n%s%5s%.2f\n  \n" ,
          //"CD Title:", title, "Product Number:", prodNumber , "Number in Stock:", numStock ,
          //"CD Price:" , "$" , price , "Restocking fee (5%):", price*0.05, "Inventory Value:" ,
          //"$" , getInventoryValue() );
 
        return "CD Title:"+ title+"\nProduct Number:"+ prodNumber+"\nNumber in Stock:"+ numStock+
        "\nCD Price: $"+ price+"\nRestocking fee (5%):"+ (price*0.05)+"\nInventory Value:"+getInventoryValue();
 
    } // end method

CD2's displayInventory() will need to be modified also. Comment it out until
you get some display working

last thing - when you display things like (price*.05) you will get something like
12.45678913456
you may want to consider using DecimalFormat("0.00") to display it as 12.46
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 25
  • Guests : 132

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums