Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How to call a j-frame from a main frame (frame to frame)

    Question to the experts
    I use both Netbeans 5.5 and Forte for java 4

    how to call a j-frame from a main j-frame (frame to frame)



    i have created a main J-frame and 8 sub j-frames in one class (package)
    i find this as a conservative method and time efficient rather than individual java files for these are not internal frames but are regular frames

    ive tried some methods to solve this either i get an error the whole window dissapears or nothing happens

    in the Main J-frame there are buttons that you click to go to the next J-frame the same way "a-href" in HTML works in the same window and back

    Lets say my project is a Finance project


    i.e

    Main J-Frame *****Contents

    Menu:- ( TVM) ----------> button // opens TVM J-frame (J-Frame 2)

    (Compound interest)------> button //Opens Compound Interest j-frame 3
    etc

    when you click the ---------> (TVM) button
    it should open the TVM J-frame (J-frame in the same window) frame 2
    this also applies when you click----> (Compound interest) opens J-frame 3
    while the 2nd or 3rd frame is shown the main frame should be invisible
    either this.dispose or HIDE_ON_CLOSE

    while using action performed or mouse clicked to be assigned to a button
    (action listeners) you get a message

    i.e
    public void actionPerformed(ActionEvent e)

    //add your handling code here

    now what code is to be placed here in order to achieve this
    calling a j-frame (frame to frame)

    Your help on this one

    thanks

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    Here is an example which you can expand upon

    PHP Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class 
    OpenFrames {
        public static 
    void main(String[] args) {
            
    JFrame first = new JFrame("First Frame");
            
    first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
    first.getContentPane().setLayout(new FlowLayout());
            
    JButton b1 = new JButton("Open First");
            
    JButton b2 = new JButton("Open Second");
            
    first.getContentPane().add(b1);
            
    first.getContentPane().add(b2);
            
    b1.addActionListener(new ActionListener() {
                    public 
    void actionPerformed(ActionEvent e) {
                        new 
    SecondFrame();
            }});
            
    b2.addActionListener(new ActionListener() {
                    public 
    void actionPerformed(ActionEvent e) {
                        new 
    ThridFrame();
            }});
            
    first.setSize(450200);
            
    first.setVisible(true);
        } 
    //-- ends class method main
    //-- ends class definition

    class SecondFrame extends JFrame {
        public 
    SecondFrame() {
            
    setTitle("Second Frame");
            
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            
    getContentPane().setLayout(new FlowLayout());
            
    getContentPane().add(new JLabel("THIS IS THE 2nd JFRAME"));
            
    setSize(200200);
            
    setVisible(true);
        } 
    //-- ends constructor
        
    //-- ends SecondFrame

    class ThridFrame extends JFrame {
        public 
    ThridFrame() {
            
    setTitle("Third Frame");
            
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            
    getContentPane().setLayout(new FlowLayout());
            
    getContentPane().add(new JLabel("THIS IS THE 3rd JFRAME"));
            
    setSize(200200);
            
    setVisible(true);
        } 
    //-- ends constructor
    //-- ends firstFrame 
    Copy the above code and save in the file "OpenFrames.java"...then compile and run.

    Hope this helps

    Cheers
    ~E

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Bigg Thanks you just alleviated my stress in a therapeutic way
    i should work my way round this once again thanks

    i hope this works


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •