-2

Here is the code I use but nothing I am doing it with netbeans;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    new NewJFrame().setVisible(true);       // to open a new frame
    Menu.setVisible(false);  //not working
    Menu.dispose();// not working
    Menu().setVisible(false); //not working
    new Menu().setVisible(true); // this don't give me error but nothing happens if I press the button only open the new frame
    // I tried all of these with "this." but nothing
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
manuel.koliqi
  • 313
  • 1
  • 3
  • 11
  • So, what's `Menu`? Another JFrame? Have you overridden any of `Menu`'s methods like `dispose`? – MadProgrammer Aug 09 '12 at 23:55
  • yeah Menu is an another JFrame, what does it mean override? – manuel.koliqi Aug 10 '12 at 00:08
  • 3
    Tossing out several JFrames at the user may not be the best program design and can annoy the user. The best answer for your question may be to suggest that you don't consider doing this but rather design your application in a more user-friendly way. Consider instead swapping views with a CardLayout for instance. – Hovercraft Full Of Eels Aug 10 '12 at 00:26
  • That is a idea but there will be a lot of more work – manuel.koliqi Aug 10 '12 at 00:33
  • @HovercraftFullOfEels Instead of swapping views with CardLayout, consider: http://goo.gl/SDHvX – rtheunissen Aug 10 '12 at 00:54
  • To expand on @Hovercraft - See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) *"That is a idea but there will be a lot of more work"* If you are suggesting that `CardLayout` (or any of the plethora of alternatives) will be more work than managing multiple frames, that suggests you have no idea how much work it will take to get multiple frames working in the first place. Most importantly, in regard to other comments, stop letting the IDE make you its bitch. It should not matter if you are doing this with Netbeans, Eclipse or pencil and paper. – Andrew Thompson Aug 10 '12 at 02:46
  • 1
    @paranoid-android I do not see the advantage of the class you refer to compared with a `CardLayout`. Whether you call the `switch` on that `ViewSwitcher` class or on a `CardLayout` comes down to the same thing. Or am I missing something – Robin Aug 10 '12 at 08:42
  • @Robin Well it's simply a matter of how you can extend it if you ever need to. But sure, anything you can do with the ViewSwitcher I referenced you can do CardLayout too. I just like having full control over all my code and it doesn't come with all the extra baggage of `CardLayout`. It's a small class with a clear function that's easy to use and adapt. – rtheunissen Aug 10 '12 at 12:12

1 Answers1

3

Works fine for me

public class ParentFrame extends JFrame {

    private JButton btnBirth;

    public ParentFrame() {

        setTitle("Parent");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        setLayout(new GridBagLayout());

        setSize(200, 200);
        setLocationByPlatform(true);

        btnBirth = new JButton("Spawn");
        btnBirth.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                setVisible(false);
                ChildFrame child = new ChildFrame(ParentFrame.this);
                child.setVisible(true);

            }

        });

        add(btnBirth);

    }

}

public class ChildFrame extends JFrame {

    private JLabel lblGoo;
    private JFrame parent;

    public ChildFrame(JFrame parent) {

        this.parent = parent;

        setTitle("Child");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new GridBagLayout());

        setSize(100, 100);
        setLocationByPlatform(true);

        lblGoo = new JLabel("Goo");
        add(lblGoo);

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {

                ChildFrame.this.parent.setVisible(true);

            }

        });

    }

}

UPDATED with Netbeans form editor

public class ParentFrame extends javax.swing.JFrame {

    /**
     * Creates new form ParentFrame
     */
    public ParentFrame() {

        initComponents();

        setSize(200, 200);

    }

    /**
     * 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() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Parent");
    setLocationByPlatform(true);
    setPreferredSize(new java.awt.Dimension(200, 200));
    getContentPane().setLayout(new java.awt.GridBagLayout());

    jButton1.setText("Spawn");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });
    getContentPane().add(jButton1, new java.awt.GridBagConstraints());

    pack();
  }// </editor-fold>

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

        setVisible(false);
        new ChildFrame(this).setVisible(true);

  }

  // Variables declaration - do not modify
  private javax.swing.JButton jButton1;
  // End of variables declaration
}


public class ChildFrame extends javax.swing.JFrame {

private JFrame parentFrame;

/**
 * Creates new form ChildFrame
 */
public ChildFrame() {

    initComponents();

    setSize(100, 100);

}

public ChildFrame(JFrame frame) {

    this();

    parentFrame = frame;

}

/**
 * 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() {

jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Child");
setLocationByPlatform(true);
setPreferredSize(new java.awt.Dimension(100, 100));
addWindowListener(new java.awt.event.WindowAdapter() {
  public void windowClosing(java.awt.event.WindowEvent evt) {
    doWindowClosing(evt);
  }
});
getContentPane().setLayout(new java.awt.GridBagLayout());

jLabel1.setText("Goo");
getContentPane().add(jLabel1, new java.awt.GridBagConstraints());

pack();
}// </editor-fold>

private void doWindowClosing(java.awt.event.WindowEvent evt) {                                 

    setVisible(false);
    parentFrame.setVisible(true);

}                                

// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • you have done this with "Eclipse" but I want it with "Netbeans". I haven't tried with eclipse because I don' t have it now. – manuel.koliqi Aug 10 '12 at 00:27
  • 3
    **NO** - I did this, by hand, in Netbeans. This will work even if I had done it in NotePad, it will make **NO** difference what so ever. Try and understand what this code is doing and find the errors in your code. – MadProgrammer Aug 10 '12 at 00:31
  • Try to do it with JFrame, Create 2 JFrames (not by hand) click with the right button of mouse-->New--> JFrames Form, put in a button an do the code I did. Than tell me – manuel.koliqi Aug 10 '12 at 00:35
  • I will study the code tomorrow because now I am going to sleep. Good Night – manuel.koliqi Aug 10 '12 at 00:42
  • @manuel.koliqi Updated with netbeans form editor and it still works ;) – MadProgrammer Aug 10 '12 at 00:50
  • You probably have the `deafultCloseOperation` set to `DO_NOTHING` – MadProgrammer Aug 10 '12 at 00:51
  • this way is road to the hell, not good idea to playing with two JFrames, use JDialog instead, basically question only about CardLayout, sorry brrrrr – mKorbel Aug 10 '12 at 08:11
  • @mKorbel Agree, but I'm just trying to answer the question, without context, it can difficult to make those assumptions, but I also agree that throwing frames around like this is just asking for trouble – MadProgrammer Aug 10 '12 at 08:20
  • Thank you MadProgrammer, It works fine with the second example the "deafultCloseOperation" had no relevance. My brain last evening was blowing about this. Yes I know that there are hundred of methods to do that but I want to learn. – manuel.koliqi Aug 10 '12 at 12:47
  • @manuel.koliqi glad we cold resolve it. I'd suggest if you're willing to take the time, avoid the form editor for a while. It's great & I love it, but you really need to understand what's goining under the hood. It will make solving problems like this a little simper ;) – MadProgrammer Aug 10 '12 at 16:59