actionlisteners


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: actionlisteners

  1. #1
    Join Date
    Sep 2004
    Posts
    1

    actionlisteners

    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...
    [JAVA]
    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
    about.addActionListener(
    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 );
    aboutFrame.setSize(150,200);
    aboutFrame.show();
    aboutOK.addActionListener(

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

    }
    }
    );


    }
    }
    );
    // END __ Help - About Information Window
    }
    [/JAVA]

    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
    Posts
    635
    Here's how I do my menus.


    JMenuBar menuBar = new JMenuBar();
    menuBar.add(createOptionsMenu());

    private JMenu createOptionsMenu()
    {
    JMenu menu = new JMenu("Options");
    menu.add(createOptionsDirectoriesItem());

    return menu;
    }


    private JMenuItem createOptionsDirectoriesItem()
    {
    JMenuItem item = new JMenuItem("Directories");
    class MenuItemListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    //launchPopupPathConfigure();
    }
    }
    ActionListener listener = new MenuItemListener();
    item.addActionListener(listener);

    return item;
    }

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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