4 Replies - 255 Views - Last Post: 14 August 2019 - 06:37 PM Rate Topic: -----

#1 adgajax   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-August 19

Move from one jframe window to another

Posted 14 August 2019 - 01:51 AM

This is the code for the main window:
package windowchange;
import java.awt.event.ActionListener;
public class winOne extends javax.swing.JFrame {

    
    public winOne() {
        initComponents();        
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        winOneNextBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        winOneNextBtn.setText("Next");
        winOneNextBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                winOneNextBtnActionPerformed(evt);
            }
        });

        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(287, Short.MAX_VALUE)
                .addComponent(winOneNextBtn)
                .addGap(58, 58, 58))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(217, Short.MAX_VALUE)
                .addComponent(winOneNextBtn)
                .addGap(60, 60, 60))
        );

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

    private void winOneNextBtnActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        winTwo obj2 = new winTwo();
        obj2.setVisible(true);
    }                                             
    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(winOne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(winOne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(winOne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(winOne.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 winOne().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton winOneNextBtn;
}



and this is the code for the second window:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package windowchange;

/**
 *
 * @author Ajax
 */
public class winTwo extends javax.swing.JFrame {

    /**
     * Creates new form winTwo
     */
    public winTwo() {
        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() {

        winTwoBackBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        winTwoBackBtn.setText("Back");
        winTwoBackBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                winTwoBackBtnActionPerformed(evt);
            }
        });

        jLabel1.setText("Second page");

        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(winTwoBackBtn)
                .addGap(116, 116, 116))
            .addGroup(layout.createSequentialGroup()
                .addGap(130, 130, 130)
                .addComponent(jLabel1)
                .addContainerGap(208, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(116, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(70, 70, 70)
                .addComponent(winTwoBackBtn)
                .addGap(77, 77, 77))
        );

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

    private void winTwoBackBtnActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        //obj1.setVisible(false);
    }                                             

    /**
     * @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(winTwo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(winTwo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(winTwo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(winTwo.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 winTwo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton winTwoBackBtn;
    // End of variables declaration                   
    winOne obj1 = new winOne();
}



Now the above code works fine but I want to do some modification as such while the button from the main widow is pressed then the main window's visibility should be hidden and while pressing the back button from the second window the second one should be closed and the first one should return as previous condition.

Is This A Good Question/Topic? 0
  • +

Replies To: Move from one jframe window to another

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Move from one jframe window to another

Posted 14 August 2019 - 11:23 AM

What happens now when the code is executed?
Was This Post Helpful? 0
  • +
  • -

#3 chanjul   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 66
  • Joined: 11-September 18

Re: Move from one jframe window to another

Posted 14 August 2019 - 06:21 PM

I hope this will help, on your method just add this statement
winOneNextBtnActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        winTwo obj2 = new winTwo();
        obj2.setVisible(true);// make winTwo frame open
        winOne winone = new winOne();
        winone.setVisible(false);// make winOne frame hidden
    }    

and on your second frame which is winTwo :
winTwoBackBtnActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        //obj1.setVisible(false);
        winOne obj1 = new winOne();
        obj1.setVisible(true);//make winOne frame open
        winTwo obj2 = new winTwo();
        obj2.setVisibl(false);// make winTwo frame hidden
    }    

you can modify the code with your own variable
hope this help you and have a nice day.
Was This Post Helpful? 1
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Move from one jframe window to another

Posted 14 August 2019 - 06:28 PM

@chanjul Your code creates too many new instances. Why not use the existing ones?
Was This Post Helpful? 0
  • +
  • -

#5 chanjul   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 66
  • Joined: 11-September 18

Re: Move from one jframe window to another

Posted 14 August 2019 - 06:37 PM

View PostNormR, on 14 August 2019 - 06:28 PM, said:

@chanjul Your code creates too many new instances. Why not use the existing ones?

sorry if I make too many instances, but I say it on my comment that "you can modify the code", I just focus on two button next button on the winOne and back button on winTwo. because he said

Quote

Now the above code works fine but I want to do some modification as such while the button from the main widow is pressed then the main window's visibility should be hidden and while pressing the back button from the second window the second one should be closed and the first one should return as previous condition

This post has been edited by chanjul: 14 August 2019 - 06:38 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1