File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sreedhar sharma
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Laukik N. Desai
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
============================================
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laukik,
Please use Code Tags
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic