Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Lightbulb runtime MenuBar Generator

    Hello ,

    I am trying to generate menu-bar run time. But application throws exception. Can anyone know that how can we generate Swing Menu-Bar run time or modify it Run time?


    thanks in Advance

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: runtime MenuBar Generator

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    Also, whenever seeking help with an exception, always post the stack trace and indicate which code line gave rise to the exception.

    db

  3. #3
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Default Re: runtime MenuBar Generator

    <code>
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * NewJFrame.java
    *
    * Created on Oct 24, 2011, 12:37:17 PM
    */
    /**
    *
    * @author vns08103
    */

    import javax.swing.*;
    public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(132, 132, 132)
    .addComponent(jButton1)
    .addContainerGap(195, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addContainerGap(199, Short.MAX_VALUE)
    .addComponent(jButton1)
    .addGap(78, 78, 78))
    );

    pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    JMenu fileMenu = new JMenu("File");
    JMenuItem fileMenuNew = new JMenuItem("New");
    JMenuItem fileMenuEdit = new JMenuItem("Edit");

    fileMenu.add(fileMenuNew);
    fileMenu.add(fileMenuEdit);

    JMenuItem helpMenu = new JMenu("Help");
    JMenuItem helpMenuAbout = new JMenuItem("About Us");

    helpMenu.add(helpMenuAbout);

    menuBar.add(fileMenu);
    menuBar.add(helpMenu);

    this.setJMenuBar(menuBar);
    this.setVisible(true);
    //JFrame frame = new JFrame("New");
    //frame.setJMenuBar(menuBar);
    //frame.setVisible(true);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new NewJFrame().setVisible(true);
    }
    });
    }
    JMenuBar menuBar;
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration
    }

    </code>

  4. #4
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Default Re: runtime MenuBar Generator

    The program is developed in NetBeans 7 as desktop application. I wrote a action command on button press and on press it will generate following exceptions
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * NewJFrame.java
    *
    * Created on Oct 24, 2011, 12:37:17 PM
    */
    /**
    *
    * @author vns08103
    */

    import javax.swing.*;
    public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(132, 132, 132)
    .addComponent(jButton1)
    .addContainerGap(195, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addContainerGap(199, Short.MAX_VALUE)
    .addComponent(jButton1)
    .addGap(78, 78, 78))
    );

    pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    JMenu fileMenu = new JMenu("File");
    JMenuItem fileMenuNew = new JMenuItem("New");
    JMenuItem fileMenuEdit = new JMenuItem("Edit");

    fileMenu.add(fileMenuNew);
    fileMenu.add(fileMenuEdit);

    JMenuItem helpMenu = new JMenu("Help");
    JMenuItem helpMenuAbout = new JMenuItem("About Us");

    helpMenu.add(helpMenuAbout);

    menuBar.add(fileMenu);
    menuBar.add(helpMenu);

    this.setJMenuBar(menuBar);
    this.setVisible(true);
    //JFrame frame = new JFrame("New");
    //frame.setJMenuBar(menuBar);
    //frame.setVisible(true);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new NewJFrame().setVisible(true);
    }
    });
    }
    JMenuBar menuBar;
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration
    }


    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\


    run:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at NewJFrame.jButton1ActionPerformed(NewJFrame.java:7 9)
    at NewJFrame.access$000(NewJFrame.java:17)
    at NewJFrame$1.actionPerformed(NewJFrame.java:40)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 5 seconds)


    \\\\\\\\\\\\\\\\\ Image is attached for reference
    runtime MenuBar Generator-runtime-menu-generator.jpg

    Please help me.

    Thanks in advance
    Last edited by vishrut_n_shah; 10-25-2011 at 05:25 AM.

  5. #5
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: runtime MenuBar Generator

    JMenuItem helpMenu = new JMenu("Help"); ??

    menuBar.add(helpMenu);

    this.setJMenuBar(menuBar);
    this.setVisible(true); //??? it is already visible //FRAME

    Ofcourse this is not your problem. Your problem is more detailed.

    AWT-EVENTS like action performed or mouseEvent are stored in a queue. when you trigger the button AWT-Event queue is empty .
    Wow, most probably setVisible(true ) methods invoke paint method so a chain of event produced by this method. But there is not a event in the queue because Window(JFRAME) and its components are already drawn.
    Last edited by Grkn; 10-25-2011 at 10:43 AM.

  6. #6
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Default Re: runtime MenuBar Generator

    Hello Grkn,

    I just wanted to show the menu bar on click of the button. So according to your previous explanation it is not possible until we show it in different Frame. Right?

    Is there any other way that can do this thing?

    Thanks in advance.

Similar Threads

  1. Vertical menubar
    By Java Unknown in forum New To Java
    Replies: 0
    Last Post: 02-22-2011, 05:50 AM
  2. Visual Editor MenuBar problems
    By StevenJMUK in forum SWT / JFace
    Replies: 0
    Last Post: 09-01-2009, 02:55 PM
  3. About MenuBar
    By makpandian in forum AWT / Swing
    Replies: 1
    Last Post: 03-20-2009, 11:50 AM
  4. Help about Java Menubar
    By makpandian in forum AWT / Swing
    Replies: 5
    Last Post: 02-09-2009, 08:17 AM
  5. menubar in splitpane
    By masa in forum AWT / Swing
    Replies: 1
    Last Post: 12-16-2008, 09:01 AM

Tags for this Thread

Posting Permissions

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