#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    How do you bring the user to separate interfaces using JMenu?


    Suppose there were two menu items. One that had individual components to draw a circle and the other interface separate components to draw a square.

    Here is what I want to create but I cant post a URL. Is it ok if a mod can put this picture for me:

    http: // imageshack.us/a/img163/3456/basicmenu.png

    I have been searching for some time to do this. All I have found was action Listeners that popped open a new window and double buffering is very inefficient if it exists.



    My question is: can you link me to a tutorial site that shows how to do this? Or to a forum link here?

    The Java/Oracle library only has instructions on how to create the JMenus and add actions for the current interface. How can I do this?
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,664
    Rep Power
    1958
    Have you looked at the different layouts in java?
    In your case, the CardLayout could be a good place to start.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    I have been searching for some time to do this. All I have found was action Listeners that popped open a new window and double buffering is very inefficient if it exists.
    double buffering does exist and triple buffering exists also, double buffering is very efficient and triple is even more, they are used for graphics and images
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    The irony is that I feel there should be websites specifically made to explain bit by bit on how to use this.. Cardlayout to me is something completely different tbh.

    Also I have some questions if anyone can answer:

    1. For every JMenu item interface can I have one method for it that includes all of the components and their action listeners?

    2. Why can I not put the JMenu components outside of the init ()? I want them to be public but that is not possible.

    3. How many action listener methods do I need? One for each interface- one total or one for each method?
  8. #5
  9. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,664
    Rep Power
    1958
    Can you how CardLayout is different? what kind of function/"effect" are you looking for?

    1. For every JMenu item interface can I have one method for it that includes all of the components and their action listeners?
    This page have an example where it use one method.
    You will then have to use the parameter e to see which component that triggered the event.


    2. Why can I not put the JMenu components outside of the init ()? I want them to be public but that is not possible.
    Can you show you code? Do you get any error message?
    you should be able to do something like this:
    Code:
    class myClass
    {
        public JMenu myJMenu;
    
        public init()
        {
           myJMenu = new JMenu();
        }
    }
    3. How many action listener methods do I need? One for each interface- one total or one for each method?
    You can have one method as specified in 1.
    It is also possible to create an (inner) event function for each object.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by MrFujin
    Can you how CardLayout is different? what kind of function/"effect" are you looking for?
    The picture I posted is what I intend to do for my program I want to create. As basic as that. Card Layout will be inefficient as it can only provide a certain amount of interfaces.

    I have an example where it uses one method.
    You will then have to use the parameter e to see which component that triggered the event.
    TBH I read that page over 3 times and tried to implement certain concepts onto my code. I couldn't as it doesn't really elaborate on JFrame which I think I should be using. Also it never specifies how to do what I want to do.. Which is clearing the screen for each option.

    Can you show you code?
    Nothing much to show other than a basic menu with 2 MenuItem options which lead to nowhere. I can but there is little point to.

    If you want to I will post it.
    You can have one method as specified in 1.
    It is also possible to create an (inner) event function for each object.
    I thought I will need ItemListeners as well as well as a dozen ActionListener methods.
  12. #7
  13. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,664
    Rep Power
    1958
    Generally, if you have any problems with the code, it will help if you post it and specified the issue. e.g. what is happening and what is the expectation.

    it can only provide a certain amount of interfaces
    As far as I understand, you want an interface with square and an interface with circle?
    With CardLayout, you will be able to create and switch between two layouts: one JPanel for the square and one JPanel for the circle.
    Can you be more detailed about what you want?

    You are right that you should use JFrame.
    Did you look at the completed source code?

    Maybe you can look the MenuDemo example to get some ideas/methods about how to differentiate the menu from each other.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    As far as I understand, you want an interface with square and an interface with circle? With CardLayout, you will be able to create and switch between two layouts: one JPanel for the square and one JPanel for the circle.
    Can you be more detailed about what you want?
    No- that is not what I am aiming to build. What I am aiming to do is a more elaborate program with multiple interfaces from multiple JMenu Items. I understand this may make you frustrated as this program works best with card layout but I feel its not plausible. If you want I can listen to your advise from card-layout. I wont mind. I just don't want to project any confusion towards you.

    The issue is I don't know where to start as I cant even grasp something as simple as this. I appreciate your help with the CardLayout but I don't know how I will be able to relate the knowledge learned to what I intend to do.



    You are right that you should use JFrame.
    Did you look at the completed source code?
    Here is what I have so far:

    Code:
    import java.awt.event.*;
    import javax.swing.*;
     
    public class MainMPP extends JFrame implements ActionListener {
     
     
        public MainMPP() {
     
            super("PROGRAM"); // 'super' - Explicitly calls the parent constructor method
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
     
            
            
            // Name the JMenu & Add Items
            JMenu menu = new JMenu("File");
            menu.add(makeMenuItem("Open"));
            menu.add(makeMenuItem("Save"));
            menu.add(makeMenuItem("Quit"));
            
            JMenu menu2 = new JMenu("Drawing Shapes");
            menu2.add(makeMenuItem("Draw Circle"));
            menu2.add(makeMenuItem("Draw Square"));
            
            // Add JMenu bar
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(menu);
            menuBar.add(menu2);
            setJMenuBar(menuBar);
            setSize(300, 300);
            setLocation(200, 200);
            setVisible(true);
        }
     
        public void actionPerformed(ActionEvent e) {
     
            // Menu item actions
            String command = e.getActionCommand();
     
            if (command.equals("Quit")) {
                System.exit(0);
            } else if (command.equals("Open")) {         
                System.out.println("Open menu item clicked");
                
                textField = new JTextField(20);
                
                
                
            } else if (command.equals("Save")) {
                // Save menu item action
                System.out.println("Save menu item clicked");
            }
        }
     
        private JMenuItem makeMenuItem(String name) {
            JMenuItem m = new JMenuItem(name);
            m.addActionListener(this);
            return m;
        }
    
        public static void main(String[] s) {
            new MainMPP();
            
            MainMPP gui = new MainMPP ();
            
            
        }
    
    }
    I have tried to implement methods for each menu item but its fruitless as each of those methods need their own set of Paint, init and ActionListener methods.



    Maybe you can look the MenuDemo example to get some ideas/methods about how to differentiate the menu from each other.
    I looked it over. Again no ActionListesters except for displaying some text over the default GUI. But thank-you for the suggestion
  16. #9
  17. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,664
    Rep Power
    1958
    I am not frustrated, I just have to remember how it was to be a beginner.

    Can see you more or less have the menu in place.
    The code you have now, does it do what you expect it to do? (besides the missing things)
    The code has several print statements, are those lines being printed out?

    When you are working with GUI, it will be very useful (necessary?) if you read about how to use JPanel and the different layout managers (if you haven't done it already)

    As written in previous post, my idea was that you have one JPanl for each "function". So instead of "clearing the screen", you are changing the JPanels.

    Time must tell if CardLaout will be a plausible layout to use.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    I started working on JTabbed Pane and now I am officially getting the hang of it. It works similar to the menu. Since I figured out a way how to use null layout it is more flexible.

    Maybe someday I will learn how to add a picture onto the null layout using JFrame

IMN logo majestic logo threadwatch logo seochat tools logo