|
|
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.
|
|

03-30-2008, 03:56 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Blackpool
Posts: 4
|
|
|
[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
|
|

03-30-2008, 11:36 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,146
|
|
|
Let's see your code.
|
|

03-31-2008, 01:11 AM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Blackpool
Posts: 4
|
|
|
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);
}
});
}
|
|

03-31-2008, 03:07 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,146
|
|
|
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.
|
|

03-31-2008, 12:04 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Blackpool
Posts: 4
|
|
Here it is again, sorry didnt realise that it needed to be in the [code] tags
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);
}
});
}
|
|

03-31-2008, 05:11 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,146
|
|
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);
}
});
}
}
|
|

04-01-2008, 02:06 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Blackpool
Posts: 4
|
|
|
Thanks for that, now I continue with the program.
Again Thanks
|
|

04-01-2008, 02:23 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|