making seperate JFrames return objects
I have a JFrame with a GUI, buttons etc. One of the buttons is supposed to bring up a seperate frame, do something, then return an object to the first frame (similar to how JFileChoosers work). I have no idea how to make new frames return something.
Thanks in advance
[280 byte] By [
tschnella] at [2007-11-27 10:37:05]

# 1
> I have a JFrame with a GUI, buttons etc. One of the
> buttons is supposed to bring up a seperate frame, do
> something, then return an object to the first frame
> (similar to how JFileChoosers work). I have no idea
> how to make new frames return something.
Look into the JDialog class. You will want to read the API for that so that you can construct it as a modal object. This will give you the functionality that you desire.
Note that your question is very general and could suggest to some that you want someone else to provide the code for you, in other words, to plagarize. I'm not saying that this was your intent, just that in this forum it can and often is interpretted this way. Next time, I suggest that you show that you are putting a little more effort into it. This includes discussing the results of your google or forum searches, showing code (working or not) that demonstrate your attempts to solve this problem, and then asking specific questions rather than general throw up my hands and give up type questions. If you follow this advice, believe me you will thank me.
Good luck!
# 2
Again thanks for the response.
JDialogs would be a good go, but Here's a more specific example of what I want to do.
Let's say I have an object class called "MagicSpell" which has many variables. I have a database text file which stores the variables for different MagicSpell objects, which my programs can read and initialize MagicSpell objects from.
What I want to do (if I use JDialogs) is have the JDialog load all of these hundreds of MagicSpell objects into a JComboBox (or some other visual array) and have the user be able to see the variables of each MagicSpell on the JDialog as he scrolls through them. I don't think you can add actionlisteners to JDialogs though.
I thought about using clipboard copy+pasting, but I am still readin up on those docs. Would that be a better choice or not?
My original plan was to make a seperate object to handle it. My action for pressing my "load" button would be:
private class SpellChooserButton implements ActionListener
{public void actionPerformed(ActionEvent event){
//a SpellChooser object would bring up a window on initialization
//and show each spell's variables, have a "choose this spell" button which would
//close the window and return the selected object in the JComboBox.
SpellChooser sc = new SpellChooser();
MagicSpell temp = sc.selectSpell();
//...do what I want with the object returned in sc.selectSpell
This works in text based programs, but not in GUIs (not the way I wanted it to anyway). Would I be able to use that SpellChooser object to bring up a new frame and select something? I tried making another frame in the above method, and it worked, but I had no way of returning information back to the original frame. That's where I thought cut and paste might be useful, but I was hoping there was a less-complicated way to do it.
Message was edited by:
tschnell
# 3
> I don't think you can add actionlisteners to JDialogs though.
JDialog will not limit you in any way here. You wouldn't add an actionlistener directly to a JDialog, but you wouldn't with a JFrame either. They are both pretty much analogous, and you can do pretty much anything with a JDialog that you can do with a JFrame. For both, I'd add a JPanel, and on that panel I'd add my JComboBox with actionlisteners and all that.
JFileChooser is a dialog. It let's you choose something and after completion allows you to get the object(s) chosen. Everything you have described can be done (and in my mind should be done with a JDialog. Go to the sun tutorial example download page that I will reference below and download the "Dialog Demo Project". This consists of two java files, DialogDemo.java and CustumDialog.java. Read through both of these files carefully and you will see how JDialog can be extended to do just what you desire.
http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html#DialogDemo
# 4
> My original plan was to make a seperate object to handle it. My action for pressing my "load" button would be:
Your basic code below looks fine. You just have to write the selectSpell() method to return the appropriate object.
The key is to make the dialog modal so that statment isn't executed until the dialog is closed.