aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Simple Accept / Reject Dialog refuses to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Simple Accept / Reject Dialog refuses to work " Watch "Simple Accept / Reject Dialog refuses to work " New topic
Author

Simple Accept / Reject Dialog refuses to work

Jason Guyton
Ranch Hand

Joined: Apr 15, 2010
Posts: 40
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

Joined: Apr 15, 2010
Posts: 40
The Player Class:

The call to AcceptReject from the main..


The AcceptReject class..

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

ItDoesntWorkIsUseless. JOptionPanel ? Which library are you using ?


[My Blog]
All roads lead to JavaRanch
Jason Guyton
Ranch Hand

Joined: Apr 15, 2010
Posts: 40
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

You can concatenate the attribute values and pass them to the JOptionPane as a String. Do you need more ?
Jason Guyton
Ranch Hand

Joined: Apr 15, 2010
Posts: 40
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Apr 15, 2010
Posts: 40
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

You're free to make your own JDialog is the default JOptionPane visuals do not suit your needs.
Jason Guyton
Ranch Hand

Joined: Apr 15, 2010
Posts: 40
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

You can pass a custom component to the message parameter. For example a JPanel. Have a look here.
Jason Guyton
Ranch Hand

Joined: Apr 15, 2010
Posts: 40
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?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jason Guyton
Ranch Hand

Joined: Apr 15, 2010
Posts: 40
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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

Joined: Apr 15, 2010
Posts: 40
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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

Joined: Apr 15, 2010
Posts: 40
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

Joined: Apr 15, 2010
Posts: 40
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.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Apr 15, 2010
Posts: 40
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.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Apr 15, 2010
Posts: 40
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Accept / Reject Dialog refuses to work