Switching between frames from a seperate class
I have a generic class which creates a menubar
Code :
public class CreateMenuBar {
JMenuBar MenuBar;
JMenu FileMenu;
JMenuItem AddAction;
ActionListener MyListener;
ServicesFrame ServicesFrame;
public JMenuBar CreateMenuBar() {
MenuBar = new JMenuBar();
FileMenu = new JMenu("File");
AddAction = new JMenuItem(Add);
MenuBar.add(FileMenu);
FileMenu.add(AddAction);
AddAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addService()
}
});
return MenuBar
}
public void addService()
{
//this.setisible(false); // Doesnt work because this is a generic class
if (ServicesFrame == null)
{
ServicesFrame = new ServicesFrame(MenuBar, null);
}
ServicesFrame.setVisible(true);
}
}
Code :
public class Main {
static JMenuBar MainMenuBar;
public static void main(String[] args) {
CreateMenuBar Menu = new CreateMenuBar();
MainMenuBar = Menu.AddItems();
ListServersFrame ListServersFrame = new ListServersFrame(MainMenuBar);
ListServersFrame.setVisible(true);
}
Code :
public class ListServersFrame extends JFrame implements ActionListener {
JMenuBar MainMenuBar;
public ListServersFrame(JMenuBar MainMenuBar) {
super();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 850);
this.setJMenuBar(MainMenuBar);
so i want to click on the menu and switch between frames ( the example above is just one menuitem but i have two which can switch back and forth)
Any ideas? i did try checking if an instance of the frame already exist, if not create one and hide it but i got errors.
Thanks for any ideas
Kurt
Re: Switching between frames from a seperate class
Quote:
if not create one and hide it but i got errors
What errors? Please post them in their entirety. If you just have 2 windows, create both and have action listeners on the menu items themselves which activates the particular JFrame. If you have more than this, you could add each window to a common List, which the Menu then shows (you can use a MenuListener to dynamically populate the menu)
Re: Switching between frames from a seperate class
i either get a nullpointer exception if i dont create a new instance of the frame or it creates more than one instance of the same frame. if it does this when i close one frame down it closes the rest!
what i want to do is
-create an instance of aframe (frame 1)
-hide this (frame 1) from a seperate class and show another frame (Frame 2)
-then close or dispose of the current frame (frame 2) and show the first frame (frame 1)
i have many frames in my application so the above was just sections of mine
I would like a dynamic menu which shows all of the available frames but Im unsure of how to do ths.
Any advice on how to achieve this?
i have a function which gets the current frame which i can hide or dispose
but showing the previous frame is the problem.
Thanks
Kurt
Re: Switching between frames from a seperate class
Quote:
i either get a nullpointer exception if i dont create a new instance of the frame or it creates more than one instance of the same frame
NullPointer makes sense in the first context, but you should just create one instance of each frame. Pass a reference of this JFrame to whatever you want to control it (the JMenu) and activate it upon an action (you could do so by creating a single ActionListener class which takes a JFrame reference, the upon actionPerformed calls JFrame.toFront.
As I mentioned above but will elaborate on, if you have many frames then you could keep references to each frame in a List (this could be a static list, or one that is passed to each new frame). In the JMenu, you can then add a MenuListener which upon menuSelected you can populate with the titles of all the JFrames in the List (together with an ActionListener that brings the window to the fron)
Re: Switching between frames from a seperate class
Quote:
Pass a reference of this JFrame to whatever you want to control it (the JMenu) and activate it upon an action (you could do so by creating a single ActionListener class which takes a JFrame reference, the upon actionPerformed calls JFrame.toFront.
The problems with this is that im passing a service to the constructor so when i create an instance of the frame from the menubar class i cant access the current service.
Can i create a global variable which is accessiable for all my classes?
the only other way i can think of is to add the service to my database and read it in from the new class, but this means many reads to and from the database.
Thanks
Kurt