Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,817 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,686 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

RETNE

4 Oct, 2008 - 11:04 AM
Post #1

New D.I.C Head
*

Joined: 17 Nov, 2007
Posts: 29


My Contributions
just want to know how to open a second form when a button is pressed

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Have More Then 1 Form

4 Oct, 2008 - 11:50 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,305



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

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

java

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!" decap.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:12PM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month