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.ja va file : C:\Documents and Settings\Sam\CD Inventory2.java :33: 'void' type not allowed here
JLabel cDObjects1 = new JLabel(CD.displ ayTotalInventor y( completeCDInven tory ), JLabel.LEFT);
^
C:\Documents and Settings\Sam\CD Inventory2.java :39: 'void' type not allowed here
JLabel cDObjects2 = new JLabel( CD.sortedCDInve ntory( completeCDInven tory ), JLabel.LEFT);
^
2 errors
Heres the code: - // CDInventory2.java
-
// uses CD class
-
-
import java.awt.*;
-
import javax.swing.*;
-
-
public class CDInventory2 extends JFrame
-
{
-
-
public static void main( String args[] )
-
{
-
new CDInventory2();
-
-
}//end main
-
-
public CDInventory2()//constructor
-
{
-
super( "Compact Disc Inventory" );
-
-
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
-
-
Container content = getContentPane();
-
-
JLabel cDObjects1 = new JLabel(CD.displayTotalInventory( completeCDInventory ), JLabel.LEFT);
-
content.add(cDObjects1);
-
-
JLabel invVal1 = new JLabel("Total Inventory value is:" + totalInventoryValue , JLabel.LEFT);
-
content.add(invVal1);
-
-
JLabel cDObjects2 = new JLabel( CD.sortedCDInventory( completeCDInventory ), JLabel.LEFT);
-
content.add(cDObjects2);
-
-
JLabel invVal2 = new JLabel("Total Inventory value is:" + totalInventoryValue , JLabel.LEFT);
-
content.add(invVal2);
-
-
pack();
-
setVisible( true );
-
-
}//end constructor
-
-
} //end class
- // 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
6 3883 JosAH 11,448
Recognized Expert MVP
Did you ever take a peek at the JTable component?
kind regards,
Jos
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!): ![](/images/imgserve.php?url=http://www.geocities.com/sammyboy78/guidisplayexample.jpg)
Is that top one possibly using JLabels and the bottom example using a JTextArea?
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: -
String text= "<html><center>Hello<br>world></center></html>";
-
JLabel lab= new JLabel(text);
-
...
-
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.
so how would I get my CDobjects to display on a JLabel?
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: -
String html= "<html><center>";
-
-
for (CD cd : CDCollection)
-
html+= cd+"<br>";
-
-
html+="</center></html>";
-
-
// html is the text for your label
-
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
Laurence
*** Sent via Developersdex http://www.developersdex.com ***
|
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 Groups (e.g. data tables, look up tables, etc....)
Table
Fields
|
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 expression to return true as both arrays are
identically the same but it keeps returning false. Any info on how to solve
this problem will be appreciated.
|
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?
|
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?
| |
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 it is best to use an array of string pointers or an array of the actual words. also i am very new to C and really am not comfortable with malloc yet. any help would be greatly appreciated
|
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 handle multiple items. Use an array
to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the...
|
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 table A ....
But here is a little problem, table A has some records that I don't want to be overwritten by the records that are being copied or inserted from table A ....
infact I want that all these records that are being copied from tabe B into...
|
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...
|
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |