jMenuItem doesn't appear










0















I've just starting using Java Swing and I have a issue.
I tried to do a simple menuBar and a menuItem 'Exit', but before linking the button to the action the menuItem appeared, now that I've linked the button to a System.exit(0) action it disappeared. Help?
The code is the following:



in MainPanel (the autogenerated code from swing is excluded):



public void init() 
initComponents();
initActions();

setLocationRelativeTo(null);
pack();
setVisible(true);


private void initActions()
this.menuItemExit.setAction(Application.getInstance().getPanelControl().getActionExit());




In PanelControl:



public class PanelControl {

private Action actionExit;


public Action getActionExit()
return actionExit;




public class ActionExit extends AbstractAction

public ActionExit()
putValue(Action.NAME, "Exit");
putValue(Action.SHORT_DESCRIPTION, "Exit from the application");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl e"));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);


@Override
public void actionPerformed(ActionEvent e)
System.exit(0);





In Application:



private void init() 
viewMainPanel = new MainPanel();
controlPanel = new ControlPanel();
viewMainPanel.init();




i think the problem is somewhere in here but i can't figure out where. any help?
(there's other code but i just put the more relevant part, also i translated the code from italian so i'm sorry if there are any problems or a few names dont match up)










share|improve this question






















  • Show the code that creates the menuBar.

    – Paco Abato
    Nov 13 '18 at 17:14















0















I've just starting using Java Swing and I have a issue.
I tried to do a simple menuBar and a menuItem 'Exit', but before linking the button to the action the menuItem appeared, now that I've linked the button to a System.exit(0) action it disappeared. Help?
The code is the following:



in MainPanel (the autogenerated code from swing is excluded):



public void init() 
initComponents();
initActions();

setLocationRelativeTo(null);
pack();
setVisible(true);


private void initActions()
this.menuItemExit.setAction(Application.getInstance().getPanelControl().getActionExit());




In PanelControl:



public class PanelControl {

private Action actionExit;


public Action getActionExit()
return actionExit;




public class ActionExit extends AbstractAction

public ActionExit()
putValue(Action.NAME, "Exit");
putValue(Action.SHORT_DESCRIPTION, "Exit from the application");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl e"));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);


@Override
public void actionPerformed(ActionEvent e)
System.exit(0);





In Application:



private void init() 
viewMainPanel = new MainPanel();
controlPanel = new ControlPanel();
viewMainPanel.init();




i think the problem is somewhere in here but i can't figure out where. any help?
(there's other code but i just put the more relevant part, also i translated the code from italian so i'm sorry if there are any problems or a few names dont match up)










share|improve this question






















  • Show the code that creates the menuBar.

    – Paco Abato
    Nov 13 '18 at 17:14













0












0








0








I've just starting using Java Swing and I have a issue.
I tried to do a simple menuBar and a menuItem 'Exit', but before linking the button to the action the menuItem appeared, now that I've linked the button to a System.exit(0) action it disappeared. Help?
The code is the following:



in MainPanel (the autogenerated code from swing is excluded):



public void init() 
initComponents();
initActions();

setLocationRelativeTo(null);
pack();
setVisible(true);


private void initActions()
this.menuItemExit.setAction(Application.getInstance().getPanelControl().getActionExit());




In PanelControl:



public class PanelControl {

private Action actionExit;


public Action getActionExit()
return actionExit;




public class ActionExit extends AbstractAction

public ActionExit()
putValue(Action.NAME, "Exit");
putValue(Action.SHORT_DESCRIPTION, "Exit from the application");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl e"));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);


@Override
public void actionPerformed(ActionEvent e)
System.exit(0);





In Application:



private void init() 
viewMainPanel = new MainPanel();
controlPanel = new ControlPanel();
viewMainPanel.init();




i think the problem is somewhere in here but i can't figure out where. any help?
(there's other code but i just put the more relevant part, also i translated the code from italian so i'm sorry if there are any problems or a few names dont match up)










share|improve this question














I've just starting using Java Swing and I have a issue.
I tried to do a simple menuBar and a menuItem 'Exit', but before linking the button to the action the menuItem appeared, now that I've linked the button to a System.exit(0) action it disappeared. Help?
The code is the following:



in MainPanel (the autogenerated code from swing is excluded):



public void init() 
initComponents();
initActions();

setLocationRelativeTo(null);
pack();
setVisible(true);


private void initActions()
this.menuItemExit.setAction(Application.getInstance().getPanelControl().getActionExit());




In PanelControl:



public class PanelControl {

private Action actionExit;


public Action getActionExit()
return actionExit;




public class ActionExit extends AbstractAction

public ActionExit()
putValue(Action.NAME, "Exit");
putValue(Action.SHORT_DESCRIPTION, "Exit from the application");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl e"));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);


@Override
public void actionPerformed(ActionEvent e)
System.exit(0);





In Application:



private void init() 
viewMainPanel = new MainPanel();
controlPanel = new ControlPanel();
viewMainPanel.init();




i think the problem is somewhere in here but i can't figure out where. any help?
(there's other code but i just put the more relevant part, also i translated the code from italian so i'm sorry if there are any problems or a few names dont match up)







java swing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 17:05









frafra

32




32












  • Show the code that creates the menuBar.

    – Paco Abato
    Nov 13 '18 at 17:14

















  • Show the code that creates the menuBar.

    – Paco Abato
    Nov 13 '18 at 17:14
















Show the code that creates the menuBar.

– Paco Abato
Nov 13 '18 at 17:14





Show the code that creates the menuBar.

– Paco Abato
Nov 13 '18 at 17:14












2 Answers
2






active

oldest

votes


















0














private Action actionExit;


public Action getActionExit()
return actionExit;



Your actionExit variable is null.



Nowhere in your code do you create an instance of your ActionExit class.



Somewhere you need:



actionExit = new ActionExit();


Your design seems a bit complicated, I have no idea why you have a panel just to create an instance of the ActionExit class.



I would suggest you just create the ActionExit instance in your main class and get rid of the PanelControl class.



Instead of using an IDE to generate confusing code you should consider learning how to write the code yourself so you can better structure your classes. Read the section from the Swing tutorial on How to Use Menus for a working example to get you started.






share|improve this answer






























    0














    A menu item has to be added to a Native Java Swing component. You have to add it to a JFrame. You can't add a MenuItem to a Panel. The Parent 'root' container in any Java Swing application is 'native' and a JFrame. Everything else in that container is 'drawn' into the rectangle using the look and feel of your choosing.



    Then you CREATE a MenuItem using your TAbstractAction item. That object CAN be used to create a JButton, JMenuItem or ToolBar button. Keeping a reference to your TAbstractAction in your code, you can enable/disable the object and it implements an 'observable' pattern where it will enable/disable ALL UI controls you used to build with it. I actually wrote a Java Swing framework for doing Java Applications. It used to be on the Sun Open Source web site. If you wish I can put it up on GitLab for you to play with. Java Swing is nice but JavaFX should be the long term goal for UI on a JVM.



    In your JFrame object you need to do this:



     _menuBar = new JMenuBar();

    // add controls to the frame
    setJMenuBar(_menuBar);


    Then you need to add your 'exitMenuItem' to your _MenuBar control.
    Cheers






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286160%2fjmenuitem-doesnt-appear%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      private Action actionExit;


      public Action getActionExit()
      return actionExit;



      Your actionExit variable is null.



      Nowhere in your code do you create an instance of your ActionExit class.



      Somewhere you need:



      actionExit = new ActionExit();


      Your design seems a bit complicated, I have no idea why you have a panel just to create an instance of the ActionExit class.



      I would suggest you just create the ActionExit instance in your main class and get rid of the PanelControl class.



      Instead of using an IDE to generate confusing code you should consider learning how to write the code yourself so you can better structure your classes. Read the section from the Swing tutorial on How to Use Menus for a working example to get you started.






      share|improve this answer



























        0














        private Action actionExit;


        public Action getActionExit()
        return actionExit;



        Your actionExit variable is null.



        Nowhere in your code do you create an instance of your ActionExit class.



        Somewhere you need:



        actionExit = new ActionExit();


        Your design seems a bit complicated, I have no idea why you have a panel just to create an instance of the ActionExit class.



        I would suggest you just create the ActionExit instance in your main class and get rid of the PanelControl class.



        Instead of using an IDE to generate confusing code you should consider learning how to write the code yourself so you can better structure your classes. Read the section from the Swing tutorial on How to Use Menus for a working example to get you started.






        share|improve this answer

























          0












          0








          0







          private Action actionExit;


          public Action getActionExit()
          return actionExit;



          Your actionExit variable is null.



          Nowhere in your code do you create an instance of your ActionExit class.



          Somewhere you need:



          actionExit = new ActionExit();


          Your design seems a bit complicated, I have no idea why you have a panel just to create an instance of the ActionExit class.



          I would suggest you just create the ActionExit instance in your main class and get rid of the PanelControl class.



          Instead of using an IDE to generate confusing code you should consider learning how to write the code yourself so you can better structure your classes. Read the section from the Swing tutorial on How to Use Menus for a working example to get you started.






          share|improve this answer













          private Action actionExit;


          public Action getActionExit()
          return actionExit;



          Your actionExit variable is null.



          Nowhere in your code do you create an instance of your ActionExit class.



          Somewhere you need:



          actionExit = new ActionExit();


          Your design seems a bit complicated, I have no idea why you have a panel just to create an instance of the ActionExit class.



          I would suggest you just create the ActionExit instance in your main class and get rid of the PanelControl class.



          Instead of using an IDE to generate confusing code you should consider learning how to write the code yourself so you can better structure your classes. Read the section from the Swing tutorial on How to Use Menus for a working example to get you started.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 17:10









          camickrcamickr

          275k15127239




          275k15127239























              0














              A menu item has to be added to a Native Java Swing component. You have to add it to a JFrame. You can't add a MenuItem to a Panel. The Parent 'root' container in any Java Swing application is 'native' and a JFrame. Everything else in that container is 'drawn' into the rectangle using the look and feel of your choosing.



              Then you CREATE a MenuItem using your TAbstractAction item. That object CAN be used to create a JButton, JMenuItem or ToolBar button. Keeping a reference to your TAbstractAction in your code, you can enable/disable the object and it implements an 'observable' pattern where it will enable/disable ALL UI controls you used to build with it. I actually wrote a Java Swing framework for doing Java Applications. It used to be on the Sun Open Source web site. If you wish I can put it up on GitLab for you to play with. Java Swing is nice but JavaFX should be the long term goal for UI on a JVM.



              In your JFrame object you need to do this:



               _menuBar = new JMenuBar();

              // add controls to the frame
              setJMenuBar(_menuBar);


              Then you need to add your 'exitMenuItem' to your _MenuBar control.
              Cheers






              share|improve this answer





























                0














                A menu item has to be added to a Native Java Swing component. You have to add it to a JFrame. You can't add a MenuItem to a Panel. The Parent 'root' container in any Java Swing application is 'native' and a JFrame. Everything else in that container is 'drawn' into the rectangle using the look and feel of your choosing.



                Then you CREATE a MenuItem using your TAbstractAction item. That object CAN be used to create a JButton, JMenuItem or ToolBar button. Keeping a reference to your TAbstractAction in your code, you can enable/disable the object and it implements an 'observable' pattern where it will enable/disable ALL UI controls you used to build with it. I actually wrote a Java Swing framework for doing Java Applications. It used to be on the Sun Open Source web site. If you wish I can put it up on GitLab for you to play with. Java Swing is nice but JavaFX should be the long term goal for UI on a JVM.



                In your JFrame object you need to do this:



                 _menuBar = new JMenuBar();

                // add controls to the frame
                setJMenuBar(_menuBar);


                Then you need to add your 'exitMenuItem' to your _MenuBar control.
                Cheers






                share|improve this answer



























                  0












                  0








                  0







                  A menu item has to be added to a Native Java Swing component. You have to add it to a JFrame. You can't add a MenuItem to a Panel. The Parent 'root' container in any Java Swing application is 'native' and a JFrame. Everything else in that container is 'drawn' into the rectangle using the look and feel of your choosing.



                  Then you CREATE a MenuItem using your TAbstractAction item. That object CAN be used to create a JButton, JMenuItem or ToolBar button. Keeping a reference to your TAbstractAction in your code, you can enable/disable the object and it implements an 'observable' pattern where it will enable/disable ALL UI controls you used to build with it. I actually wrote a Java Swing framework for doing Java Applications. It used to be on the Sun Open Source web site. If you wish I can put it up on GitLab for you to play with. Java Swing is nice but JavaFX should be the long term goal for UI on a JVM.



                  In your JFrame object you need to do this:



                   _menuBar = new JMenuBar();

                  // add controls to the frame
                  setJMenuBar(_menuBar);


                  Then you need to add your 'exitMenuItem' to your _MenuBar control.
                  Cheers






                  share|improve this answer















                  A menu item has to be added to a Native Java Swing component. You have to add it to a JFrame. You can't add a MenuItem to a Panel. The Parent 'root' container in any Java Swing application is 'native' and a JFrame. Everything else in that container is 'drawn' into the rectangle using the look and feel of your choosing.



                  Then you CREATE a MenuItem using your TAbstractAction item. That object CAN be used to create a JButton, JMenuItem or ToolBar button. Keeping a reference to your TAbstractAction in your code, you can enable/disable the object and it implements an 'observable' pattern where it will enable/disable ALL UI controls you used to build with it. I actually wrote a Java Swing framework for doing Java Applications. It used to be on the Sun Open Source web site. If you wish I can put it up on GitLab for you to play with. Java Swing is nice but JavaFX should be the long term goal for UI on a JVM.



                  In your JFrame object you need to do this:



                   _menuBar = new JMenuBar();

                  // add controls to the frame
                  setJMenuBar(_menuBar);


                  Then you need to add your 'exitMenuItem' to your _MenuBar control.
                  Cheers







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 17:32

























                  answered Nov 13 '18 at 17:24









                  WaxhawWaxhaw

                  274




                  274



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286160%2fjmenuitem-doesnt-appear%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3