Re: Getting data from a JDialog
I have set followups to comp.lang.java.gui (reset it if you don't like
this).
zelao.itu@gmail.com wrote:
Hi everybody, I'm doing an app where I need to get a value from a
variable of another JDialog.
My MainFrame call the class SelectClient, where SelectClient has a
JTable with all data. when user double click over the table it supose
to return the data of the selected cell.
I tried like this :
//main.java
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btCad) {
SelectClient tmp = new SelectClient(this);
This next part shouldn't be needed normally.
while(tmp.running) {
try {
wait();
} catch ( InterruptedException e1) { }
}
String data = tmp.data;
You don't say what you expected and what actually happened.
I'd use a debugger or insert a debug statement like
System.out.println("dialog returned data: '"+data+"'.");
tmp.dispose();
I don't think you need to explicitly dispose of object references. Just
make sure their scope is limited and they should be garbage collected
automatically. The Sun tutorials on Dialogs have the dialog do
..setVisible(false) when the OK/Cancel (or equivalent) buttons are
pressed. This seems to be sufficient - I'd do that inside your
SelectClient class.
//SelectClient.java
class SelectClient extends JDialog ....
public void mouseClicked(MouseEvent e) {
if (e.getSource() == tableClients) {
if (e.getClickCount() == 2) {
data = tableModel.getPrimaryKey(1);
My guess (since you don't provide a complete program) is that
getPrimaryKey(1) doesn't do what you think it does.
running = false;
notifyAll();
}
}
}
Your code is indented using a mix of tabs and spaces, which is why it
looks a mess in the newsgroup. I configured my IDE/Editor to expand tabs
to spaces - it avoids this problem. You might like to try that.