• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple Accept / Reject Dialog refuses to work

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought this part would be easy. It's ended up costing me hours of failed attempts.

My problem:

I'm creating a program to assist a fantasy football team owner during a live draft. The program will recommend a player when it's his turn to draft. I have the main functionality of the system working. But the part where it asks if the user wants to accept or reject the recommendation just won't work.

Basically, the program reads in player data from an excel spreadsheet and creates an arraylist of players. Certain factors are applied to the players projected fantasy point total and the list is sorted. Then I just need it to pop up a window that displays certain attributes of the top player in the list and asks "Do you accept or reject this player?". Then two buttons. I just want it to return a true or false to the calling class.

I have tried using jOptionPanel, but that didn't seem to work for me. Now I'm trying a window with jButtons. I can get all the classes to compile without errors in Eclipse, but the run of the AcceptReject window spits out a bunch of red error stuff in the console. It hangs up on the initGUI() method. I will try to post the pertinent code in the next post.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Player Class:

The call to AcceptReject from the main..


The AcceptReject class..

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have tried using jOptionPanel, but that didn't seem to work for me.


ItDoesntWorkIsUseless. JOptionPanel ? Which library are you using ?
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant jOptionPane. If this is the way to go, I'm all for it. I just couldn't get it to work. But, I need it to display attributes of a Player object passed to it, if that is possible.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can concatenate the attribute values and pass them to the JOptionPane as a String. Do you need more ?
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I do. I think what is confusing me is that the examples of an option pane don't seem to include a listener. Is that correct? Can an option pane just be thrown into a sequence of code to quickly get a yes/no?

When I tried something like this...


it just seemed to skip over it and do nothing. Is there something else I need to do?

Also, I thought at first I could embed this option pane inside a window that has the player info in it. Is it possible to have an embedded option pane that closes the whole window and returns a simple answer?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no reason why it wouldn't show up. Are you sure you called the method where this source resides ?
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I got this to work..


So, I have to cram all the player info into the message line? Is there any way to embellish that?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're free to make your own JDialog is the default JOptionPane visuals do not suit your needs.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Phew, I found my error. Another stupid mistake. I was calling a different class that was very similar than the one I kept tweaking. I forgot to change the call when I made a copy and renamed it. Duh, again. I think it helped just posting on here though to organize my thoughts. This program is getting quite complex for a beginner, and I'm starting to lose track of things.

But, my question still stands. Is there any way to embellish the message in the jOptionPane? Or, is it possible to embed it into a window that has the player info?

Thanks again for your time and patience.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can pass a custom component to the message parameter. For example a JPanel. Have a look here.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, now that I realize my problem, maybe my original method would also work.

But now I have a related question. The main window of this program will display several buttons. One button will be player data that will open a new window. It has a list of players on the left, and when one is selected, their data displays in fields on the right. This works fine. But, while I'm runnning a draft simulation, I am only able to interact with the Accept / Reject dialog. It won't allow me to check any player data until the draft simulation is done.

Does this have something to do with threading? How would I be able to interact with multiple windows at the same time? I also need to have a team roster display that updates as the draft progresses. This would also need to update concurrently with the draft sequence. I assume this would be threading as well?

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your dialog is probably modal. Modal dialog block access to its parent window. You'd need to make the dialog non-modal, which automatically means you can't use JOptionPane (or at least its static methods).
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so if I go back to the original method that I posted, creating a dialog from scratch, I should be able to interact with all windows that are open? No need for threading and synchronizing? I'm starting to read up on threading, and it seems a bit daunting since I have less than a week to go on this project.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you don't need to read about threading (yet). If you have several (non-modal) windows open, your code should be prepared for the user to interact with any of them at any time. That's all. It's no different than if you had all the components combined into a single window.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All user interaction occurs on the same thread, called the Event Dispatcher Thread. So even with multiple dialog, each (mouse, keyboard, etc) event is handled sequentially. You will only need to take care that you don't expect one specific sequence - the order of events will be unpredictable if multiple windows get events at roughly the same time. But they will still be handled one by one.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is good news. Thank you. I've been cramming enough java junk in my head for the past few weeks, I'm not sure I can conquer a new topic like that at this point.

So, the jOptionPane is out since it's inherently modal. I'll hammer on the original method when I get home. Bummer, I had it working great with jOptionPane. I have that as a fallback tho.

Thanks for the help.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't throw away my JOptionPane code just yet. I said you couldn't use the static methods. I think you can still use JOptionPane like this, but doing some of the stuff the static methods do manually. In essence:
The only thing you miss out is the automatically returned value, but even that can be determined, by using a WindowListener. In short:
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I tried going back to my original attempt at an Accept Reject window with 2 jbuttons. I corrected the errors and I get my window finally. It just doesn't seem to return a value.

What am I missing to return a value from my Accept/Reject popup window?

So, the call to the AcceptReject is inside a loop. The call lines look like this...


Now the AcceptReject class....



So, I think it's not changing the value of accepted. It is also not waiting for the input to continue the draft loop. It just zips thru the rest of the automatic picks and the AcceptReject window is still there. Clearly, my window has no power.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:You can pass a custom component to the message parameter. For example a JPanel. Have a look here.



Now I'm trying this. I'm trying to adapt the code for the standalone jOptionPane with the CustomMessage component. This will display adequately for my purposes, although I would still like to get my other window to work. It seems so close.

So, the code for the jOptionPane is a standalone application. So, I'm trying to replace the main with a method.


Then this is the call line in the parent class..


I'm missing something again. ap is the arraylist of players. I need to send the recommended player to this Accept Reject dialog.

And again, I'm confused about how to get a usable piece of data from this.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Listen to your error messages as they'll often solve your problem for you. Here:


You are creating a constructor but are giving it a "void" return type and turning it into a method. Get rid of the void and make it a true constructor. Again, the error message tells you exactly what is wrong.
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, good. Thank you. That works fine. My custom window pops up with the player's attributes in separate text fields.

HOWEVER

I'm still not sure how to get a yes or no piece of data out of it. When you call a JOptioPane explicitly right in the code you do something like..



When I go to try something like that in the CustomObjectPane class like this...


I get the "This method has a constructor name." error. How else can I return the value? I'm sorry if I'm asking something I should already know, but I've been swimming in java for the last three weeks. I've seen so much code, with so many different ways to do something, I'm just really getting it all mixed up in my head.

Thanks again for all the help. I'm sort of relying on you guys to get thru to the thursday deadline. I'm really close though. the logic of my program works great, it's just this GUI business that has me all twisted around.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exactly the same error as previously stated

when you have a class called CustomObjectPane, the constructor is
public CustomObjectPane(..)

if you try to put a return type void/int/whatever, you get that message

move what you're trying to do into another method, and call it from the constructor if needed
 
Jason Guyton
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HA! Finally got this dang thing to work. Thanks for the clues guys. Phew! Now on to the other issues.

See my thread regarding displaying arraylist data in a jtable. Thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic