File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Get information from JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Get information from JFrame" Watch "Get information from JFrame" New topic
Author

Get information from JFrame

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Hello. I have a question, maybe some of you might have an idea if something like this can be done. I have an application where i need to insert some data, and the fields look something like this:

Client(button): clientId(textField)

When i press the button client, a new JFrame opens, where the user can search for the client that he wishes. Now, the problem is, after the user selects the client that he wishes and then closes the client window, i want somehow the id of the client to appear in the clientId textField. Any idea if something like this can be done?

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

How is the user selecting the client ids? A combobox? radiobutton? I imagine it would be a combobox, in which case using the javax.swing.JComboBox API you could call the



After you get this data you can then update the clientId text field.

Hope this was what you were looking for.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
I select the id from a JTable. The problem is that i have to somehow connect the closing of the second window to the first window. The thing is: I have the client id text field, and a button near it. when i press the button, a new client form is opened, where i select an id from a jtable. This id is a global variable for the entire application. What i want to do is: treat the closing of the second window like an event for the first window, meaning that only when the second window closes, the id that i selected is displayed in the first window
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

If you want to watch for when the window closes you need to create a WindowListener. Tutorial for making WindowListeners: http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html

Then whenever the event occurs, update the value of the text field in the first window.


Hunter
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Ok, thanks. That's whati tried to do now, but i have some problems with the variables. I have a class Clients, where i have 2 public variables: hasChanged, of type boolean and clientId of type int. When window closes i set hasChanged to be true, and clientId to the id of the selected client. The problem is when i try to access these variables from the other window, after this one closes. I get the message: non-static variable cannot be referenced from static context. And i have no idea why. The variables are global, i accessed them with Clients.hasChanged and Clients.clientId
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Since the variables are not static you need to access them on an instance of the class Clients, meaning that you need a Clients object in order to access those variables.

with


you are trying to access the variables by using only the class name, which is how static variables are accessed. So to get the values you need to either make these variables static or create a Clients object. From what I have read of your description of what you are trying to do, you only want one value for hasChanged and clientId and you want it to change when the window closes. Making these variables static would allow you to access without an object.

Hunter
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Yes, that worked, but i still can't get the value of the global variable from second frame when it closes.
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
I didn't want to make it more complicated with objects, since i mostly work with sql queries and it seems a bit easier
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

But normally you wouldn't use a JFrame for that. You would use a modal dialog. Go to the JDialog documentation (yes, follow the link right here: JDialog) and then follow the link to the tutorial about how to use dialogs.
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Well, the thing is. I have a frame with client details, and a frame with electronics. I need to be able to access each frame individually, from a main frame, and i also need to access the clients frame from the electronics frame, and get an id from there. Can this be done with a JDialog?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get information from JFrame