Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2008, 03:56 PM
stevedb's Avatar
Member
 
Join Date: Mar 2008
Location: Blackpool
Posts: 4
stevedb is on a distinguished road
[SOLVED] Navigation between swing forms
Hi all,

I am using Netbeans version 6 to create a tutorial application and I cannot seem to find a way to use a button to open up a different form.

I have created the forms using JFrames with a Main Menu, then 2 other menu forms that I want to have the user press a button to take them to one of the 2 other menu forms.

I know that I need to use Action Listeners but it is the code to open the form when the button is pressed which is eluding me.

Thanks in advance
Steve
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-30-2008, 11:36 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Let's see your code.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-31-2008, 01:11 AM
stevedb's Avatar
Member
 
Join Date: Mar 2008
Location: Blackpool
Posts: 4
stevedb is on a distinguished road
Here it is for the Main Menu Form, the other forms are called 'UseCaseTutorialMenu.java' and 'ActivityDiagramTutorialMenu.java':

public class UMLMenu extends javax.swing.JDialog {

/** Creates new form UMLMenu */
public UMLMenu(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

javax.swing.JFrame jFrame1 = new javax.swing.JFrame();
javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
javax.swing.JButton exitButton = new javax.swing.JButton();
javax.swing.JButton useCaseTutorialMenuButton = new javax.swing.JButton();
javax.swing.JButton ActDiaTutMenuButton = new javax.swing.JButton();
javax.swing.JPanel jPanel1 = new javax.swing.JPanel();

javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Aliens", 0, 24));
jLabel1.setLabelFor(this);
jLabel1.setText("UML Diagram Tutorials");

exitButton.setFont(new java.awt.Font("Tahoma", 1, 12));
exitButton.setText("EXIT");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});

useCaseTutorialMenuButton.setFont(new java.awt.Font("Tahoma", 1, 12));
useCaseTutorialMenuButton.setText("Use Case Diagram Tutorial and Quiz");
useCaseTutorialMenuButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
useCaseTutorialMenuButtonActionPerformed(evt);
}
});

ActDiaTutMenuButton.setFont(new java.awt.Font("Tahoma", 1, 12));
ActDiaTutMenuButton.setText("Activity Diagram Tutorial and Quiz");
ActDiaTutMenuButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ActDiaTutMenuButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(197, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addComponent(exitButton)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(126, 126, 126))))
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(useCaseTutorialMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 120, Short.MAX_VALUE)
.addComponent(ActDiaTutMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(48, 48, 48))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 73, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(57, 57, 57)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(useCaseTutorialMenuButton, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
.addComponent(ActDiaTutMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(60, 60, 60)
.addComponent(exitButton)
.addContainerGap())
);

pack();
}// </editor-fold>

private void exitButtonActionPerformed(java.awt.event.ActionEve nt evt) {
System.exit(0); // TODO add your handling code here:
}

private void useCaseTutorialMenuButtonActionPerformed(java.awt. event.ActionEvent evt) {
// TODO add your handling code here:
}

private void ActDiaTutMenuButtonActionPerformed(java.awt.event. ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
UMLMenu dialog = new UMLMenu(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-31-2008, 03:07 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
I'm unable to compile your file. The forum software can mangle code that is posted outside the [code] [/code] tags. I have no patience for dealing with the GroupLayout machine code. If you can edit your post and post formatted code I'll try again.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-31-2008, 12:04 PM
stevedb's Avatar
Member
 
Join Date: Mar 2008
Location: Blackpool
Posts: 4
stevedb is on a distinguished road
Here it is again, sorry didnt realise that it needed to be in the [code] tags

Code:
public class UMLMenu extends javax.swing.JDialog { /** Creates new form UMLMenu */ public UMLMenu(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { javax.swing.JFrame jFrame1 = new javax.swing.JFrame(); javax.swing.JLabel jLabel1 = new javax.swing.JLabel(); javax.swing.JButton exitButton = new javax.swing.JButton(); javax.swing.JButton useCaseTutorialMenuButton = new javax.swing.JButton(); javax.swing.JButton ActDiaTutMenuButton = new javax.swing.JButton(); javax.swing.JPanel jPanel1 = new javax.swing.JPanel(); javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane()); jFrame1.getContentPane().setLayout(jFrame1Layout); jFrame1Layout.setHorizontalGroup( jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); jFrame1Layout.setVerticalGroup( jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Aliens", 0, 24)); jLabel1.setLabelFor(this); jLabel1.setText("UML Diagram Tutorials"); exitButton.setFont(new java.awt.Font("Tahoma", 1, 12)); exitButton.setText("EXIT"); exitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitButtonActionPerformed(evt); } }); useCaseTutorialMenuButton.setFont(new java.awt.Font("Tahoma", 1, 12)); useCaseTutorialMenuButton.setText("Use Case Diagram Tutorial and Quiz"); useCaseTutorialMenuButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { useCaseTutorialMenuButtonActionPerformed(evt); } }); ActDiaTutMenuButton.setFont(new java.awt.Font("Tahoma", 1, 12)); ActDiaTutMenuButton.setText("Activity Diagram Tutorial and Quiz"); ActDiaTutMenuButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ActDiaTutMenuButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(197, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(exitButton) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(126, 126, 126)))) .addGroup(layout.createSequentialGroup() .addGap(41, 41, 41) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addComponent(useCaseTutorialMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE) .addComponent(ActDiaTutMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(48, 48, 48)))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(19, 19, 19) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(57, 57, 57) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(useCaseTutorialMenuButton, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE) .addComponent(ActDiaTutMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(60, 60, 60) .addComponent(exitButton) .addContainerGap()) ); pack(); }// </editor-fold> private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); // TODO add your handling code here: } private void useCaseTutorialMenuButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void ActDiaTutMenuButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { UMLMenu dialog = new UMLMenu(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-31-2008, 05:11 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class UMLMenu2 extends JDialog { public UMLMenu2(Frame parent, boolean modal) { super(parent, modal); initComponents(); } private void initComponents() { JLabel jLabel1 = new JLabel(); JButton exitButton = new JButton(); JButton useCaseTutorialMenuButton = new JButton(); JButton ActDiaTutMenuButton = new JButton(); JPanel jPanel1 = new JPanel(); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); jLabel1.setFont(new Font("Aliens", 0, 24)); jLabel1.setLabelFor(this); jLabel1.setText("UML Diagram Tutorials"); exitButton.setFont(new Font("Tahoma", 1, 12)); exitButton.setText("EXIT"); exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); useCaseTutorialMenuButton.setFont(new Font("Tahoma", 1, 12)); useCaseTutorialMenuButton.setText("Use Case Diagram Tutorial and Quiz"); useCaseTutorialMenuButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { String s = "This is where you launch your next " + "gui with components for user interaction."; String title = "Use Case Diagram Tutorial and Quiz"; JOptionPane.showMessageDialog(UMLMenu2.this, s, title, JOptionPane.PLAIN_MESSAGE); } }); ActDiaTutMenuButton.setFont(new Font("Tahoma", 1, 12)); ActDiaTutMenuButton.setText("Activity Diagram Tutorial and Quiz"); ActDiaTutMenuButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { String s = "This is where you launch your next " + "gui with components for user interaction."; String title = "Activity Diagram Tutorial and Quiz"; JOptionPane.showMessageDialog(UMLMenu2.this, s, title, JOptionPane.PLAIN_MESSAGE); } }); jPanel1.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.anchor = GridBagConstraints.NORTH; gbc.gridwidth = 2; jPanel1.add(jLabel1, gbc); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 1; gbc.gridy = 1; gbc.anchor = GridBagConstraints.SOUTH; jPanel1.add(useCaseTutorialMenuButton, gbc); jPanel1.add(ActDiaTutMenuButton, gbc); gbc.gridy++; gbc.anchor = GridBagConstraints.SOUTHEAST; gbc.gridwidth = 2; jPanel1.add(exitButton, gbc); add(jPanel1, BorderLayout.CENTER); pack(); } public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { UMLMenu2 dialog = new UMLMenu2(new JFrame(), true); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-01-2008, 02:06 PM
stevedb's Avatar
Member
 
Join Date: Mar 2008
Location: Blackpool
Posts: 4
stevedb is on a distinguished road
Thanks for that, now I continue with the program.

Again Thanks
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-01-2008, 02:23 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
stevedb, please click Thread Tools > 'Mark Thread as Solved' to let other members know this problem has been resolved. Thanks!
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Login Forms dankonsoer New To Java 1 08-07-2008 01:06 PM
Use multiple forms coco Java Applets 1 08-03-2007 11:56 PM
Wep page navigation buttons anthony417 Advanced Java 1 07-26-2007 09:40 PM
Forms in jsp bbq JavaServer Pages (JSP) and JSTL 1 07-05-2007 05:28 AM
Migration from oracle forms to java Nick15 Advanced Java 3 05-12-2007 08:00 AM


All times are GMT +3. The time now is 07:45 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org