Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

I need help passing a reference  RSS feed

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is my setup
I have a GUI that starts my program.
in my GUI I make a instance of speedMathData Class.  
Also from my GUI I would made an options button that opens a new JDialog window.  It contains all my programs settable options.
How can I have my JDalog windows change the settings in my speedMathData Class?


Would it be bad form to just leave the JDialog open but not visible when the OK button is clicked and use something like options.getNumberOfProblems()?
Thanks for the help.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Options are generally done via a menu on the GUI interface, not a Dialog box. Most users, I think would agree, that dialog boxes suck.
 
Damon McNeill
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of a dialog box, create a new window (whose parent is your main interface). Dialog box is limited to a simple choice like "OK", "RETRY", "CANCEL", but you can do anything you like in a new window.
 
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Damon McNeill wrote:. . . you can do anything you like in a new window.

But who wants secondary windows opening in front of their app?

I think there is a problem with the concept of having a speedMath object in the GUI. You don't want to have anything in the GUI. GUIs shouldn't contain things. GUIs shouldn't do things (slight exaggeration for effect, maybe). You shoul‍d be running your speedMath object at the command line and getting it working correctly before you create the GUI. All GUIs do is display things. Well, maybe you can put the numbers in boxes and push the buttons on the GUI too.
Create a math object. Create a class which has a math object as a variable, and which also connects to a GUI. The GUI doesn't then have a math object. The math object has a GUI. Once you get that concept, you will start to get the design right.
You will have things like buttons to push. What you want is to take a number (maybe entered in a text box) and pass it to the GUI object. Then you get a result returned from the GUI object and maybe you want to display that in another text box.

DM is right to warn you away from dialogue boxes, but the real problem you will have is that they are often modal. If you don't dismiss the dialogue box, it will retain focus and you won't be able to use any of the other GUI components. Yes, you can add anything you like to a dialogue box, not only YES NO MAYBE, but it will look much better to have a menu.
 
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Damon McNeill wrote:Dialog box is limited to a simple choice like "OK", "RETRY", "CANCEL", but you can do anything you like in a new window.



But that isn't true. I've designed and implemented in Swing a dialog box -- it's a modal dialog -- which contains an interface to OpenStreetMap and a JTable with a set of locations and the options to choose one of the locations or to create a new location to add to the JTable. A long way from OK, RETRY, CANCEL.
 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Rawley wrote:How can I have my JDialog windows change the settings in my speedMathData Class?



When you create the JDialog, you make sure that the code which creates it has a reference to the data contained in it. (Actually you seem to have that part covered already, based on what you're going to say in the next paragraph.) Then when the user clicks OK, your code which caused the dialog to pop up in the first place should get the data from the JDialog and put it into whatever part of the data model it's supposed to be in. That way the other parts of the application which need to know that stuff don't have to go looking in a dialog box for it.

Would it be bad form to just leave the JDialog open but not visible when the OK button is clicked and use something like options.getNumberOfProblems()?



Reusing a JDialog isn't a bad thing but using it to store application data is definitely bad form.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my "doing stuff" code from my GUI.
Am I doing to Much here?
Also I tried to pass my smd via SMOptions options = new SMOptions(smd); and from my options JDialog I could use smd.setNumberOfProbs, but it didn't seem to affect anything.  Eclipse made me create a constructor to do this.  I'm not sure what to do, but I'm sure I'm doing it wrong.
Thanks again for the help.
I will post all the code if it would help. Thanks






 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your posts it seems that you have some kind of a problem with a JDialog. The problem from our side of the screen is first, we can't tell what your problem is, and second, your posted code doesn't contain anything about the JDialog you're telling us about.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
error is
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at speedMath.SMOptions$1.actionPerformed(SMOptions.java:137)
it's smd.setNumberOfProbs(val);
also Eclipse said I need to add null to compile ---   SMOptions dialog = new SMOptions(null);
thank you everyone for the quick responses.

 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Rawley wrote:it's smd.setNumberOfProbs(val);



If you get a NullPointerException there, it's because smd is null.

also Eclipse said I need to add null to compile ---   SMOptions dialog = new SMOptions(null);



And smd is null specifically because you passed null to the constructor there. Did Eclipse specifically say you had to put null there? Or did it say you needed something there? Because you do need something there, and null isn't what you need. You originally said you needed some kind of speedmathdata object in that position, so why don't you do that?
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I have public SMOptions(speedMathData smd) {
then SMOptions dialog = new SMOptions()   shows constructor SMOptions() is undefined,
when I pick add argument to match SMOptions(speedDataMath) eclipse sets it to null.

this is all the code, sorry its so long.
thanks for all the help












 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Rawley wrote:if I have public SMOptions(speedMathData smd) {
then SMOptions dialog = new SMOptions()   shows constructor SMOptions() is undefined,
when I pick add argument to match SMOptions(speedDataMath) eclipse sets it to null.



Well, who's in charge here? Eclipse reminds you that you didn't provide a parameter to the one and only constructor, and it fixes that. But of course it has no idea what you really want for that parameter, so to make something which compiles correctly it uses null as that parameter. But it's your program and you decided that the constructor needs a SpeedMathData reference. So pass it one already.
 
Bartender
Posts: 2277
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim Rawley, I think you need to have a look at constructors first.
Here's a simple example to help you out:




 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So...........
I thought I was making progress, but I seem to have hit a wall  
Thanks for the help  


 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why your SMOptions class includes a public static void main(String[] args) method -- is that for testing, or something?
 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Rawley wrote:. . .

Your long comments make those lines too long to read easily, but that is only a minor problem.
I think the real question is, “What are you trying to make work?” Are you trying to learn how to write a constructor? Are you trying to learn how to pass information to constructors and methods? Are you trying to learn how to get that number? Or all three? I think you will only learn that if you forget about your GUI for a bit. Start with very small classes. Let's have a Person class with a UML diagram like thisThat means class name, then fields i.e. one private field called name, then a public constructor and a public getName method. I think you are not really supposed to include constructors in UML, but I have done so anyway. You are also supposed to supply all information but I have missed some out intentionally to give you a slightly bigger task. Consider adding a toString() method which must read like this:-I shall leave you to fill in the blanks. Now write this sort of code in another class:-When you have got that working, you will know how to write a constructor. And how to pass it information. It works the same for methods, and you can remind yourself of the procedure in the Java™ Tutorials.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I could pass references to class instances through the constructor to then be able to do standard method calls.  ie. System.out.println(scwc.getNum());  

here is what I did.  I seems kinda wrong, but works.

Also I will study up on Constructors, but I'm not sure they would have helped in this situation anyway.
Thanks..

PS.  SMOptions had Main because Eclipse put it there when I ran the add new wizard for JDialog     It's gone now.
It would seem as thought Eclipse is in charge.  




SMOptions
 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by formatting your code correctly. No {} on the same line (except in array initialisers). Look at lines 31‑35. That is really difficult to read.
I still think you are tying yourself in knots by trying to design an application via a GUI. Also: have you got a definite decision what this application is to do? Write out a simple specification for the app. When you have that specification, you can design the app to meet that specification. Yes, you do need constructors. If you are doing arithmetic you will need at least one number to do the arithmetic with, or usually two numbers. You will have methods to add subtract multiply and divide (maybe called sum difference product quotient and remainder) and maybe even a negation method (but that takes only one operand). You can then write a method with code like this:-You do that with lots of different inputs, some positive, some negative, some 0, and see what happens. Remember what is liable to happen if you divide by 0.
Yes, you will need a constructor. Yes, you will need methods. Yes, you will need fields. You will need to get that code working without a GUI before you even think about GUIs.

Don't say class instances. Say objects or instances. System.out.println(scwc.getNum()); hasn't got any constructors in; you have the name of two objects (System.out and scwc) and you are calling methods on them. That shou‍ld work. You can pass absolutely everything except void to System.out.println, so if it doesn't work, there is something wrong with scwc or getNum. Get your arithmetic class working first.

It appears that Eclipse Exceptions and Rottweilers have something in common. They can smell fear. Eclipse only added a main (not Main) method to your class because you told it to (possibly by ticking a box when you created the class).
Eclipse Exceptions and Rottweilers have something in common. If you show them who is boss, they will roll on their backs and let you tickle their bellies. Well, maybe not Rottweilers, but you do need to beat Eclipse into submission and show it who is in control here.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So as is, my silly app works, but I just wanted to be able to change options via an options screen.

The app makes and answers a set number of questions using the options that are preset.       randomN   (   randomN 1 to 4 to get a symbol + or - or * or / ) randomN   like   1+2  or 12/4 or 3-2
by default it will not create negative numbers or fractions.  I also fixed it so when there is division there will be no 0's.





for each new problem it makes a new Problem object and puts it in an ArrayList.

the main game just loops over the ArrayList and if you answer incorrectly then that problem is added to another ArrayList for missed answers.

I was basically just making flash cards, but I wanted to later incorporate the missed items into the next round.  

Thanks again for the help.


 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Rawley wrote:. . .. . .

Somebody please hit me for not noticing the logic error in that line. I am not certain there is an error, but the variable names suggest it. Please tell us what sort of result you expect from that method if you pass (10, 10) as arguments.

Thanks again for the help.

That's a pleasure

Once you have the application working at the command line (and you haven't yet shown much evidence that it is working), start designing your GUI by sketching what you want it to look like. You may have done that already with the Matisse tool.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure on that one.  I borrowed it from the net, it seems to work correctly.



As far as making it for command line, it feels a bit counter intuitive.  The app is meant to be entirely mouse driven.  I do however get your point of making the base code work before adding a GUI though.

I still feel that I'm missing something fundamental as far passing info back to the parent object from the child object.

I might need component listener, or call back method?


Also
that example helped me get constructors better.  I was just doing everything in methods, but I could save a few steps doing it with a constructor.

Thanks again for the help and input  


 
Paul Clapham
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Rawley wrote:I still feel that I'm missing something fundamental as far passing info back to the parent object from the child object.

I might need component listener, or call back method?



The usual way is, when the "parent" object calls a method of the "child" object, "passing info back" is done by the return statement. You call the method, you get a return value. That's pretty fundamental and also pretty ordinary.

By the way I don't understand why you're using language which implies a parent/child relationship between two objects. Sometimes there's a relationship between two objects as a result of their design, but that doesn't imply that there are special ways for transferring data between them. They're just two objects.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

By the way I don't understand why you're using language which implies a parent/child relationship between two objects. Sometimes there's a relationship between two objects as a result of their design, but that doesn't imply that there are special ways for transferring data between them. They're just two objects.


So I read that and went back to the ClassWithNoP() example to try to keep it simple and realized that I somehow put two mains in it.  When I remove the second unneeded one things worked as expected.



Then I looked at my code on speedMathGui and I wasn't even creating the new object half of the time I was testing it.
I moved the creation of smd to the top and then everything works as intended.  





Thanks for all the help.
I think I'm finally making headway.

 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Rawley wrote:. . . I borrowed it from the net, it seems to work correctly. . . .

I know it is late to comment about that, but I doubt whether you shou‍ld use those two statements together
What is that line supposed to do, what does it actually do, and what do the variables you are using there mean? If I use 10, 10 as the two variables right of the = operator, what range of output would you expect?
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are correct it goes crazy.  

It turns out I never changed the minimumNumber, other that 0, or 1. They both seem to work fine. I'm not sure why.   Maybe I should rename minimumNumber to zeroOrOne.

Thanks for the help.  
Good Times.

 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Rawley wrote:. . . They both seem to work fine. I'm not sure why. . . .

Dangerous combination. You need to work out what you want to do, and then work out what you are actually doing. What would that formula do if both variables had the value 10, as I asked yesterday? You need to know that sort of thing before you can program random numbers confidently. I do not like doing arithmetic with Math#random, because there are much better ways to get random numbers.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

  • 12 11 19 12 12 16 19 11 19 14 14 12 16 12 19 14 15 13 10 19 10 18 11 16 17 13 10 13 19 19 15 15 18 10 13 18 11 15 19 18 13 19 17 17 19 17 12 19 12 16 13 13 14 15 14 11 12 10 11 13 14 13 11 19 11 19 12 13 15 14 16 18 17 13 17 18 11 15 11 15 17 16 10 13 17 11 15 15 13 12 11 15 17 14 18 15 13 12 16 11


  • So I'm not really sure what is happening there.    Maybe starts at 10 plus the following 10.

    (5,10) got
  • 10 5 9 14 11 12 9 9 10 6 9 6 8 6 14 7 9 10 14 8 12 13 11 8 12 7 14 7 7 13 9 14 13 6 5 8 6 8 13 10 13 5 13 14 12 9 14 14 6 12 13 5 11 13 5 5 8 5 9 7 7 14 10 5 13 12 13 5 6 11 8 14 6 8 14 5 5 6 5 8 9 13 9 14 5 9 13 11 6 10 8 14 12 11 5 11 12 6 5 10


  • (1,10) got
  • 10 10 3 3 1 8 9 1 5 4 1 7 4 8 9 1 8 10 9 5 1 7 8 3 3 8 9 8 5 8 6 10 2 8 4 5 9 2 7 6 2 4 7 6 7 2 1 7 8 3 7 2 9 4 5 9 10 10 1 4 4 1 3 8 2 9 2 2 1 9 1 6 5 9 9 10 8 3 8 1 7 9 4 8 1 6 2 2 6 2 1 2 8 10 8 6 6 8 5 8


  • (0,10) got
  • 4 3 1 9 0 5 2 7 0 1 2 6 3 9 9 6 8 9 8 4 0 2 4 9 7 4 7 0 6 7 8 9 0 3 4 9 4 5 5 1 8 2 5 7 8 4 3 2 8 8 6 1 9 2 7 3 2 4 6 2 2 1 4 9 4 3 2 9 9 2 1 1 8 6 4 8 8 3 5 7 8 4 9 2 1 2 1 4 8 1 2 1 1 3 6 5 4 6 2 6


  • seems to work ok as I only use 0, or 1.
     
    Tim Rawley
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm not really sold on Math#random if you have a preferred method.  It does seem to repeat quite a bit.
    I just did a test with bigger numbers (1,1000) and then it didn't repeat.
    Thanks
     
    Campbell Ritchie
    Marshal
    Posts: 64473
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Rawley wrote:. . . So I'm not really sure what is happening there. . . .

    Start by reading the documentation for Math#random(), then work out what you will get if you multiply by 10, then work out what you will get if you cast that to an int. Also compare the precedences of multiplication and cast, for example here. The reason you are not getting repeated values from 1, 1000 is probably because you have a larger range; if you printed 10,000 numbers you would have at least 9*** repeated numbers.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!