473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

creating a GUI to display a previously created array of objects

sammyboy78
21 New Member
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the examples I've seen are creating the information that will be displayed from scratch, while I have to use my previously created classes and add a GUI to it. I'm trying to do this GUI using JLabels but it won't let me refer to my CD class methods that display the information. I keep getting this error when I try to compile my CDinventory2.java file :

C:\Documents and Settings\Sam\CDInventory2.java:33: 'void' type not allowed here
JLabel cDObjects1 = new JLabel(CD.displayTotalInventory( completeCDInventory ), JLabel.LEFT);
^
C:\Documents and Settings\Sam\CDInventory2.java:39: 'void' type not allowed here
JLabel cDObjects2 = new JLabel( CD.sortedCDInventory( completeCDInventory ), JLabel.LEFT);
^
2 errors



Heres the code:

Expand|Select|Wrap|Line Numbers
  1. // CDInventory2.java
  2. // uses CD class
  3.  
  4. import java.awt.*;
  5. import javax.swing.*;
  6.  
  7. public class CDInventory2 extends JFrame
  8. {
  9.  
  10.     public static void main( String args[] )
  11.     {
  12.     new CDInventory2();
  13.  
  14.     }//end main
  15.  
  16.     public CDInventory2()//constructor
  17.     {
  18.     super( "Compact Disc Inventory" );
  19.  
  20.     CD completeCDInventory[] = new CD2[ 5 ]; // creates a new 5 element array
  21.  
  22.     // populates array with objects that implement CD
  23.     completeCDInventory[ 0 ] = new CD2( "Sixpence None the Richer" , "D121401" , 12 , 11.99, 1990 );
  24.     completeCDInventory[ 1 ] = new CD2( "Clear" , "D126413" , 10 , 10.99, 1998 );
  25.     completeCDInventory[ 2 ] = new CD2( "NewsBoys: Love Liberty Disco" , "2438-51720-2" , 10 , 12.99, 1999 );
  26.     completeCDInventory[ 3 ] = new CD2( "Skillet: Hey You, I Love Your Soul" , "D122966" , 9 , 9.99, 1998 );
  27.     completeCDInventory[ 4 ] = new CD2( "Michael Sweet: Real" , "020831-1376-204" , 15 , 12.99, 1995 );
  28.  
  29.     double totalInventoryValue = CD.calculateTotalInventory( completeCDInventory ); //declares totalInventoryValue variable
  30.  
  31.     Container content = getContentPane();
  32.  
  33.     JLabel cDObjects1 = new JLabel(CD.displayTotalInventory( completeCDInventory ), JLabel.LEFT);
  34.     content.add(cDObjects1);
  35.  
  36.     JLabel invVal1 = new JLabel("Total Inventory value is:" + totalInventoryValue , JLabel.LEFT);
  37.     content.add(invVal1);
  38.  
  39.     JLabel cDObjects2 = new JLabel( CD.sortedCDInventory( completeCDInventory ), JLabel.LEFT);
  40.     content.add(cDObjects2);
  41.  
  42.     JLabel invVal2 = new JLabel("Total Inventory value is:" + totalInventoryValue , JLabel.LEFT);
  43.     content.add(invVal2);
  44.  
  45.     pack();
  46.     setVisible( true );
  47.  
  48.     }//end constructor
  49.  
  50. } //end class
Expand|Select|Wrap|Line Numbers
  1. // CD2.java
  2. // subclass of CD
  3.  
  4. public class CD2 extends CD
  5. {
  6.     protected int copyrightDate; // CDs copyright date variable declaration
  7.     private double price2;
  8.  
  9.     // constructor
  10.     public CD2( String title, String prodNumber, double numStock, double price, int copyrightDate )
  11.     {
  12.         // explicit call to superclass CD constructor
  13.         super( title, prodNumber, numStock, price );
  14.  
  15.         this.copyrightDate = copyrightDate;
  16.  
  17.     }// end constructor
  18.  
  19.     public double getInventoryValue() // modified subclass method to add restocking fee
  20.     {
  21.         price2 = price + price * 0.05;
  22.         return numStock * price2;
  23.  
  24.     } //end getInventoryValue
  25.  
  26.     public void displayInventory() // modified subclass display method
  27.         {
  28.  
  29.             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() );
  30.  
  31.         } // end method
  32.  
  33.  
  34. }//end class CD2
Expand|Select|Wrap|Line Numbers
  1. // CD.java
  2. // Represents a compact disc object
  3. import java.util.Arrays;
  4.  
  5. class CD implements Comparable
  6. {
  7.     protected String title; // CD title (name of product)
  8.     protected String prodNumber; // CD product number
  9.     protected double numStock; // CD stock number
  10.     protected double price; // price of CD
  11.     protected double inventoryValue; //number of units in stock times price of each unit
  12.  
  13.  
  14.     // constructor initializes CD information
  15.     public CD( String title, String prodNumber, double numStock, double price )
  16.     {
  17.         this.title = title; // Artist: album name
  18.         this.prodNumber = prodNumber; //product number
  19.         this.numStock = numStock; // number of CDs in stock
  20.         this.price = price; //price per CD
  21.  
  22.     } // end constructor
  23.  
  24.     public double getInventoryValue()
  25.     {
  26.         return numStock * price;
  27.  
  28.     } //end getInventoryValue
  29.  
  30.     public void displayInventory()
  31.     {
  32.  
  33.         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() );
  34.  
  35.     } // end method
  36.  
  37.     public static double calculateTotalInventory( CD completeCDInventory[] )
  38.     {
  39.         double totalInventoryValue = 0;
  40.  
  41.         for ( int count = 0; count < completeCDInventory.length; count++ )
  42.         {
  43.              totalInventoryValue += completeCDInventory[count].getInventoryValue();
  44.  
  45.         } // end for
  46.  
  47.         return totalInventoryValue;
  48.  
  49.     } // end calculateTotalInventory
  50.  
  51.  
  52.     public static void displayTotalInventory( CD completeCDInventory[] )
  53.     {
  54.         System.out.printf( "\n%s\n" ,"Inventory of CDs (unsorted):" );
  55.  
  56.         for ( int count = 0; count < completeCDInventory.length; count++ )
  57.         {
  58.             System.out.printf( "%s%d", "Item# ", count + 1 );
  59.  
  60.             completeCDInventory[count].displayInventory();
  61.  
  62.         }// end for
  63.  
  64.     }// end displayTotalInventory
  65.  
  66.     public int compareTo( Object obj ) //overlaod compareTo method
  67.     {
  68.         CD tmp = ( CD )obj;
  69.  
  70.         if( this.title.compareTo( tmp.title ) < 0 )
  71.         {
  72.             return -1; //instance lt received
  73.         }
  74.         else if( this.title.compareTo( tmp.title ) > 0 )
  75.         {
  76.             return 1; //instance gt received
  77.         }
  78.  
  79.         return 0; //instance == received
  80.  
  81.         }// end compareTo method
  82.  
  83.     public static void sortedCDInventory( CD completeCDInventory[] )
  84.     {
  85.            System.out.printf( "\n%s\n" ,"Inventory of CDs (sorted by title):" );
  86.  
  87.            Arrays.sort( completeCDInventory ); // sort array
  88.  
  89.            for( int count = 0; count < completeCDInventory.length; count++ )
  90.            {
  91.                 System.out.printf( "%s%d", "Item# ", count + 1 );
  92.  
  93.                    completeCDInventory[count].displayInventory();}
  94.            }
  95.  
  96.  
  97. } // end class CD
Jun 29 '07 #1
6 3861
JosAH
11,448 Recognized Expert MVP
Did you ever take a peek at the JTable component?

kind regards,

Jos
Jun 29 '07 #2
sammyboy78
21 New Member
Did you ever take a peek at the JTable component?

kind regards,

Jos
I don't think that'll work for me, the information isn't really set up to be displayed in a table. I have an example of what I need here using DVDs (I only need one of the two displays, preferrably the easiest!):

Jun 29 '07 #3
sammyboy78
21 New Member
Is that top one possibly using JLabels and the bottom example using a JTextArea?
Jun 29 '07 #4
JosAH
11,448 Recognized Expert MVP
Is that top one possibly using JLabels and the bottom example using a JTextArea?
Yep, you can even put html text in JLabels; something like this:

Expand|Select|Wrap|Line Numbers
  1. String text= "<html><center>Hello<br>world></center></html>";
  2. JLabel lab= new JLabel(text);
  3. ...
  4.  
All it takes is a bit of String fiddling to 'html-ize' your text. Adding text to a
JTextArea is even easier, i.e. add text to the area using the append() method.

kind regards,

Jos

ps. Your table rotated 90 degrees with the column headers on top make it very
well suited for a JTable display.
Jun 29 '07 #5
sammyboy78
21 New Member
so how would I get my CDobjects to display on a JLabel?
Jun 29 '07 #6
JosAH
11,448 Recognized Expert MVP
so how would I get my CDobjects to display on a JLabel?
Simply concatenate the text representations of all the CDs and htlm-ize the
result a bit; something like this:

Expand|Select|Wrap|Line Numbers
  1. String html= "<html><center>";
  2.  
  3. for (CD cd : CDCollection)
  4.    html+= cd+"<br>";
  5.  
  6. html+="</center></html>";
  7.  
  8. // html is the text for your label
  9.  
kind regards,

Jos
Jun 30 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: laurence chang | last post by:
I remembered there is an option I can use to display just user defined objects in SQL Server Enterprise Manager, but I can not find it anymore? Would you like to tell me? I really appreciate it. ...
3
by: WindAndWaves | last post by:
Hi Friends I am thinking about making an HTML library of all my objects in my database. Has anyone done this before???? Keen to get your ideas. Here is the structure of my library: Table...
2
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
16
by: rguti | last post by:
Hi, How do I create a two dimensional array? I have created a one dimensional doing this: Dim laFields As ArrayList = New ArrayList How about to do a 2 dimensional?
16
by: gucci09 | last post by:
I was wondering how you would go about creating a dynamically allocated array of strings from a file. i have a dictionary that i want to load in my program in order to spell check. i cant decide if...
1
by: lilsugaman | last post by:
Hi I have created an Inventory program that displays the product name, item number, how many units, and it's price and at the end displays the totals, I have to modify the program so that it will...
3
by: shubham rastogi | last post by:
hello guys I want to copy or insert records into the previously created table from another table.. For example I have two tables A and B .... I want to copy or insert records from table B into...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.