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

Thread: Switching between frames from a seperate class

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switching between frames from a seperate class

    I have a generic class which creates a menubar

    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);
        }
    }

    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);
     
      }

    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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Switching between frames from a seperate class

    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)

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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
    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)

  5. #5
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switching between frames from a seperate class

    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

Similar Threads

  1. swing - switching JPanels
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 5th, 2010, 09:18 AM
  2. GIF frames
    By vsector in forum AWT / Java Swing
    Replies: 0
    Last Post: April 15th, 2010, 05:25 PM
  3. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  4. Is it worth switching from PHP to JSP
    By mydarkpassenger in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: April 3rd, 2010, 02:23 AM
  5. Replies: 1
    Last Post: March 11th, 2009, 04:41 PM