File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes i created 2 frames, how to call another  frame from the first. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "i created 2 frames, how to call another  frame from the first." Watch "i created 2 frames, how to call another  frame from the first." New topic
Author

i created 2 frames, how to call another frame from the first.

sreedhar sharma
Greenhorn

Joined: Nov 19, 2008
Posts: 1
in a swing application i created 2 frames.in this two frames 1 frame has 3 buttons
ok,cancel,new buttons.My task is that when i click on the ok button it has to open the second frame .Can you please advise me on the code for the button.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Originally posted by sreedhar sharma:
in a swing application i created 2 frames.in this two frames 1 frame has 3 buttons
ok,cancel,new buttons.My task is that when i click on the ok button it has to open the second frame .Can you please advise me on the code for the button.


Add an action listener to the OK button.
In the actionPerformed() method create an instance of the second frame and make it visible.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Laukik N. Desai
Greenhorn

Joined: Aug 03, 2007
Posts: 3
============================================
FirstFrame.java

============================================
package ranch.twoframes;

import javax.swing.JFrame;

public class FirstFrame extends javax.swing.JFrame {

/** Creates new form FirstFrame */
public FirstFrame() {
initComponents();
}


private void initComponents() {

okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
newButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

okButton.setText("Ok");
okButton.setName("okButton"); // NOI18N
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});

cancelButton.setText("Cancel");
cancelButton.setName("cancelButton"); // NOI18N

newButton.setText("New");
newButton.setName("newButton"); // NOI18N

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(20, 20, 20)
.add(okButton)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cancelButton)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(newButton)
.addContainerGap(74, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(26, 26, 26)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(okButton)
.add(cancelButton)
.add(newButton))
.addContainerGap(48, Short.MAX_VALUE))
);

pack();
}

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {

if (evt.getActionCommand().equalsIgnoreCase("OK")) {
JFrame secondFrame = ranch.twoframes.SecondFrame.getInstance();
secondFrame.setVisible(true);
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

JFrame firstFrame = new FirstFrame();
firstFrame.setVisible(true);
firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}


private javax.swing.JButton cancelButton;
private javax.swing.JButton newButton;
private javax.swing.JButton okButton;

}

============================================
SecondFrame.java

============================================

package ranch.twoframes;

public class SecondFrame extends javax.swing.JFrame {

private static final SecondFrame secondFrame = new SecondFrame();
/** Creates new form SEcondFrame */
private SecondFrame() {
initComponents();
}

public static SecondFrame getInstance()
{
return secondFrame;
}

private void initComponents() {

sampleLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

sampleLabel.setText("Second Frame");
sampleLabel.setName("sampleLabel"); // NOI18N

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(33, 33, 33)
.add(sampleLabel)
.addContainerGap(299, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(sampleLabel)
.addContainerGap(275, Short.MAX_VALUE))
);

pack();
}




private javax.swing.JLabel sampleLabel;


}
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Laukik,
Please use Code Tags
 
Don't get me started about those stupid light bulbs.
 
subject: i created 2 frames, how to call another frame from the first.