| | |
Help with displaying text through GUI interface
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 1
Reputation:
Solved Threads: 0
This is my code, I could get the program to run before i tried to get it to display using a GUI interface; can anyone help please?
//Inventory Program Part5
import java.util.*; //=============================import all java.util classes
import java.text.*;
import javax.swing.*;
class Product //============================================start Product class
{
public String[] className; //=====================================class's name
public int[] classNumber; //=====================class's unique product number
public int[] classQuantity; //=======================class's quantity in stock
public double[] classPrice; //===============================class's price per
public Product(String[] name, int[] number, int[] quantity, double[] price)
//==========================================================product constructor
{
className = name; //==============================set className to name
classNumber = number; //======================set classNumber to number
classQuantity = quantity; //==============set classQuantity to quantity
classPrice = price; //==========================set classPrice to price
} //====================================================end Product constructor
public Product SortedArray(Product Inventory5) //======start method SortedArray
{
String[] name=new String[Inventory5.className.length];
//=================================================new name for sorting array
int [] number = new int[Inventory5.classNumber.length];
//===============================================new number for sorting array
int[] quantity = new int[Inventory5.classQuantity.length];
//=============================================new quantity for sorting array
double [] price = new double [Inventory5.classPrice.length];
//=================================================new price for sorting array
name = (String[])Inventory5.className.clone();
//=================================================place name in sorting array
Arrays.sort(name); //=============================================sort by name
for (int counter = 0; counter < name.length; counter++)
//==========================================loop and counter for sorting array
{
for(int counter2=0;counter2<name.length;counter2++)
//====================loop and counter to match unsorted array and sorted one
{
if(name[counter].equals(Inventory5.className[counter2]))
//=====================================if statement for when a match occurs
{
quantity[counter]=Inventory5.classQuantity[counter2];
//=============================set quantity equal to sorted array quantity
price[counter]=Inventory5.classPrice[counter2];
//===================================set price equal to sorted array price
number[counter]=Inventory5.classNumber[counter2];
//=================================set number equal to sorted array number
break; //============================================break for if statement
} //========================================================end if statement
} //====================================================end for loop counter2
} //======================================================end for loop counter
Product SortedProductArray = new Product (name, number, quantity, price);
//=========================new sorted product array replace old product array
return SortedProductArray; //==================return new product array sorted
} //=====================================================end method SortedArray
public double TotalInvWorth(int[] quantity, double[] price)
//=====start method TotalInvWorth
{
double total=0.00F; //====================================set double total = 0
for (int counter = 0; counter < quantity.length; counter++)
//================loop and counter to multiply each quantity x price in array
{
double perprodworth=quantity[counter]*price[counter];
//============= multiply quantity x price per counter in array = perprodworth
total=total+perprodworth; //=========================add perprodworth to total
}
return total; //==================================return total in TotalInvWorth
} //===================================================end method TotalInvWorth
} //===========================================================end Product class
public class Inventory5 extends JFrame
{
JTextArea _resultArea = new JTextArea(20,40);
//=================================================== start class Inventory5
public static void main(String[] args) //==============start main method
{
//=========================setting static data for variables in each array
String[] name = {"Java Programming IT", "Internet Concepts IT",
"Analysis & Design IT", "Computer Networking IT"};
int[] number = {315, 320, 321, 330};
int[] quantity = {1, 1, 1, 1};
double[] price = {(double) 855.00, (double) 855.00, (double) 855.00,
(double) 855.00};
Product Inventory5 = new Product (name, number, quantity, price);
//=========================================needed to resolve constructor
JOptionPane.showMessageDialog(null, "Course Name: " className,
"\nCourseID: " classNumber, "\n Price of Remaining Class: $"
classPrice, "\nTotal of Remaining Courses: $" TotalInvWorth,
JOptionPane.PLAIN_MESSAGE);
} //========================================================end main method
} //======================================================= end class Inventory5
//Inventory Program Part5
import java.util.*; //=============================import all java.util classes
import java.text.*;
import javax.swing.*;
class Product //============================================start Product class
{
public String[] className; //=====================================class's name
public int[] classNumber; //=====================class's unique product number
public int[] classQuantity; //=======================class's quantity in stock
public double[] classPrice; //===============================class's price per
public Product(String[] name, int[] number, int[] quantity, double[] price)
//==========================================================product constructor
{
className = name; //==============================set className to name
classNumber = number; //======================set classNumber to number
classQuantity = quantity; //==============set classQuantity to quantity
classPrice = price; //==========================set classPrice to price
} //====================================================end Product constructor
public Product SortedArray(Product Inventory5) //======start method SortedArray
{
String[] name=new String[Inventory5.className.length];
//=================================================new name for sorting array
int [] number = new int[Inventory5.classNumber.length];
//===============================================new number for sorting array
int[] quantity = new int[Inventory5.classQuantity.length];
//=============================================new quantity for sorting array
double [] price = new double [Inventory5.classPrice.length];
//=================================================new price for sorting array
name = (String[])Inventory5.className.clone();
//=================================================place name in sorting array
Arrays.sort(name); //=============================================sort by name
for (int counter = 0; counter < name.length; counter++)
//==========================================loop and counter for sorting array
{
for(int counter2=0;counter2<name.length;counter2++)
//====================loop and counter to match unsorted array and sorted one
{
if(name[counter].equals(Inventory5.className[counter2]))
//=====================================if statement for when a match occurs
{
quantity[counter]=Inventory5.classQuantity[counter2];
//=============================set quantity equal to sorted array quantity
price[counter]=Inventory5.classPrice[counter2];
//===================================set price equal to sorted array price
number[counter]=Inventory5.classNumber[counter2];
//=================================set number equal to sorted array number
break; //============================================break for if statement
} //========================================================end if statement
} //====================================================end for loop counter2
} //======================================================end for loop counter
Product SortedProductArray = new Product (name, number, quantity, price);
//=========================new sorted product array replace old product array
return SortedProductArray; //==================return new product array sorted
} //=====================================================end method SortedArray
public double TotalInvWorth(int[] quantity, double[] price)
//=====start method TotalInvWorth
{
double total=0.00F; //====================================set double total = 0
for (int counter = 0; counter < quantity.length; counter++)
//================loop and counter to multiply each quantity x price in array
{
double perprodworth=quantity[counter]*price[counter];
//============= multiply quantity x price per counter in array = perprodworth
total=total+perprodworth; //=========================add perprodworth to total
}
return total; //==================================return total in TotalInvWorth
} //===================================================end method TotalInvWorth
} //===========================================================end Product class
public class Inventory5 extends JFrame
{
JTextArea _resultArea = new JTextArea(20,40);
//=================================================== start class Inventory5
public static void main(String[] args) //==============start main method
{
//=========================setting static data for variables in each array
String[] name = {"Java Programming IT", "Internet Concepts IT",
"Analysis & Design IT", "Computer Networking IT"};
int[] number = {315, 320, 321, 330};
int[] quantity = {1, 1, 1, 1};
double[] price = {(double) 855.00, (double) 855.00, (double) 855.00,
(double) 855.00};
Product Inventory5 = new Product (name, number, quantity, price);
//=========================================needed to resolve constructor
JOptionPane.showMessageDialog(null, "Course Name: " className,
"\nCourseID: " classNumber, "\n Price of Remaining Class: $"
classPrice, "\nTotal of Remaining Courses: $" TotalInvWorth,
JOptionPane.PLAIN_MESSAGE);
} //========================================================end main method
} //======================================================= end class Inventory5
You can't force showMessageDialog to something it is not build for :cheesy:
You giving it is : Component, object, object, object and on the end title and message type
where java knows only of this method
showMessageDialog(Component parentComponent, Object message, String title, int messageType)
http://java.sun.com/javase/6/docs/ap....String,%20int)
You have two options either you will create your own showMessageDialog or you will output result in some other component (example JFrame)
Secondly you need to make some changes to the way you try to call your variables. It is a bad idea to name Product variable as Inventory5 which is your class name, if you want it with that name you should use lower case for first letter. Than className is an array, if you want to get value from array you have to give it position from which you want to retrive value. You can see my correction bellow
You giving it is : Component, object, object, object and on the end title and message type
where java knows only of this method
showMessageDialog(Component parentComponent, Object message, String title, int messageType)
http://java.sun.com/javase/6/docs/ap....String,%20int)
You have two options either you will create your own showMessageDialog or you will output result in some other component (example JFrame)
Secondly you need to make some changes to the way you try to call your variables. It is a bad idea to name Product variable as Inventory5 which is your class name, if you want it with that name you should use lower case for first letter. Than className is an array, if you want to get value from array you have to give it position from which you want to retrive value. You can see my correction bellow
•
•
•
•
Product class
public class Inventory5 extends JFrame
{
JTextArea _resultArea = new JTextArea(20,40);
//=================================================== start class Inventory5
public static void main(String[] args) //==============start main method
{
//=========================setting static data for variables in each array
String[] name = {"Java Programming IT", "Internet Concepts IT",
"Analysis & Design IT", "Computer Networking IT"};
int[] number = {315, 320, 321, 330};
int[] quantity = {1, 1, 1, 1};
double[] price = {(double) 855.00, (double) 855.00, (double) 855.00,
(double) 855.00};
Product product = new Product (name, number, quantity, price);
//=========================================needed to resolve constructor
JOptionPane.showMessageDialog(null, "Course Name: " product.className[0],
"My Message",JOptionPane.PLAIN_MESSAGE);
} //========================================================end main method
} //======================================================= end class Inventory5
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Starting Python (Python)
- Impementing First GUI into Current App (Java)
- sms through pc (Visual Basic 4 / 5 / 6)
- Python - Card Class - Graphics Win (Python)
- Data From C++ GUI to Excel/Notepad, Application Trouble (C++)
- Secondary PCI Bus? What's that? (Win98SE) (Windows 95 / 98 / Me)
Other Threads in the Java Forum
- Previous Thread: autoscroll jscrollpane
- Next Thread: JFrame refuses to resize
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception fractal free game gameprogramming gis givemetehcodez graphics gui helpwithhomework html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nextline nls number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






