How do I get a selected value from JDialog?

M

mwmann

Hi everyone

I have a class which extends JDialog, its purpose is to show a list of
values retrieved from a database, which will allow the user to select
from a table in this Dialoge, the selected string value in the dialog
should be accessable from the Parent form when the dialog is closed/OK
pressed- or a text field in the parent field populated when OKAY
clicked.

How do go about retrieving the user selected value from the dialog ?

Code samples/ psuedo code would be helpfull.

Thanks
 
M

M.J. Dance

mwmann said:
Hi everyone

I have a class which extends JDialog, its purpose is to show a list of
values retrieved from a database, which will allow the user to select
from a table in this Dialoge, the selected string value in the dialog
should be accessable from the Parent form when the dialog is closed/OK
pressed- or a text field in the parent field populated when OKAY
clicked.

How do go about retrieving the user selected value from the dialog ?

Code samples/ psuedo code would be helpfull.

In order to be of any use, your dialog (extending JDialog) must have some
controls (like JTextTield). Those have methods which enable you to access the
data they're holding (like getText()). Just "delegate" those methods:

public class MyDialog extends JDialog {
JTextField blahblahField;
...
String getBlahblahText() {
return blahblahField.getText();
}
...
}

//"Parent form" code
....
MyDialog myDialog = ...
....
String blahblahString = myDialog.getBlahblahText();
....

OK?
 
K

Knute Johnson

mwmann said:
Hi everyone

I have a class which extends JDialog, its purpose is to show a list of
values retrieved from a database, which will allow the user to select
from a table in this Dialoge, the selected string value in the dialog
should be accessable from the Parent form when the dialog is closed/OK
pressed- or a text field in the parent field populated when OKAY
clicked.

How do go about retrieving the user selected value from the dialog ?

Code samples/ psuedo code would be helpfull.

Thanks

Here is a simple working example for you. There are literally hundreds
of ways to do this task but these are the critical elements.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test4 {
static JTextField tf;
static JButton b;

public static void main(String[] args) {

class DataDialog extends JDialog {
public String result;

public DataDialog(Frame parent, boolean modal, String[] data) {
super(parent,modal);

final JComboBox bx = new JComboBox(data);
add(bx,BorderLayout.WEST);
final JButton b = new JButton("OKAY");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
result = (String)bx.getSelectedItem();
setVisible(false);
dispose();
}
});
add(b,BorderLayout.EAST);
pack();
setLocationRelativeTo(parent);
}
}

Runnable r = new Runnable() {
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// text field to hold result
tf = new JTextField();
f.add(tf,BorderLayout.NORTH);

b = new JButton("Get Data From DB");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// disable button so you can't try to retrieve
while
// the previous request is going on
b.setEnabled(false);
// it could take a while so retrieve the data in
// another thread
Runnable r = new Runnable() {
public void run() {
// simulate retrieving data from db
try { Thread.sleep(1000); }
catch (InterruptedException ie) { }
// this is simulated data
String[] s = {"one","two","three","four"};
// create dialog
DataDialog d = new DataDialog(f,true,s);
d.setVisible(true);
// get data from public variable in
DataDialog
// and show data in text field in frame
tf.setText(d.result);
// re-enable button
b.setEnabled(true);
}
};
new Thread(r).start();
}
});
f.add(b,BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
 
M

mwmann

Thanks, I will give this a try.

Knute said:
mwmann said:
Hi everyone

I have a class which extends JDialog, its purpose is to show a list of
values retrieved from a database, which will allow the user to select
from a table in this Dialoge, the selected string value in the dialog
should be accessable from the Parent form when the dialog is closed/OK
pressed- or a text field in the parent field populated when OKAY
clicked.

How do go about retrieving the user selected value from the dialog ?

Code samples/ psuedo code would be helpfull.

Thanks

Here is a simple working example for you. There are literally hundreds
of ways to do this task but these are the critical elements.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test4 {
static JTextField tf;
static JButton b;

public static void main(String[] args) {

class DataDialog extends JDialog {
public String result;

public DataDialog(Frame parent, boolean modal, String[] data) {
super(parent,modal);

final JComboBox bx = new JComboBox(data);
add(bx,BorderLayout.WEST);
final JButton b = new JButton("OKAY");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
result = (String)bx.getSelectedItem();
setVisible(false);
dispose();
}
});
add(b,BorderLayout.EAST);
pack();
setLocationRelativeTo(parent);
}
}

Runnable r = new Runnable() {
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// text field to hold result
tf = new JTextField();
f.add(tf,BorderLayout.NORTH);

b = new JButton("Get Data From DB");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// disable button so you can't try to retrieve
while
// the previous request is going on
b.setEnabled(false);
// it could take a while so retrieve the data in
// another thread
Runnable r = new Runnable() {
public void run() {
// simulate retrieving data from db
try { Thread.sleep(1000); }
catch (InterruptedException ie) { }
// this is simulated data
String[] s = {"one","two","three","four"};
// create dialog
DataDialog d = new DataDialog(f,true,s);
d.setVisible(true);
// get data from public variable in
DataDialog
// and show data in text field in frame
tf.setText(d.result);
// re-enable button
b.setEnabled(true);
}
};
new Thread(r).start();
}
});
f.add(b,BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
 
M

mwmann

Thanks, I will give this a try.


M.J. Dance said:
In order to be of any use, your dialog (extending JDialog) must have some
controls (like JTextTield). Those have methods which enable you to access the
data they're holding (like getText()). Just "delegate" those methods:

public class MyDialog extends JDialog {
JTextField blahblahField;
...
String getBlahblahText() {
return blahblahField.getText();
}
...
}

//"Parent form" code
...
MyDialog myDialog = ...
...
String blahblahString = myDialog.getBlahblahText();
...

OK?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top