1

I am using Netbeans 7.3 Beta 2 GUI builder. Coding a Swing GUI and I am trying to show another GUI once a button has been pressed.

private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) {
    new LoginGUI();
}

Once login button is pressed I want to show the LoginGUI but it does nothing and Netbeans states "New instance ignored". I've added a system.out.println on the button to make sure it works fine and it does.

Here is more or less the full source. Where am I going wrong? I want to replace this GUI with the login GUI.

package com.john.spp.view;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author John
 */
public class LogRegGUI extends javax.swing.JFrame {

/**
 * Creates new form LoginGUI
 */
public LogRegGUI() {
    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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
   //GUI BUILDER CODE HERE
}// </editor-fold>

private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) {
    new LoginGUI();
}

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

/**
 * @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(LogRegGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(LogRegGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(LogRegGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(LogRegGUI.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 LogRegGUI().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton dataVaultButton;
private javax.swing.JButton helpButton;
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton loginButton;
private javax.swing.JButton logoutButton;
private javax.swing.JButton registerButton;
private javax.swing.JButton settingsButton;
// End of variables declaration
}

Thanks.

EDIT:

Fixed it by using this code, but it looks quite bad as it flashes quite quickly and re-draws the new GUI, any ideas on how to stop this?

private void loginButtonSceneActionPerformed(java.awt.event.ActionEvent evt) { 
    LoginGUI itemloader=new LoginGUI();
    itemloader.setVisible(true);
    this.setVisible(false);
} 
John Vasiliou
  • 977
  • 7
  • 27
  • 48
  • we need to see the code of `LoginGUI` what does the constructor? does it show the constructed frame? maybe you how to change that line to `new LoginGUI().setVisible(true);` – Zhedar Feb 03 '13 at 12:03
  • did you thought about using just one window? and just reuse it via `JDesktopPane`, `CardLayout` or `JTabbedPane`? – Zhedar Feb 03 '13 at 12:06
  • If I use the code you gave me, it still works, but it does not remove the old window. – John Vasiliou Feb 03 '13 at 12:06
  • your code does the right thing, but you should test if switching `itemloader.setVisible(true); this.setVisible(false);` to `this.setVisible(false); itemloader.setVisible(true);` does the trick. first fade, then show new window. otherwise you could think about not using multiple frames. – Zhedar Feb 03 '13 at 12:09
  • 1) Don't extend `JFrame`. I have yet to see a case where it makes sense to do so. Jut create an instance and add the GUI to it. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Feb 03 '13 at 12:27
  • @AndrewThompson already linked your answer since I quite like it. maybe I should label it better :D – Zhedar Feb 03 '13 at 12:29
  • 1
    @Zhedar Ehh.. (shrugs) already up-voted your answer. After equivocating - dithering - for some minutes as to whether to vote you up for the other question and the 'Edit 2' which I felt was counter-productive for newbies. I decided 'yes' to the up-vote and 'buyer beware' to the user, since your answer does add warnings against the alternatives. :) – Andrew Thompson Feb 03 '13 at 12:35

1 Answers1

2

There're several causes why using multiple frames isn't the best solution for applications. Well, it often leaves a confused user and as you see brings several complications.
As I already suggested at the comments you could let you guide by my other answer yesterday by constructing all your JFrames before you show them. If you then set them visible, there won't be so much delay as it is now and also you save some memory and CPU time.
Another way may be to just use one single frame and change it's contents. This answer has really grown to my favorite in referencing ;)
This way there's also the possibility to construct first and then instantly show what you've already build. No delay, no flickering.

Community
  • 1
  • 1
Zhedar
  • 3,480
  • 1
  • 21
  • 44
  • I am using Netbeans GUI builder. Going by Java's tutorial on their website, for CardLayout, it seems the GUI must be coded instead of having the ability to use a GUI builder to create it. Can you confirm this? – John Vasiliou Feb 03 '13 at 12:43
  • Well, as I'm not using any GUI builder I can not confirm or reject that. But if you still want to use it, maybe you could try to create 2 `JPanel`s with NetBeans and then insert them into a single `CardLayout` by hand. Not the best solution but may be a workaround if you want to stay with your builder. – Zhedar Feb 03 '13 at 12:47
  • No worries - I was advised to not waste time coding a GUI myself. It seems that it may be the best solution after all. Thanks for your help, looking into CardLayout as we speak. – John Vasiliou Feb 03 '13 at 12:49
  • 1
    http://vincentramdhanie.blogspot.de/2009/11/cardlayout-with-netbeans.html that may be interesting for you as it looks quite easy. – Zhedar Feb 03 '13 at 12:51