944,209 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3612
  • Java RSS
Mar 4th, 2007
0

Help with displaying text through GUI interface

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
meha1331 is offline Offline
1 posts
since Mar 2007
Mar 5th, 2007
0

Re: Help with displaying text through GUI interface

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

Click to Expand / Collapse  Quote originally posted by meha1331 ...
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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 875
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: autoscroll jscrollpane
Next Thread in Java Forum Timeline: JFrame refuses to resize





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC