PDA

View Full Version : How to call a j-frame from a main frame (frame to frame)


Inquisit
08-22-2008, 10:20 PM
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 :confused:
calling a j-frame (frame to frame)

Your help on this one

thanks

ess
08-28-2008, 02:28 PM
Here is an example which you can expand upon


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(450, 200);
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(200, 200);
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(200, 200);
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

Inquisit
08-28-2008, 09:16 PM
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