473,704 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help displaying array of objects using a GUI

sammyboy78
21 New Member
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not even sure if the way I've written this is going to work but anyway, I keep getting a compilation error that says:

C:\Documents and Settings\Sam\GU ICDInventory.ja va:22: cannot find symbol
symbol : constructor JList(CDInvento ry)
location: class javax.swing.JLi st
cDJList = new JList( gUICDInventory );


Here's the code where I get the error:

Expand|Select|Wrap|Line Numbers
  1. // GUICDInventory.java
  2. // uses CD class
  3. import java.awt.FlowLayout;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.SwingConstants;
  7. import javax.swing.JList;
  8. import javax.swing.JScrollPane;
  9.  
  10. public class GUICDInventory extends JFrame
  11. {
  12.  
  13.     CDInventory gUICDInventory = new CDInventory();
  14.  
  15.     private JList cDJList;
  16.  
  17.     public GUICDInventory()
  18.     {
  19.         super( "Compact Disc Inventory" );
  20.         setLayout( new FlowLayout() );
  21.  
  22.         cDJList = new JList( gUICDInventory );
  23.         cDJList.setVisibleRowCount( 25 );
  24.  
  25.         add( new JScrollPane( cDJList ) );
  26.  
  27.  
  28.  
  29.     }
  30.  
  31.     // executes application
  32.   public static void main( String args[] )
  33.     {
  34.         GUICDInventory guICDInventory = new GUICDInventory();
  35.         guICDInventory.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  36.         guICDInventory.setSize( 500, 500 );
  37.         guICDInventory.setVisible( true );
  38.  
  39.     } // end main
  40.  
  41. } // end class GUICDInventory
and the other classes:

Expand|Select|Wrap|Line Numbers
  1. // CDInventory.java
  2. // uses CD class
  3.  
  4. public class CDInventory
  5. {
  6.  
  7.     // executes application
  8.     public static void main( String args[] )
  9.     {
  10.  
  11.         CD completeCDInventory[] = new CD2[ 5 ]; // creates a new 5 element array
  12.  
  13.         // populates array with objects that implement CD
  14.         completeCDInventory[ 0 ] = new CD2( "Sixpence None the Richer" , "D121401" , 12 , 11.99, 1990 );
  15.         completeCDInventory[ 1 ] = new CD2( "Clear" , "D126413" , 10 , 10.99, 1998 );
  16.         completeCDInventory[ 2 ] = new CD2( "NewsBoys: Love Liberty Disco" , "2438-51720-2" , 10 , 12.99, 1999 );
  17.         completeCDInventory[ 3 ] = new CD2( "Skillet: Hey You, I Love Your Soul" , "D122966" , 9 , 9.99, 1998 );
  18.         completeCDInventory[ 4 ] = new CD2( "Michael Sweet: Real" , "020831-1376-204" , 15 , 12.99, 1995 );
  19.  
  20.         double totalInventoryValue = CD.calculateTotalInventory( completeCDInventory ); //declares totalInventoryValue variable
  21.  
  22.         CD.displayTotalInventory( completeCDInventory ); //calls CD's display method
  23.  
  24.         System.out.println("Total Inventory Value is: " + totalInventoryValue);
  25.  
  26.         CD.sortedCDInventory( completeCDInventory ); // calls CD's sortedCDInventory method
  27.  
  28.         System.out.println("Total Inventory Value is: " + totalInventoryValue);
  29.  
  30.     } // end main
  31.  
  32. } // end class CDInventory
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 28 '07 #1
3 4743
r035198x
13,262 MVP
Read the docs for JList and see where you're doing it wrong. Notice especially it's constructors.
Jun 28 '07 #2
sammyboy78
21 New Member
I read about the constructors and it looks like I need this one:

public JList(Object[] listData) Constructs a JList that displays the elements in the specified array. This constructor just delegates to the ListModel constructor.

Parameters:
listData - the array of Objects to be loaded into the data model

But, I'm not sure how to use it. If I do this:

cDJList = new JList( Object[] gUICDInventory );
cDJList.setVisi bleRowCount( 25 );


I get this error message:

C:\Documents and Settings\Sam\GU ICDInventory.ja va:22: '.class' expected
cDJList = new JList( Object[] gUICDInventory );
^
C:\Documents and Settings\Sam\GU ICDInventory.ja va:22: ';' expected
cDJList = new JList( Object[] gUICDInventory );
^
2 errors
Jun 28 '07 #3
r035198x
13,262 MVP
I read about the constructors and it looks like I need this one:

public JList(Object[] listData) Constructs a JList that displays the elements in the specified array. This constructor just delegates to the ListModel constructor.

Parameters:
listData - the array of Objects to be loaded into the data model

But, I'm not sure how to use it. If I do this:

cDJList = new JList( Object[] gUICDInventory );
cDJList.setVisi bleRowCount( 25 );


I get this error message:

C:\Documents and Settings\Sam\GU ICDInventory.ja va:22: '.class' expected
cDJList = new JList( Object[] gUICDInventory );
^
C:\Documents and Settings\Sam\GU ICDInventory.ja va:22: ';' expected
cDJList = new JList( Object[] gUICDInventory );
^
2 errors
Don't try to guess how to use it, read the swing tutorial.
Jun 29 '07 #4

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

Similar topics

2
3053
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
3
1942
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store objects of my classes Person(superclass), Student(inherit Person), Teacher(inherit Person) in that array. The name will be the unique key. These classes are all working ok. I want to be able to add, remove, find etc. objects. To all of this, I...
4
1618
by: Christiaan | last post by:
Hi, I have two classes, Department and Employer, the Department contains an ArrayList (employers) filled with Employer objects. I want to display an ArrayList filled with Department objects using a Datagrid. Therefor, I do a setDatabinding with the Deparments ArrayList and I create two DatagridTableStyle objects, both with mappingname "ArrayList", and add them to the Datagrid. However, if I want to add the second DatagridTableStyle, I get...
18
2332
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before. Here's the scenario: We have a base class with all virtual functions. We'll call this the Animal class. We then make two classes Fish and Bird that both inherit from Animal. In the program, we have a single array of Animal pointers that will...
2
2026
by: Severus Snape | last post by:
I can display and hide 1 object at a time, but haven't seen it done on multiple objects simultaneously. I have four (or more) tables on a page that start off hidden, and I want to toggle their visibility -- but have only one table visible at any time. If I click button #1, table #1 should toggle visibility and all other tables should remain hidden. If table #1 is visible and I click button #3, table #1 becomes hidden and table #3 becomes...
0
1137
by: Matthew87 | last post by:
Hi all I'm having a bit of trouble completeing my coursework basically so far i have created Citenary class with support methods for a private array. The problem is i can add objects to the array through a instance of the class in the main form but im having trouble retreving the data from a combobox. I have the values Name, Passport Number, Issuedby and Expires that are added to the array. The value name is passed to the combobox when a...
9
3942
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result - I have read every post out there and spent hours trying to figure out the problem with no success whatsoever - I have constrained the problem to one form however, and I think it's hiding somewhere in my code associated with this form, which is...
2
3153
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
4
1426
by: jeddiki | last post by:
Hi, I have written a script that is listing articles from my article table and displaying the titles ten on each page. As I need to calculate and print other stuff in between the extracting and the displaying of article titles, I use an array to catchj the data. The mysql query just grabs the first ten
0
8764
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8677
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9266
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6603
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4435
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2468
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2074
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.