Reputation: 1529
In my class file, I am making a GUI appear, and what I am trying to accomplish is when you hit the finish button, a different GUI (that I made in another class) appears, while the old one closes.
This is the GUI I want to appear -
package my.contacteditor2;
import java.util.ArrayList;
/**
*
* @author Prox
*/
public class ContactEditorUI_1 extends javax.swing.JFrame {
String disease1;
String disease2;
String disease3;
String disease4;
String disease5;
int ChromeNum=0;
String Chrome;
ArrayList<String> diseases = new ArrayList<String>();
/**
* Creates new form ContactEditorUI
*/
public ContactEditorUI_1() {
initComponents();
setLocationRelativeTo(null);
}
public String resultsDisplayed (String [] disease, int number, boolean [] haveDisease) {
return Chrome;
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton2.setText("Exit");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Trebuchet MS", 0, 36)); // NOI18N
jLabel1.setText("Results");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 870, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addContainerGap(25, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/**/
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ContactEditorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ContactEditorUI_1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
In my original class, I cant figure out how to call it successfully so that the GUI appears. I have tried making run its own method, and calling it in the other class with ContactEditorUI_1.run();
.
I have also tried ContactEditorUI_1.ContactEditorUI_1()
(the main constructor).
I'm sure there is a simple way to do this, but I'm having some trouble finding it. Does anyone have any good ideas?
Upvotes: 1
Views: 1380
Reputation: 285405
It's usually not much fun for the user to be barraged by a succession of new windows, and most applications when faced with this problem show just one window, but swap views in that window, for instance a game that first shows set up views and then the main game, all in one window. Another option is to show one main window, and then if you need to gain information from the user in a modal fashion -- in a way where the program flow must stop until the information is returned -- then using a modal dialog such as a modal JDialog or JOptionPane.
The key to any and all of this (in my opinions), whether you go with either of the two approaches above, or even if you want to show a succession of main windows, is to gear your Swing GUI programs towards creating JPanels not JFrames. If you do this, then you can place your JPanel whenever needed wherever you want, be it in its own JFrame, in a dialog, swapped in a CardLayout (the view swapping mentioned above), as a tab in a JTabbedPane or even in a JOptionPane.
Upvotes: 4