DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: actionlisteners

  1. #1
    Join Date
    Sep 2004


    Hi All,

    I'm a bit of a noob to Java, and was wondering the best way to complete the actions of a menubar. What I have done in my Java code is to create a menu bar, and implement an actionlistener for each menu item, which then has some code in the action listener to perform a task. e.g. Menu item Help -> About creates a new JFrame which is simply an about window.

    I have a couple of Q's, is this the right way to do it? it just doesn't look very tidy, and if a menu item needs to do a lot then that will be a big actionlistener, and doesn't make for very readable code. Maybe I should have a menu class, and have a method as each menu items action listener?

    Here is my code...
    private void buildMainMenu(JMenuBar menu)
    JMenu file = new JMenu("&File");
    JMenu help = new JMenu( "Help" );

    JMenuItem connect = new JMenuItem( "Connect" );
    JMenuItem about = new JMenuItem( "&About" );

    file.add( connect );
    help.add( about );
    menu.add( file );
    menu.add( help );

    // START __ Help - About Information Window
    new ActionListener() {

    public void actionPerformed( ActionEvent e) {

    JFrame aboutFrame = new JFrame( title+" - About" );

    Container aboutCont = aboutFrame.getContentPane();

    JPanel aboutNorth = new JPanel();
    JPanel aboutSouth = new JPanel();
    JLabel aboutLabel = new JLabel( title+"\nv1.01", SwingConstants.CENTER);
    JButton aboutOK = new JButton( "OK" );
    aboutNorth.add( aboutLabel );
    aboutSouth.add( aboutOK );
    aboutCont.add( aboutNorth, BorderLayout.NORTH );
    aboutCont.add( aboutSouth, BorderLayout.SOUTH );

    new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    aboutFrame.setVisible( false );


    // END __ Help - About Information Window

    Currently, the code above simply builds the menu for the JFrame. Help -> about creates a new JFrame, which displays version info about the app, within which is an OK button which I would like to close the current JFrame with. When I try to reference aboutFrame (the JFrame I want to close) in the action listener for the OK button in the JFrame aboutFrame, it doesn't know what i'm talking about, i.e. can't see aboutFrame, but why not??

    I will be very grateful to anyone that tries to help a rising Java Dev...

    Cheers every1,

    - Harvey

  2. #2
    Join Date
    Mar 2004
    Here's how I do my menus.

    JMenuBar menuBar = new JMenuBar();

    private JMenu createOptionsMenu()
    JMenu menu = new JMenu("Options");

    return menu;

    private JMenuItem createOptionsDirectoriesItem()
    JMenuItem item = new JMenuItem("Directories");
    class MenuItemListener implements ActionListener
    public void actionPerformed(ActionEvent e)
    ActionListener listener = new MenuItemListener();

    return item;

  3. #3
    Join Date
    Feb 2004
    Make the actionlistener as short as possible, it should just call a method. Make that method do all the work. That is how Netbeans would create it for you if you used it's GUI editor.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.