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 2 of 2

Thread: Java Applet program to create different screen and link them together

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java Applet program to create different screen and link them together

    Hi,

    I'm very new to java, and currently learning some of the basics.

    What i need help on is how to program in java so that i can change from one window to another. Not really sure how to word this but here goes:

    For example, when i start the java applet, a login screen (window1) will appear, when a user enters username and password, when the user is verified, it "switches" to a different window (window2) to access the data.

    Any help much appreciated.

    Thanks in advance,
    -channi3


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Java Applet Switching To Different Windows?

    Hello channi3 and welcome to the forums.

    Take a look at this code example and compile it.

    There is a main JFrame with a button on it. When this button is clicked, JFrame1 closes and opens JFrame2.

    Its just a matter of invoking the run method for each form when an action is performed and setting the visibility of the previous form to false.

    import javax.swing.*;   
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Channi3 {
     
        public static JFrame frame1 = new JFrame("Java Frame1");
        public static JFrame frame2 = new JFrame("Java Frame2");
     
        //Form1
        private static void createAndShowGUI1()  {
     
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JButton button = new JButton("HI! IM JFRAME1 - CLICK ME TO OPEN FRAME 2");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    [B]frame1.setVisible(false);[/B]
                    System.out.println("Opening JFrame 2");
                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            [B]createAndShowGUI2();[/B]
     
                        }
                    });
                }
     
            });      
     
            frame1.getContentPane().add(button);
            frame1.pack();
            frame1.setVisible(true);
        }
     
     
        //Form2
        private static void createAndShowGUI2()  {
     
            frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JButton button = new JButton("HI! IM JFRAME2 - CLICK ME TO OPEN FRAME 1");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    [B]frame2.setVisible(false);[/B]
                    System.out.println("Opening JFrame 1");
                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            [B]createAndShowGUI1();[/B]
     
                        }
                    });
                }
     
            });      
     
            frame2.getContentPane().add(button);
            frame2.pack();
            frame2.setVisible(true);
        }
     
     
     
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI1();
                }
            });
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. servlet applet communication
    By prashanthi_asn in forum Java Servlet
    Replies: 0
    Last Post: May 21st, 2009, 12:50 AM
  2. Designing RLC Circuit using Java Applet
    By syxxpac316 in forum Java Applets
    Replies: 6
    Last Post: May 13th, 2009, 04:26 PM
  3. Problem in merging two java classes
    By madkris in forum Object Oriented Programming
    Replies: 11
    Last Post: March 16th, 2009, 09:02 AM
  4. FileNotFound error while working with XML files in windows
    By ochieng in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 14th, 2008, 06:56 AM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM