Results 1 to 9 of 9
  1. #1
    benra is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default Exiting on menuSelected action

    Hi all

    I'm writing a simple swing screen for my Dad. He's a bit of a technophobe (by that I mean a lot of a technophobe), so I'm trying to keep it really, really simple.

    I've added some click options as menus, such that when he clicks the menu the event springs into life. I could go down the whole menu ITEMS route but, like I said, keeping it really simple. I've got this:


    Java Code:
    JMenu menu = new JMenu("Load");
    menu.addMenuListener(new MenuListener() {
    	public void menuEvent(MenuEvent e) {				
    	}
    	public void menuCanceled(MenuEvent e) {				
    	}
    	public void menuSelected(MenuEvent e) {
    		/* Do the load */
    	}
    	public void menuDeselected(MenuEvent e) {				
    	}
    });
    menuBar.add(menu);

    When I click the menu, the load happens, but within a few seconds the whole program exits, no stacktrace or anything. I don't have the luxury of a debugger where I am at the moment so I don't know quite how far it's getting. Anyway, is there something I need to do to stop the whole damn thing disintegrating when the menu is clicked?

    Thanks
    Ben

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

    Default

    What do you do after the load happens?
    We'd need more information and/or a bit more of your code to be sure.

  3. #3
    benra is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the response.

    I don't do anything after the load yet. My (inexperienced) understanding is that the menu event is handled, and the window should stay open and return to a state awaiting user input.

    I'm building a JFrame with a few Components, a few menu options, and I understood that it sits open until you explicitly close it.

    Java Code:
    public class LoadScreen extends JFrame
    {
    ...
    above menu code
    ...
    }
    
    public static void main(String [] args)
    {
    	LoadScreen ls = new LoadScreen();
    }
    What I can see is happening is that the program is simply running to completion, main() is exiting. How do I handle the situation where I want the LoadScreen to remain alive and active once an event has been handled? I think I have a fundamental lack of understanding of how swing works...

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    Could u paste the complete code?
    Ramya:cool:

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

    Default

    If the code for the LoadScreen class were not classified and filed under code sub zero protocols, you could have posted it and we could have had a look-see what's up with it.

  6. #6
    benra is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    If the code for the LoadScreen class were not classified and filed under code sub zero protocols, you could have posted it and we could have had a look-see what's up with it.

    Haha, I guess I am coming across kind of mysterious. The whole thing is rather messy as it's the result of a few hours of poking at swing with a stick, so I need to tidy it up and rip out all the irrelevant bits before I post it. I was hoping that pasting a few relevant chunks might be enough to get my question across, but I will be clearer.

    I'm also at work and being a bit naughty coding on the side so I will have to come back to you!

    Thanks
    Ben

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

    Default

    I suspected it would take time. What with all the clearances required from Langley and possibly even from the highest authorities e.t.c

  8. #8
    benra is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    You know his name?! I am compromised. *Bang*

  9. #9
    benra is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Ah, I've figured it out. Because there's also a menu item Exit, which System.exits on menuSelected, when I clicked the Load menu and moved the mouse past the Exit menu it was calling Exit.menuSelected and thus exiting.

    However, my problem now is that when the menu is clicked I need it to deselect straight away but it remains selected until I click elsewhere, which is ugly. Run the code below and you'll see what I mean. So is there a cleaner way of doing this, such that the menu name itself acts as the button?

    Java Code:
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    import java.awt.print.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    
    public class LoadScreenSkeleton extends JFrame {
    
    	private static LoadScreenSkeleton ls;
    	private JDesktopPane jdpDesktop = new JDesktopPane();
    	private Dimension screenSize = new Dimension();
    	private static JList list;
    
    	public static void main(String [] args)
    	{
    		ArrayList<String> listMembers = new ArrayList<String>();
    		
    		for (int i=0; i<10; i++) {
    			listMembers.add("List item " + i);
    		}
    		
    		list = new JList(listMembers.toArray());
    		ls = new LoadScreenSkeleton();
    	}
    	
    	protected JMenuBar createMenuBar() {
    		JMenuBar menuBar = new JMenuBar();
    		
    		JMenu menu = new JMenu("Load");
    		menu.addMenuListener(new MenuListener() {
    			public void menuEvent(MenuEvent e) {				
    			}
    			public void menuCanceled(MenuEvent e) {				
    			}
    			public void menuSelected(MenuEvent e) {
    				System.out.println("Loading " + (String)(list.getSelectedValue()));
    			}
    			public void menuDeselected(MenuEvent e) {				
    			}
    		});
    		
    		menuBar.add(menu);
    
    		menu = new JMenu("Exit");
    		menu.addMenuListener(new MenuListener() {
    			public void menuEvent(MenuEvent e) {				
    			}
    			public void menuCanceled(MenuEvent e) {				
    			}
    			public void menuSelected(MenuEvent e) {
    				System.exit(0);
    			}
    			public void menuDeselected(MenuEvent e) {				
    			}
    		});
    		
    		menuBar.add(menu);
    		
    		return menuBar;
    	}
    	
    	public void doLoad(String name)
    	{
    		System.out.println("Loading " + name);
    	}
    	
    	public LoadScreenSkeleton() {
    
    		super("MenuListener example");
    		
    		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		setBounds(0,0, screenSize.width, screenSize.height);
    
    		addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    
    		jdpDesktop.setLayout(new FlowLayout());
    
    		setContentPane(jdpDesktop);
    		setJMenuBar(createMenuBar());
    		
    		JScrollPane listScroller = new JScrollPane(list);
    		listScroller.setPreferredSize(new Dimension(screenSize.width - 20,
    				screenSize.height - 20));
    		listScroller.setAlignmentX(LEFT_ALIGNMENT);
    
    		JPanel listPane = new JPanel();
    		listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
    		JLabel label = new JLabel("Items:");
    
    		listPane.add(label);
    		listPane.add(Box.createRigidArea(new Dimension(0,5)));
    		listPane.add(listScroller);
    		listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    
    		jdpDesktop.add(listPane);
    		jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
    		setVisible(true);
    
    	}	
    }

Similar Threads

  1. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM
  2. action class
    By jayagowri in forum Web Frameworks
    Replies: 1
    Last Post: 05-07-2008, 03:23 PM
  3. Run RCP action from outside of the Eclipse RCP
    By leonbandas in forum Eclipse
    Replies: 1
    Last Post: 01-07-2008, 07:15 AM
  4. jsp:forward action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:04 AM
  5. jsp:param action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:03 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
  •