popup window

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 7872 Views - Last Post: 29 May 2010 - 09:52 AM Rate Topic: -----

#1 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

popup window

Posted 23 May 2010 - 05:06 PM

Hello I am trying to make a window pop up when you click the new button in my program. and the window will hold values that the main JFrame needs. How do i make the main JFrame wait for the window to close.

main JFrame
public class ProjectM extends JFrame
{	
	private String fileName;
	private int width;
	private int height;
	private String ImageLocation;
	private int tileSize;
	
	private ContextPane CP = new ContextPane();
	
	public ProjectM()
	{newAction.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent e) 
            {
            	CP.setVisible(true);
            	
            	if(CP.stateval() == false)
            	{
            		fileName = CP.Get_File_Name();
            		width = CP.Get_Width();
            		height = CP.Get_Height();
            		ImageLocation = CP.Get_Tile_Directory();
            		tileSize = CP.Get_Tile_Size();
            	}
            	System.out.println(fileName);
                System.out.println("What would you like to call your new map?");
            }
        });
}


popup JFrame
public class ContextPane extends JFrame
{
	private String fileName;
	private String tileDirectory;
	private int tileSize;
	private int height;
	private int width;
	private boolean state;
	
	public ContextPane()
	{
             JButton Cancel = new JButton("Cancel");
		Cancel.setBounds(155,201,80,22);
		
		Cancel.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent e) 
            {
            	exit();
            }
        });
		
		this.add(Cancel);
            public void exit()
	{
		this.setVisible(false);
		state = false;
	}
	
	public boolean stateval()
	{
		return state;
	}
        }


Right now when you click the newaction button it pops the window up and continues going with the button click. but i want it to wait for the window to close before it moves on.

This post has been edited by giuseppe105: 23 May 2010 - 05:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: popup window

#2 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: popup window

Posted 23 May 2010 - 05:16 PM

What i would do is use the isActive() method on the one of the frames and then simply set the Frames visibility to false.
Was This Post Helpful? 0
  • +
  • -

#3 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: popup window

Posted 23 May 2010 - 05:23 PM

View Postm-e-g-a-z, on 23 May 2010 - 04:16 PM, said:

What i would do is use the isActive() method on the one of the frames and then simply set the Frames visibility to false.


The problem is the program keeps running. so when it sets the popup window to be visible it moves onto the next instruction and doesn't wait for the popup window to close. i could run a loop but i'm sure there's some kind of method for doing this.
Was This Post Helpful? 0
  • +
  • -

#4 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: popup window

Posted 23 May 2010 - 07:00 PM

Loop doesn't work it just freezes the whole application. any way to stop the loop from freezing?
Was This Post Helpful? 0
  • +
  • -

#5 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: popup window

Posted 24 May 2010 - 08:06 AM

seems like theres no help on this.

Can someone at least tell me what this window is called so i can search around?
Was This Post Helpful? 0
  • +
  • -

#6 Vestah  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 86
  • Joined: 15-October 09

Re: popup window

Posted 24 May 2010 - 09:50 AM

I would suggest that you let ContextPane extend JDialog instead of JFrame. It provides the functionality you want pretty much for free.

Sun has made a tutorial on creating dialogs: http://java.sun.com/...nts/dialog.html

This post has been edited by Vestah: 24 May 2010 - 09:51 AM

Was This Post Helpful? 1
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: popup window

Posted 24 May 2010 - 04:43 PM

You know, when I posted on this thread:
http://www.dreaminco...panels-in-java/

I meant to post here. By chance, it *almost* makes sense on that thread too. This is what I said: :)

View Postcfoley, on 24 May 2010 - 08:21 AM, said:

A JDialog seems more suited for this than a JFrame since you can have a modal dialog which handles disabling the parent window(s).

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;


// extend JDialog instead of JFrame
public class PopupDemo extends JDialog implements ActionListener {
	
	public PopupDemo() {
		// Make sure it's a model dialog.
		// i.e. you can't interact with any other
		// windows until it is dismissed
		setModal(true);
		
		// add a button to close the dialog
		JButton closePopup = new JButton("Close");
		closePopup.addActionListener(this);
		add(closePopup);
		
		// Ste the dialog to a sensible size and display
		pack();
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// close the dialog
		this.setVisible(false);
	}
	
	public static void main(String[] args) {
		// I've ued the main method here but really, this
		// code could be in any method of any class,
		// including an event handler methood
		System.out.println("Do some stuff before the dialog is shown.");
		new PopupDemo();
		System.out.println("The main thread is halted until the dialog is closed.");
	}

}


Was This Post Helpful? 1
  • +
  • -

#8 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: popup window

Posted 24 May 2010 - 05:18 PM

Can this JDiolog look like this?

Posted Image
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: popup window

Posted 24 May 2010 - 05:28 PM

Sure, You can lay them out just like a JFrame, JApplet or JPanel. I only put one button in mine but you can do whatever you want with yours. :)
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: popup window

Posted 24 May 2010 - 05:32 PM

cfoley is right. It is one of the three Top-Level components in Swing. This means that they contain all other components and none contain them (sort of). The other two are JFrame and JApplet. However, JDialog is good as it is MADE to be called from a JFrame or JApplet and is often seen as a "popup window".

More on top-level components:
http://www.dreaminco...vel-components/
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: popup window

Posted 24 May 2010 - 06:14 PM

View Postgiuseppe105, on 24 May 2010 - 06:18 PM, said:

Can this JDiolog look like this?

Posted Image

JDialogz provide almost all functionnalities of JFrames so just changing extends JFrame by extends JDialog should almost do the trick
Was This Post Helpful? 0
  • +
  • -

#12 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: popup window

Posted 24 May 2010 - 08:00 PM

Ok I got the JDialog working but i have a problem with my added components.

the program starts and makes the JFrame. when you click the "new" button the JDialog pops up. I give it the information. click ok. now i have the JFrame make calls to getters to collect the information. and then i have it make the main part of the app.

called after JDialog "OK" is clicked
public void create_as()
	{
		AS = new AppStarter(this.fileName, this.width, this.height, this.ImageLocation, this.tileSize);
		AS.setBounds(0,0,930,961);
		this.add(AS);
	}


I am getting no error but I do not see everything on the application.

I think its because java isent drawing the new component I add.

I tryed adding repaint(); and i got an outline of my component.

heres a working and a broke pic.

working:
Posted Image

broken:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: popup window

Posted 24 May 2010 - 08:22 PM

As soon as the click is "approved" in the JDialog just
this.dispose(); after you set the correct values in your father
and you will be back to the main JFrame
Was This Post Helpful? 0
  • +
  • -

#14 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: popup window

Posted 25 May 2010 - 02:53 PM

that didn't work its just doing the same thing.
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: popup window

Posted 25 May 2010 - 05:31 PM

Wait, wait, wait. Is your MAIN GUI and JFrame? It should be. A JDialog is merely used for popup dialogs, not entire GUIs...

Can you clarify your issue and show us your code again?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3