I need to make this a popup when a button is clicked. This is the code for the Button.

JButton moreOptionsButton = new JButton("More Options");
        moreOptionsButton.addActionListener( new ActionListener() 
        {

                   public void actionPerformed(ActionEvent e) 
            {
                timer.stop();
                  
            }
        } );

I need this information to come up in a new window. After a option is clicked the window closes or when I click the Close just the new window closes.

{
    	     // Set the title bar text.
     // setTitle("More Options");
        
        // Set the size of the window.
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        
        // Specify an action for the close button.
      //  setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        
        // Add a GridLayout manager to the content pane.
        setLayout(new GridLayout(4, 2));
        
        // Create four buttons.
        JButton fillButton = new JButton("Fill Board");
        fillButton.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                model.clear();
                model.fill();
            }
        } );
        
        JButton shapeButton = new JButton("Shape 1");
        shapeButton.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                model.clear();
                model.shape1();
            }
        } );
        
        JButton helloWorldButton = new JButton("Hello World");
        helloWorldButton.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                model.clear();
                model.helloWorld();
            }
        } );
        
        JButton aboutButton = new JButton("About");
        aboutButton.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                
            }
        } );
        
        
        // Create four labels.
        JLabel fillLabel = new JLabel("Fill Board with Live Cells.");
        JLabel shapeLabel = new JLabel("Display Walking Shape.");
        JLabel helloWorldLabel = new JLabel("Display Hello World.");
        JLabel aboutLabel = new JLabel("Information About Program.");
        
        
        // Create four panels.
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        JPanel panel5 = new JPanel();
        JPanel panel6 = new JPanel();
        JPanel panel7 = new JPanel();
        JPanel panel8 = new JPanel();
        
        
        // Add the labels to the panels.
        panel2.add(fillLabel);
        panel4.add(shapeLabel);
        panel6.add(helloWorldLabel);
        panel8.add(aboutLabel);
        
        // Add the buttons to the panels.
        panel1.add(fillButton);
        panel3.add(shapeButton);
        panel5.add(helloWorldButton);
        panel7.add(aboutButton);
        
        // Add the panels to the content pane.
        add(panel1);  // Goes into row 1, column 1
        add(panel2);  // Goes into row 1, column 2
        add(panel3);  // Goes into row 2, column 1
        add(panel4);  // Goes into row 2, column 2
        add(panel5);  // Goes into row 3, column 1
        add(panel6);  // Goes into row 3, column 2
        add(panel7);  // Goes into row 4, column 1
        add(panel8);  // Goes into row 4, column 2
        
        // Display the window.
        setVisible(true); 
            
    }

Edited 6 Years Ago by LordRazon: n/a

Hello - Im not exactly sure what you want to do. You say you want the actionListener() contents to be shown? You also say that you want a new window to appear - to do that you should just create a new JFrame and in that you can add buttons to close it or perform other tasks. I have added some example code to demonstrate it:

mainBoard class

import javax.swing.*;

public class mainBoard extends JFrame
{
	
	public mainBoard()
	{
		setTitle( "Creating a new Panel" );
		setSize( 350,300 );
		setResizable( false );
		
		add( new Panels() );
		
		setVisible( true );
		setLocationRelativeTo( null );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
	}
	
	public static void main( String [] args )
	{
		new mainBoard();
	}
}

Panels class

import javax.swing.*;

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

public class Panels extends JPanel
{
	JButton alpha;
	JFrame newFrame;
	
	
	
	public Panels()
	{
		setSize( 300,350 );
		setBackground( Color.BLACK );
		setVisible( true );
		
		alpha = new JButton( "Create a Panel" );
		alpha.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.out.println( "Pressed" );
				
				newFrame = new JFrame();
				newFrame.setSize( 200,200 );
				newFrame.setVisible( true );
			}	
		});
	
		add( alpha );
	}
}

Does this help at all?

Or use dialogs?

"I need this information to come up in a new window. After a option is clicked the window closes or when I click the Close just the new window closes."

seems just what dialogs are designed for, do you want something like "are you sure you want to quit?" with yes or no buttons, yes closes program, no closes dialog

http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html

they are alot like JFrames, just tied to the window (ie you can make it so the user cant ignore it and minimise it, an option has to be chosen)

Thanks this is heading in the right direction. Now how do I set the buttons and labels inside the newFrame. I would like to add a Gridlayout with 4 rows 2 cols, for 4 Buttons with 4 labels. I have the coding for the buttons and labels. I just don't know how to get them within the newFrame.

Hello - Im not exactly sure what you want to do. You say you want the actionListener() contents to be shown? You also say that you want a new window to appear - to do that you should just create a new JFrame and in that you can add buttons to close it or perform other tasks. I have added some example code to demonstrate it:

mainBoard class

import javax.swing.*;

public class mainBoard extends JFrame
{
	
	public mainBoard()
	{
		setTitle( "Creating a new Panel" );
		setSize( 350,300 );
		setResizable( false );
		
		add( new Panels() );
		
		setVisible( true );
		setLocationRelativeTo( null );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
	}
	
	public static void main( String [] args )
	{
		new mainBoard();
	}
}

Panels class

import javax.swing.*;

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

public class Panels extends JPanel
{
	JButton alpha;
	JFrame newFrame;
	
	
	
	public Panels()
	{
		setSize( 300,350 );
		setBackground( Color.BLACK );
		setVisible( true );
		
		alpha = new JButton( "Create a Panel" );
		alpha.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.out.println( "Pressed" );
				
				newFrame = new JFrame();
				newFrame.setSize( 200,200 );
				newFrame.setVisible( true );
			}	
		});
	
		add( alpha );
	}
}

Does this help at all?

This article has been dead for over six months. Start a new discussion instead.