Results 1 to 3 of 3

Thread: 'Blocking' calls like dialog forms in .NET

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    'Blocking' calls like dialog forms in .NET

    Hey,

    I need to show a 'level select' window in my game, and it's only purpose is to select a level from a list of available levels, and return the filename of that level.

    In .NET, I would have made a dialog form, display it with ShowDialog such that the calling code would halt until the form returned, and then used a property to communicate which level was selected. For example, in C# this could look like
    csharp Code:
    1. string levelPath = String.Empty;
    2. frmLevelSelect f = new frmLevelSelect();
    3. if (f.ShowDialog() == DialogResult.OK)
    4. {
    5.     levelPath = f.SelectedLevel;
    6. }
    7.  
    8. if (levelPath != String.Empty)
    9. {
    10.     // ... load level
    11. }

    The ShowDialog call waits until the DialogResult has been set in the frmLevelSelect form, so we can be sure that, if the user pressed OK, he has actually selected a level.

    If I try the same in java, replacing ShowDialog with setVisible(true), then it does not block, but it returns immediately, and obviously the user has had no chance to select a level yet.



    How can I do this? Or is there no built-in mechanism for this and should I use threads?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: 'Blocking' calls like dialog forms in .NET

    Use the JDialog. it works the same. Just make sure this JDialog is a modal
    Code:
    import java.awt.Frame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    
    public class Test extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	private final JButton button;
    
    	public Test()
    	{
    		super("Test");
    		button = new JButton("Click");
    		button.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(final ActionEvent e)
    			{
    				new TestDialog(Test.this);
    				JOptionPane.showMessageDialog(Test.this, "Hello World!");
    			}
    		});
    		this.add(button);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		pack();
    	}
    
    	public static void main(final String[] args)
    	{
    		new Test().setVisible(true);
    	}
    }
    
    class TestDialog extends JDialog
    {
    	private static final long serialVersionUID = 1L;
    
    	public TestDialog(final Frame owner)
    	{
    		super(owner, true);
    		this.add(new JTextField("Hello World!"));
    		setLocationRelativeTo(owner);
    		pack();
    		setVisible(true);
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width