Results 1 to 6 of 6
  1. #1
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default JMenuItems and actions

    Hi,

    Edit: Was wrong -> repost

    Greets
    Last edited by Kligham; 09-30-2009 at 07:02 PM.

  2. #2
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    Let me rephrase my question. The goal of my program is that you can sort like 'switch' from JPanel to JPanel by using the JMenu. So for example you have three options you can choose from your menu: 1) New Client, 2)New Pet, 3)New Recipe. And then based on what JMenuItem you choose; a specific JPanel appears in the JFrame. You also need to be able to rechose everytime you need to do something else. So the old panel should dissapear and the one you selected from the menu should appear. So how can make this possible with these three classes?

    My first class is the class which creates my JFrame, Main.java:

    Java Code:
    package test;
    
    import javax.swing.JFrame;
    
    public class Main{
    
        private static void createAndShowGUI(){
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // construct a Menu object...
            MenuGUI menu = new MenuGUI();
                // ...and obtain its menu bar
            frame.setJMenuBar(menu.getMenuBar());
    
            frame.pack();
            frame.setSize(500,500);
            frame.setVisible(true);
    
        }
    
        public static void main(String[] args) {
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    My second class is my MenuGUI class which makes my Menu:

    Java Code:
    package test;
    
    import java.awt.event.InputEvent;
    import java.awt.event.ActionEvent;
    
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.KeyStroke;
    import javax.swing.AbstractAction;
    
    public class MenuGUI {
    
        private JMenuBar menuBar;
    
            /** Constructs the menu bar. */
        public MenuGUI() {
    
            menuBar = new JMenuBar();
    
            JMenu consultatie = new JMenu("Consultatie");
            menuBar.add(consultatie);
            JMenuItem nieuweClientItem= new JMenuItem("Nieuwe CliŽnt");
            nieuweClientItem.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_DOWN_MASK));
            consultatie.add(nieuweClientItem);
    
            JMenu boekhouding = new JMenu("Boekhouding");
            menuBar.add(boekhouding);
    
        }
    
            /** Returns the menu bar corresponding to this menu. */
        public JMenuBar getMenuBar() {return menuBar;}
    and my last class, which creates my JPanel, NieuweClientGUI:

    Java Code:
    package test;
    
    import java.awt.FlowLayout;
    
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class NieuweClientGUI {
    
        private JPanel panel;
    
        private JLabel clientIdLabel;
        private JLabel patientIdLabel;
        private JLabel voornaamLabel;
        private JLabel achternaamLabel;
        private JLabel straatLabel;
        private JLabel huisnummerLabel;
        private JLabel busLabel;
        private JLabel telefoonnummerLabel;
        private JLabel gsmnummerLabel;
        private JLabel emailadresLabel;
    
        private JTextField clientId;
        private JTextField patientId;
        private JTextField voornaam;
        private JTextField achternaam;
        private JTextField straat;
        private JTextField huisnummer;
        private JTextField bus;
        private JTextField telefoonnummer;
        private JTextField gsmnummer;
        private JTextField emailadres;
    
        public NieuweClientGUI (){
    
            panel = new JPanel(new FlowLayout());
    
            clientIdLabel = new JLabel("CliŽnt ID:");
            patientIdLabel = new JLabel("PatiŽnt ID:");
            voornaamLabel = new JLabel("Voornaam:");
            achternaamLabel = new JLabel("Achternaam:");
            straatLabel = new JLabel("Staat:");
            huisnummerLabel = new JLabel("Huisnummer:");
            busLabel = new JLabel("Bus:");
            telefoonnummerLabel = new JLabel("Telefoonnummer:");
            gsmnummerLabel = new JLabel("Gsmnummer:");
            emailadresLabel = new JLabel("Emaildres:");
    
            clientId = new JTextField(15);
            patientId = new JTextField(15);
            voornaam = new JTextField(15);
            achternaam = new JTextField(15);
            straat = new JTextField(15);
            huisnummer = new JTextField(15);
            bus = new JTextField(15);
            telefoonnummer = new JTextField(15);
            gsmnummer = new JTextField(15);
            emailadres = new JTextField(15);
    
            panel.add(clientIdLabel);
            panel.add(clientId);
            panel.add(patientIdLabel);
            panel.add(patientId);
            panel.add(voornaamLabel);
            panel.add(voornaam);
            panel.add(achternaamLabel);
            panel.add(achternaam);
            panel.add(straatLabel);
            panel.add(straat);
            panel.add(huisnummerLabel);
            panel.add(huisnummer);
            panel.add(busLabel);
            panel.add(bus);
            panel.add(telefoonnummerLabel);
            panel.add(telefoonnummer);
            panel.add(gsmnummerLabel);
            panel.add(gsmnummer);
            panel.add(emailadresLabel);
            panel.add(emailadres);
    
            panel.setSize(500,500);
            panel.setVisible(true);
    
        }
    
    }
    Greets

  3. #3
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    Well, I think i'm finally in the right direction now, but there is still one little problem from getting it right. The program doesn't recognize my variable frame and I so I can't add my JPanel to my JFrame. Can anyone help me out with this one? I tried with a getter, but that doesn't work since I'm getting an error.

    Java Code:
    package test;
    
    import java.awt.event.InputEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.KeyStroke;
    
    public class MenuGUI {
    
        private JMenuBar menuBar;
    
            /** Constructs the menu bar. */
        public MenuGUI() {
    
            menuBar = new JMenuBar();
    
            JMenu consultatie = new JMenu("Consultatie");
            menuBar.add(consultatie);
            JMenuItem nieuweClientItem= new JMenuItem("Nieuwe CliŽnt");
            nieuweClientItem.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_DOWN_MASK));
            consultatie.add(nieuweClientItem);
    
            JMenu boekhouding = new JMenu("Boekhouding");
            menuBar.add(boekhouding);
    
            nieuweClientItem.addActionListener (
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
    
                            NieuweClientGUI nieuweClientGUI = new NieuweClientGUI();
    
                            [COLOR="Red"]frame[/COLOR].add(nieuweClientGUI.getPanel());
    
                        }
                    }
            );
    
        }
    
            /** Returns the menu bar corresponding to this menu. */
        public JMenuBar getMenuBar() {return menuBar;}
    
    }
    That frame is my JFrame:

    Java Code:
    public class Main{
    
        private static void createAndShowGUI(){
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // construct a Menu object...
            MenuGUI menu = new MenuGUI();
                // ...and obtain its menu bar
            frame.setJMenuBar(menu.getMenuBar());
    
            frame.pack();
            frame.setSize(500,500);
            frame.setVisible(true);
    
        }
    
        public static void main(String[] args) {
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Greets
    Last edited by Kligham; 09-30-2009 at 07:40 PM.

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/BorŚs
    Posts
    107
    Rep Power
    0

    Default

    Im kind of a newbi here, so dont trust this to much. Dont you need to extend your classes?

    Java Code:
    public class Main extends MenuGui{

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    From the strange way that your code is structured, the quickest way would be to replace the line not compiling with
    Java Code:
    menuBar.getParent().getParent().getParent().add(nieuweClientGUI);
    menuBar.getParent().getParent().getParent().validate();
    As you can see, this does not make for good reading. The Frame should be listening for that event instead so it can update itself when the event is fired.

  6. #6
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    From the strange way that your code is structured, the quickest way would be to replace the line not compiling with
    Java Code:
    menuBar.getParent().getParent().getParent().add(nieuweClientGUI);
    menuBar.getParent().getParent().getParent().validate();
    As you can see, this does not make for good reading. The Frame should be listening for that event instead so it can update itself when the event is fired.
    I'm going to give this another try. Someone told me I should use the observer design pattern for this. So I looked into it, and I understand what it does, but I don't really know how you could implement it into this menu problem.

    The observer should be the frame, and the subject would be the JMenuItem? But you have many JMenuItems, so how would you do that?

    Grtz

Similar Threads

  1. Button Actions
    By vasavi.singh in forum Advanced Java
    Replies: 3
    Last Post: 04-03-2009, 10:50 PM
  2. Showing how to add Actions for KeyStrokes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 09:17 PM
  3. Help using Buttons/Actions with Drawings
    By Deathmonger in forum New To Java
    Replies: 0
    Last Post: 04-08-2008, 04:11 PM
  4. Actions
    By alley in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 03:52 PM
  5. Bean related actions in JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 11:04 AM

Posting Permissions

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