Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  [JFrame listen for JMenuItem label changes?] [FIXED]  (Read 371 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« on: 2012-02-01 20:42:36 »

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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
   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?
 ^_^.

Offline ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #1 on: 2012-02-01 20:59:36 »

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

Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« Reply #2 on: 2012-02-01 21:02:31 »

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

1  
2  
3  
4  
5  
6  
            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.

Games published by our own members! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #3 on: 2012-02-01 21:14:37 »

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. Tongue

Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« Reply #4 on: 2012-02-01 21:17:55 »

Ah, and we have lift off.
Great, just had to remove stopwatch  ._. Shocked.
lol, thanks mate.

Offline ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #5 on: 2012-02-01 21:24:06 »

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:
1  
2  
3  
4  
5  
public void createFrame() [
    menuItem2 = optMenu.add(new JMenuItem("Debugging: Off"));
    optMenu.addSeparator();
    optMenu.add(new JMenuItem("Stopwatch"));
}


Grin

Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« Reply #6 on: 2012-02-01 21:32:07 »

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

Offline ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #7 on: 2012-02-01 22:32:20 »

Yeah try not to overengineer things Grin

Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« Reply #8 on: 2012-02-01 22:46:28 »

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.

Offline ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #9 on: 2012-02-01 23:17:21 »

Hahahahahaha exactly Smiley

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.112 seconds with 20 queries.