Java ActionListener unresponsive?

Just got done rewriting my Gui (probably why it’s not working…).
Added everything the way it previously was, of course removing/fixing (hopefully) all the errors with the Gui.
Trying to add it to my JMenuBar/JMenu/JMenuItem.
Note: Not all 3, but you get an idea where it’s trying to be implemented…
Hoping someone can take the time to read my 2 code snippets… atleast point me in a direction on why my ActionListener isn’t working, and yes my Gui class does implement a ActionListener.

Here’s my actionPerformed method, nothing edited:


	public void actionPerformed(ActionEvent evt) {
		String cmd = evt.getActionCommand();
		try {
			if (cmd != null) {
				if (cmd.equalsIgnoreCase("Exit")) {
					int i;
                			if ( (i = JOptionPane.showConfirmDialog(this, "Do you really wish to exit the client?")) == 0) {
                    				System.exit(0);
					}
					System.out.println("does this shit even work...");
           			}
				if (cmd.equalsIgnoreCase("Low Memory")) {
					if (!super.lowMem) {
						// setlow
						super.setLowMem();
					} else if (super.lowMem) {
						// refuse already low
					}
				}
				if (cmd.equalsIgnoreCase("High Memory")) {
					if (super.lowMem) {
						// sethigh
						super.setHighMem();
					} else if (!lowMem) {
						// refuse already high
					}
				}
			}
		} catch (Exception e) { }
	}

Might be kinda long for someone to read, here’s the initUI method (sets up Gui) :


	private final void initUI(String title) {
		try {
			frame = new JFrame(title);
			cp = frame.getContentPane();
			dim = new Dimension(ClientSettings.w, ClientSettings.h);
			initLookAndFeel();
			gamePanel = new JPanel();
			gamePanel.setLayout(new BorderLayout());
			gamePanel.add(this, BorderLayout.CENTER);
			gamePanel.setPreferredSize(dim);
			frame.setResizable(true);
			frame.setDefaultCloseOperation(3);
			frame.setLayout(new BorderLayout());
			// Begin operations and component setup here..
//begin
			frame.setIconImage(JFICON.getImage());
			this.menuBar = new JMenuBar();
			this.jmenubar = new JMenuBar();
			this.fileMenu = new JMenu("File");
			this.detailMenu = new JMenu("Memory Settings");
			for (String itemOneName : this.fileMenuItems) {
				JMenuItem menuItem1 = new JMenuItem(itemOneName);
				if (itemOneName.equalsIgnoreCase("-")) {
					fileMenu.addSeparator();
				} else {
					menuItem1.addActionListener(this);
					fileMenu.add(itemOneName);
				}
			}
			for (String itemTwoName : this.detailMenuItems) {
				JMenuItem menuItem2 = new JMenuItem(itemTwoName);
				if (itemTwoName.equalsIgnoreCase("-")) {
					detailMenu.addSeparator();
				} else {
					menuItem2.addActionListener(this);
					detailMenu.add(itemTwoName);
				}
			}
			frame.add(jmenubar);
			menuBar.add(fileMenu);
			menuBar.add(detailMenu);
//end
			cp.add(this.menuBar, BorderLayout.NORTH);
			cp.add(this.gamePanel, BorderLayout.CENTER);
			gamePanel.setBackground(Color.black.darker());
			this.setCursor(0); // Regular Gold Cursor
			frame.pack(); // Final Frame Init
			frame.validate();
			frame.setVisible(true);
			frame.setLocationRelativeTo(null);
			super.init();
		} catch (Exception exc) {
			exc.printStackTrace(System.err);
			System.err.println("A Fatal Exception has occured during runtime!");
			System.exit(0);
		}
	}

You never add ‘menuItem2’ to the ‘detailMenu’, you only add the String ‘itemTwoName’.

Kind of confused on your grammer there r4king, no offence…
Are you saying i ‘Should never’, or ‘I never did’?

Line 36: You never added menuItem2

EDIT: Btw, you would want to move Line 31 to before Line 35 because that’s a waste of an object if the string happens to be a separator.

lol, i’m not having issues while trying to add Items to the menus.
I’m having issues with getting the ActionListener to even outprint on the cmd when its hit…

Usually i set a Gui up first, easiest thing to do, this one doesn’t want to play nice…

EDIT: want me to post the whole class? not too big :S

Well…if the item is not added…how will it ever fire the ActionListener? :wink:

It’s all good…
I just cheffed this up:


public class Jframe {

	public Jframe() {
		init();
	}

	public void init() {
		JMenuBar jmb = new JMenuBar();
		JMenu jm = new JMenu("nub");
		JMenuItem item = new JMenuItem("file");
		JFrame frame = new JFrame("");
		jm.add(item);
		item.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.err.println("WORK????");
			}
			
		});
		jmb.add(jm);
		frame.add(jmb, BorderLayout.NORTH);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(3);
	}

	public static void main(String[] args) {
		new Jframe();
	}
}

Works perfect…
I’ll just rewrite it (Actually thinking this time…) not speed programming lulz

Did you notice the original problem though? Look at your second code block in your original post. You see lines 31-36? Look at 35 and 36. You never added the JMenuItem ‘menuItem2’ to the JMenu ‘detailMenu’. You only added a String!

All i see me doing is:


for (String itemTwoName : this.detailMenuItems) {
//ONE: creating a JMenuItem, giving it a name by the first in a String[] of names.
            JMenuItem menuItem2 = new JMenuItem(itemTwoName);
            if (itemTwoName.equalsIgnoreCase("-")) {
               detailMenu.addSeparator();
            } else {
//TWO: adding a ActionListener to the JMenuItem
               menuItem2.addActionListener(this);
//THREE: adding the JMenuItem's names to it...
               detailMenu.add(itemTwoName);
            }
         }

Why would I ‘not’ want to add a ‘String’ for the JMenuItem’s ‘items’ again?

facepalm ;D

But you don’t want to add a String! You want to add a JMenuItem that has an ActionListener attached to it! Therefore, change line 10 to ‘detailMenu.add(menuItem2)’ :wink: