0

the sun tutorials doesn't include anything on how to extend JDialog to make a dialog like this:
http://www.oucs.ox.ac.uk/windows/exceed/input.jpg

can anyone point me to a tutorial that included this?

my attempts to try making a frame into a dialog result in 100% cpu and infinite loops how do you make the program wait for a user to click a button or something else?

2
Contributors
1
Reply
4
Views
9 Years
Discussion Span
Last Post by Ezzaral
0

There is a somewhat convoluted example from the Swing tutorial here: http://java.sun.com/docs/books/tutorial/uiswing/examples/components/DialogDemoProject/src/components/CustomDialog.java
(It's a Java file so you may have to download it)

Here is a simpler, more direct example that I slapped together. Perhaps it will help:

import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JRadioButton;

public class CustomDialog extends JDialog{
    JRadioButton button1;
    JRadioButton button2;
    
    public CustomDialog(Frame owner){
        // create a modal dialog that will block until hidden
        super(owner, true);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        button1 = new JRadioButton("First thing");
        button1.setSelected(true);
        contentPane.add(button1);
        button2 = new JRadioButton("Second thing");
        contentPane.add(button2);
        
        ButtonGroup bGroup = new ButtonGroup();
        bGroup.add(button1);
        bGroup.add(button2);
        
        JButton btnOk = new JButton("Ok");
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // This will "close" the modal dialog and allow program
                // execution to continue.
                setVisible(false);
                dispose();
            }
        });
        contentPane.add(btnOk);
        pack();
    }
    
    public String showDialog(){
        // show the dialog
        // this will block until setVisible(false) occurs
        setVisible(true);
        // return whatever data is required
        return button1.isSelected() ? button1.getText() : button2.getText();
    }
    
    public static void main(String[] args) {
        CustomDialog dialog = new CustomDialog(null);
        String userChoice = dialog.showDialog();
        System.out.println("User chose: "+userChoice);
    }

}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.