I'm trying to understand how to create and use dialogs. Could someone
explain how to build a swing dialog that would show up to 10 different
buttons, that can change dynamically based on some state? Only by
clicking one of the buttons should close the dialog. I don't know if
or how I should use a subclass of JDialog or what.
I would like to do something like:
void myButtonActionPerformed(ActionEvent e)
{
myDialog.setState(currState);
myDialog.setVisible(true);
String myText = myDialog.getResult();
// do something with myText
}
> I'm trying to understand how to create and use dialogs. Could someone
> explain how to build a swing dialog that would show up to 10 different
[quoted text clipped - 11 lines]
> // do something with myText
> }
You don't have to subclass it because you are not going to add to it's
functionality. Just create a JDialog and add your components.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test8 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
final JDialog dialog =
new JDialog((Frame)null,"Title",true);
dialog.setLayout(new GridLayout(3,0));
JButton b = new JButton("Add a Button");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dialog.add(new JButton("New Button"));
dialog.validate();
}
});
dialog.add(b);
b = new JButton("Close");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dialog.setVisible(false);
}
});
dialog.add(b);
dialog.setSize(400,300);
dialog.setVisible(true);
dialog.dispose(); // always dispose your used windows
}
});
}
}

Signature
Knute Johnson
email s/nospam/knute/
------->>>>>>http://www.NewsDem
dweesie - 11 Jan 2008 20:25 GMT
What's the normal way for the parent window to discover what button
was pressed after the dialog has been hidden?
RedGrittyBrick - 11 Jan 2008 23:28 GMT
> What's the normal way for the parent window to discover what button
> was pressed after the dialog has been hidden?
See http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
One way is to have the ActionListener for each button set a variable and
either inspect that variable or set a field (instance variable) and
provide access to it's value through a getter method.