Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get information from JFrame

 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic