[JFrame listen for JMenuItem label changes?] [FIXED]

My problem is that when I create my JFrame, it’s only setting the first String of ‘debugginState’, where later I added a ActionListener, and it’s still not updating the JFileMenuItem’s label.

Here’s the 2 pieces of code.

Code: (Majority chopped out)


	public void createFrame() {
		debuggingState = "Debugging: Off";
		secondButtons = new String[] {debuggingState, "-", "StopWatch"};
		for (String name : secondButtons) {
			menuItem2 = new JMenuItem(name);
			if (name.equalsIgnoreCase("-")) { 
				optMenu.addSeparator(); 
			} else { 
				menuItem2.addActionListener(this); 
				optMenu.add(menuItem2); 
			}
		}
	}

And there’s a ActionListener that is bieng called (had it outprint ‘true’, if the button pressed = ignoreCase"Debugging: Off", than set the debuggingState string = “Debugging: On”;

Here’s the ActionListener:


	public void actionPerformed(ActionEvent evt) {
		String cmd = evt.getActionCommand();
		try {
			if (cmd != null) {
				if (cmd.equalsIgnoreCase("Debugging: Off")) {
					debuggingState = "Debugging: On";
					secondButtons[0] = debuggingState;
					menuItem2.setText(secondButtons[0]);
					pe.debugging = true;
				}
				if (cmd.equalsIgnoreCase("Debugging: On")) {
					debuggingState = "Debugging: Off";
					secondButtons[0] = debuggingState;
					menuItem2.setText(secondButtons[0]);
					pe.debugging = false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

If someone could point me in the direction on how to get that actionPerformed method to update the JFrame’s JMenuItem labels text, that would be great.
Any ideas?
^_^.

You’re only modifying the “debuggingState” variable. To change the label of a JMenuItem, you need to call JMenuItem.setText(String);

I did, it’s only changing the label BELOW that…
Even though done as you said.


				if (cmd.equalsIgnoreCase("Debugging: Off")) {
					OutPrint.write("true");
					debuggingState = "Debugging: On";
					secondButtons[0] = debuggingState;
					menuItem2.setText("Lawl...");
				}

There’s three menuItems (menuItem1 (2 subs)), (menuItem2 (2 subs)), and menuitem3 (3 subs)).

Only one i’m wanting to change is #2, but the first string for the JMenuItem in that menu.

Look at the foreach loop in createFrame(), after the loop ends, menuItem2 is referring to the last assigned JMenuItem object, which here it would be StopWatch. :stuck_out_tongue:

Ah, and we have lift off.
http://www.420magazine.com/forums/images/smilies/smokin2.gif

Great, just had to remove stopwatch ._. :o.
lol, thanks mate.

If you didn’t want to remove stopWatch, the only way to go about this is to manually add each…which is just 3 lines of code instead of the 11 lines of code in your method:


public void createFrame() [
    menuItem2 = optMenu.add(new JMenuItem("Debugging: Off"));
    optMenu.addSeparator();
    optMenu.add(new JMenuItem("Stopwatch"));
}

;D

Going to come in handy when sounds added, thanks.
Going to be looking further into DropDownMenus/CheckBoxes.

Yeah try not to overengineer things ;D

LOL, you definitively need to say that in Java…
I tried creating a little test of what I thought would come out as the ‘Turbine Super Engine’ of platformer games.
Made like 20+ files, looped em all together, (Over-Engineered) to the max.
Came out like shit, and I mean…
Shit lol, took like 15 seconds to show the JFrame.

Hahahahahaha exactly :slight_smile: