I thought this part would be easy. It's ended up costing me hours of failed attempts.
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.
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?
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?
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?
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).
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.
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.
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.
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:
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.
posted 10 years ago
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.
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.
posted 10 years ago
OK, good. Thank you. That works fine. My custom window pops up with the player's attributes in separate text fields.
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.