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.*;
publicclass GUICDInventory extends JFrame
{
publicstaticvoid 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
publicclass CDInventory
{
// executes application
publicstaticvoid 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
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.
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.
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[] )
publicstatic 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
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