Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to open new windows after pressing button next?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to open new windows after pressing button next?

    In that rar file below is my sketch ouput that i want to make. my question is, how to create/connect to a new windows/frame after pressing button next? this is my unfinish coding for first frame, what should i add for btnNext method?


    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class GoKart extends JFrame implements ActionListener
     {
        private JLabel lblName, lblIC, lblGen, lblPn, lblMail;
        private JTextField txtName, txtIC, txtGen, txtPn, txtMail;
        private JButton btnReset, btnNext;
        private JPanel pnlA, pnlB, pnlPersonalDetails;
        private Container cont;
        private GridLayout layout;
     
        public GoKart()
        {
            //set the title
            super ("Go-Kart Booking Online Form");
            layout = new GridLayout(6, 2);
     
            cont = getContentPane();
            cont.setLayout(layout);
     
            //set label
            lblName = new JLabel ("Name: ");
            lblIC = new JLabel ("IC: ");
            lblMail = new JLabel ("Email: ");
            lblGen = new JLabel ("Gender: ");
            lblPn = new JLabel ("Phone Number: ");
     
            //set textfield
            txtName = new JTextField(10);
            txtIC = new JTextField(10);
            txtMail = new JTextField(10);
            txtPn = new JTextField(10);
     
            //set button
            btnReset = new JButton("Reset");
            btnNext = new JButton("Next");
     
            //set panel
            pnlA = new JPanel();
            pnlA.setLayout(new GridLayout(5, 2));
     
            pnlB = new JPanel();
            pnlB.setLayout(new GridLayout(1,2));
     
            pnlPersonalDetails = new JPanel();
            pnlPersonalDetails.setLayout(new GridLayout(1,0));
            pnlPersonalDetials.setBorder(new TitledBorder(new EtchedBorder(),"Personal Details"));
     
            //add to JPanel
            pnlA.add(lblName);
            pnlA.add(txtName);
            pnlA.add(lblIC);
            pnlA.add(txtIC);
            pnlA.add(lblGen);
            pnlA.add(txtGen);
            pnlA.add(lblPn);
            pnlA.add(txtPn);
            pnlA.add(lblMail);
            pnlA.add(txtMail);
     
            pnlB.add(btnReset);
            pnlB.add(btnNext);
     
            pnlPersonalDetails.add(pnlA);
     
            //register listener
            btnReset.addActionListener (this);
            btnNext.addActionListener (this);
     
            //add to Container
            cont.add(pnlPersonalDetails, BorderLayout.NORTH);
            cont.add(pnlB, BorderLayout.SOUTH);
     
            setSize (500, 250); 
            setVisible (true); 
            setDefaultCloseOperation (EXIT_ON_CLOSE);
        }
     
        public void actionPerformed (ActionEvent e)
        {
           if(e.getSource() = btnReset)
           {
               txtName.setText("");
               txtIC.setText("");
               txtGen.setText("");
               txtPn.setText("");
               txtMail.setText("");
           }
           else if(e.getSource() = btnNext)
           {
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to open new windows after pressing button next?

    how to create/connect to a new windows/frame after pressing button next
    Do you want to create an instance of a new class in the button's listener method? Use the new statement with a constructor for the class to be created.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to open new windows after pressing button next?

    errr how?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to open new windows after pressing button next?

    Are you asking how to use the new statement to create an instance of a class?
    The posted code is full of usages of the new statement. For example:
     lblName = new JLabel ("Name: ");  //  create instance of JLabel class

    Could you explain what your problem is?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to open new windows after pressing button next?

    i already declare jbutton for btnNext.i dont know what should i write to connect a new frame when i press button next. whats code should i write to connect those frame? please have a look that rar file.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to open new windows after pressing button next?

    Please post all code that is part of the problem on the forum. No links.

    whats code should i write to connect those frame
    Create an instance of the frame and use the reference to that instance to call its methods.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: How to open new windows after pressing button next?

    Can you post the contents of the .rar because many people (myself included) will not open a .rar for security reasons. You can use only the 1 JFrame you have and place the contents of the other window in a JOptionPane or a JDialog. I tend to prefer this design because it's simpler, don't need to juggle JFrames and JOptionPane forces the user to interact in it (i.e. they cannot get focus by trying to click outside of it). The JFrame would initially have its contents set to invisible and once the user enters something correct or does something in the JOptionPane or JDialog, verify it and make the JFrame contents visible. You can make them visible using the method .setVisible(Boolean : boolean), where true makes it visible, false makes it invisible.

    Alternatively, you can have multiple JFrames by creating instances of each and calling upon their references to get the methods as Norm mentioned.

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: How to open new windows after pressing button next?

    if(e.getSource() == btnNxt). Use "==" here instead of "=" otherwise it won't work.

    it should be if(e.getSource() == btnNxt)
    {
    Car mycar = new Car() //This is the new window that you want to open. it may be anyclass.
    }

  9. The Following User Says Thank You to myjava For This Useful Post:

    ThePrince (April 20th, 2013)

Similar Threads

  1. Replies: 1
    Last Post: January 31st, 2013, 04:55 AM
  2. Replies: 1
    Last Post: February 10th, 2012, 10:05 AM
  3. Open JFrame Form With Button
    By cardamis in forum AWT / Java Swing
    Replies: 2
    Last Post: April 12th, 2011, 03:18 AM
  4. [SOLVED] Click button to open JDialog
    By susieferrari in forum Java Theory & Questions
    Replies: 6
    Last Post: March 30th, 2011, 09:24 AM
  5. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM