Results 1 to 9 of 9
Thread: Exiting on menuSelected action
- 10-13-2009, 02:43 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
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
- 10-13-2009, 02:46 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What do you do after the load happens?
We'd need more information and/or a bit more of your code to be sure.
- 10-13-2009, 03:09 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
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.
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...Java Code:public class LoadScreen extends JFrame { ... above menu code ... } public static void main(String [] args) { LoadScreen ls = new LoadScreen(); }
- 10-13-2009, 03:25 PM #4
Could u paste the complete code?
Ramya:cool:
- 10-13-2009, 03:54 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 10-13-2009, 04:27 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
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
- 10-13-2009, 04:30 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
I suspected it would take time. What with all the clearances required from Langley and possibly even from the highest authorities e.t.c
- 10-13-2009, 04:32 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
You know his name?! I am compromised. *Bang*
- 10-13-2009, 04:58 PM #9
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
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
-
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM -
action class
By jayagowri in forum Web FrameworksReplies: 1Last Post: 05-07-2008, 03:23 PM -
Run RCP action from outside of the Eclipse RCP
By leonbandas in forum EclipseReplies: 1Last Post: 01-07-2008, 07:15 AM -
jsp:forward action
By Java Tip in forum Java TipReplies: 0Last Post: 12-24-2007, 10:04 AM -
jsp:param action
By Java Tip in forum Java TipReplies: 0Last Post: 12-24-2007, 10:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks