Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

How can i call methods from a separate java class to a GUI class when i implement the actionListener and providing an actionPerformed method?

OpenStudy (anonymous):

I have a worker class which has methods i want to perform action in the GUI driver to get my window running properly

OpenStudy (e.mccormick):

Did you make an object of your worker class?

OpenStudy (anonymous):

yes i'll post it up this is how i did it so far bhut i got errors so i removed my .getsource() statements Worker class(TheaterSales.java: http://dpaste.com/1408670/ so far GUI driver class: http://dpaste.com/1408671/

OpenStudy (anonymous):

retrieving the methods from the worker class is the trick part because the sa;es and total sales field can't be edited and also sales and total sales must be in currency format

OpenStudy (anonymous):

sales*

OpenStudy (e.mccormick):

So the problem is the string being returned? Did you try it with a NumberFormat.getCurrencyInstanc()?

OpenStudy (anonymous):

yes wouldn't i have to use DecimalFormat in the ActionEvent though because it is a double or does it not matter?

OpenStudy (e.mccormick):

All you are doing is returning and displaying a string, unless I am missing something. That method changes it into a properly formatted currency string.

OpenStudy (anonymous):

Ok so with that method it will return my string into currency format for the cost of tickets and refreshments and the total sales? In the attachment is a general window of how my code should work....i have it setup similarly but needed to do the same execution

OpenStudy (e.mccormick):

Then I would not use the toString method. I woild just use the getters. If you use the toString, you will need to parse it and put things in the right places.

OpenStudy (e.mccormick):

Or just make a method to get the results of each part as an individual string.

OpenStudy (e.mccormick):

Well, methods... one for each.

OpenStudy (anonymous):

i'll just remove the toString method because it seems a lot more work and has critical details

OpenStudy (e.mccormick):

See, if you made a method that returned a string for each it could use this: ``` import java.text.NumberFormat; public class Test { public static void main(String[] args) { double d1 = 1 + 4.0 / 7.0; NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println("d1:" + d1 + " Formatted d1:" + fmt.format(d1)); } } ``` d1:1.5714285714285714 Formatted d1:$1.57

OpenStudy (e.mccormick):

Then you just put each string into the right box.

OpenStudy (anonymous):

so if i made a string for each variable i would be using the constant variable instead of the instance variable because the string is supposed to return the currency of each item depending on its quantity

OpenStudy (e.mccormick):

Well, you are going to return them multiplied. That much makes sense because the constants are in the worker method. You can return them as a string or as a float. Your choice really. It depends on where you want to add up the total. Because it is an object that will have the number of tickets of each type set on it, it can do the total and return it as a string. Or, you return the 4 floats, add them to the total, then display formatted strings.

OpenStudy (anonymous):

ohh okay thanks for the help, really appreciate it!

OpenStudy (e.mccormick):

np. You had most of it, which is great. Just needed an idea for how to finish one thing.

OpenStudy (anonymous):

yeah the finishing touch is usually the most important and has too be done correctly or you get a wrong output/display

OpenStudy (e.mccormick):

Some computer errors are more dangerous than others. http://www.ccnr.org/fatal_dose.html

OpenStudy (anonymous):

Hi, again. i completed my code and compiled and ran it. Now when the user inputs any quantity value the sales and the total sales come out to be 0. I've called the getMethods from the TheaterSales.java class these are my codes now: GUI driver class: http://dpaste.com/hold/1409187/ Worker Class: http://dpaste.com/hold/1409188/

OpenStudy (anonymous):

is it the setMethod that needs to be called instead or is calling method incorrect and a result $0 is the display?

OpenStudy (e.mccormick):

Where are your setters being used?

OpenStudy (e.mccormick):

In ``` //Action Events will handle the calculate button and quantity input private class CalcSalesButtonListener implements ActionListener{ ``` I see lots of gets: ``` if(e.getSource() == adTickets){ TS.getAdultTickets(); } ``` But where are the sets?

OpenStudy (anonymous):

They haven't been used in the GUI driver. I tried it once in the if statements and the compiler error said TS.setAdultTickets(adultTickets); it said it couldn't be located in a variable

OpenStudy (e.mccormick):

Somewhere in that you should be using something like textField.getText(); and converting it to the proper type. Like this: http://stackoverflow.com/questions/1995611/j2me-text-field-to-double You feed that value into a setter. You can probably just take it in from the form and put it direct without any sort of intermediate varable.

OpenStudy (anonymous):

make object of u r worker class and call methods of that class by object reference

OpenStudy (anonymous):

@zakiullah2 that is what i ended up doing. i made a String object and called the methods from that constructor to the ActionEvent/ActionListener It worked fine

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!