School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,156 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,995 people online right now. Registration is fast and FREE... Join Now!



have more then 1 form

have more then 1 form how do i get more then 1 form up Rate Topic: -----

#1 RETNE  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 17-November 07


Dream Kudos: 0

Post icon  Posted 04 October 2008 - 11:04 AM

just want to know how to open a second form when a button is pressed
Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 04 October 2008 - 11:50 AM

Modifying the code of an example I was just given in another question, here is how you can show multiple forms.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PizzaForm extends JFrame implements ActionListener {
	JButton button; 
	
	// Create an instance of the form and its operations
	public PizzaForm() {
		setSize(300,300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("New Form");
		setLayout(new BorderLayout());
		
		// Create a button, attach it to an event (actionPerformed listed below) and add it to the form
		button = new JButton("New Form");
		button.addActionListener(this);
		add(button,BorderLayout.NORTH);
		
		// Show the form
		setVisible(true);
	}

	// When the button is pressed, create a new instance of the class which shows a new form.
	// Now this part can be any form class you created etc. Here I just keep creating the same type of form.
	public void actionPerformed(ActionEvent e) {
        PizzaForm pizza = new PizzaForm();    
	}
    
	// On start create the form which calls the constructor, assembles the form, then shows it.
    public static void main(String args[]){
        PizzaForm pizza = new PizzaForm(); 
    }
}



Read through the in code comments and you will see that main creates a form object called "pizza". During this creation it calls the constructor which sets up the form and adds a button to the form. This button is attached to an event which creates more instances of the form.

So essentially this example will create as many forms as you like depending on how many times you click the button.

Hope this is straight forward enough for you. Enjoy!

"At DIC we be pizza form creating code ninjas... this form is brought to you by pizza ninjas at DIC... want pepperoni? Want mushrooms? Call 1-800-EAT-DICC for delivery code ninjas style!" :snap:
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month