Results 1 to 13 of 13
  1. #1
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default Deactivate some methods for some radioButtons

    I want to check somehow which MenuItem was selected, so that for some menu buttons to draw a grid in a different way.

    I have the menu created in the package view, in the class MainUI:

    Java Code:
    	// Create the game menu
    		menuGame = new JMenu( "Game" );
    		menuGame.setMnemonic( 'G' );
    		menuBar.add( menuGame );
    		// Create the game menu
    		// Build a game menu items
                    //...Create 3x3 Game
    		menuGame3 = CreateMenuItem( menuGame, ITEM_RADIO,
    								"Create 3x3 Game", null, '3', null );
    		//...Create 4x4 Game
    		menuGame4 = CreateMenuItem( menuGame, ITEM_RADIO,
    								"Create 4x4 Game", null, '4', null );
                    //...Create 5x5 Game
    		menuGame5 = CreateMenuItem( menuGame, ITEM_RADIO,
    								"Create 5x5 Game", null, '5', null );
    		//...Create 6x6 Game
    		menuGame6 = CreateMenuItem( menuGame, ITEM_RADIO,
    								"Create 6x6 Game", null, '6', null );
                    //...Create 7x7 Game
    		menuGame7 = CreateMenuItem( menuGame, ITEM_RADIO,
    								"Create 7x7 Game", null, '7', null );
                    //...Create 8x8 Game
    		menuGame8 = CreateMenuItem( menuGame, ITEM_RADIO,
    								"Create 8x8 Game", null, '8', null );
                    //...Create 9x9 Game
    		menuGame9 = CreateMenuItem( menuGame, ITEM_RADIO,
    								"Create 9x9 Game", null, '9', null );
                    ButtonGroup group = new ButtonGroup();
                    group.add(menuGame3);
                    group.add(menuGame4);
                    group.add(menuGame5);
                    group.add(menuGame6);
                    group.add(menuGame7);
                    group.add(menuGame8);
                    group.add(menuGame9);
    and in GameUI in the same pakage I have the paint Component:

    Java Code:
            @Override
    	public void paintComponent(Graphics g){
    		highLightCage =false ;
    		super.paintComponent(g); //clear the off-screen bitmap
    		drawCells(g);
       		drawCages(g);
                    drawCageLabels(g);
       		drawLabels(g);
       		drawGameHint(g);
    	}
    What I want to do is somehow disable drawCageLabels(g); when some menus are selected, let's say menuGame8 and menuGame9.
    I'm trying to figure this out the hole day and I can't do it. Can someone help me.
    If it helps, here it is the code for drawCageLabels(g):


    Also I tried with the code you mentioned but it say: *non-static variable menuGame8 cannot be referenced from a static context non-static variable menuGame9 cannot be referenced from a static context*.
    This is the code:

    Java Code:
            @Override
    	public void paintComponent(Graphics g){
    		highLightCage =false ;
    		super.paintComponent(g); //clear the off-screen bitmap
    		drawCells(g);
       		drawCages(g);
                    if ( !MainUI.menuGame8.isSelected() || !MainUI.menuGame9.isSelected() ) {
                         drawCageLabels(g);
                    }
                    // drawCageLabels(g);
       		drawLabels(g);
       		drawGameHint(g);
    	}
    The correct code is above ... the problem was that I didn't made menuGame8 and menuGame9 as public static in MainUI class, so I couldn't refer to them correctly.
    Last edited by Johnny22; 06-12-2011 at 10:26 PM. Reason: removed code that wasn't necesary and posted the solution to the problem

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Cross-posted here: how-to-deactivate-a-method-for-a-specific-menuitem.

    Out of consideration for all, please let us know if you have cross-posted this question anywhere else. Also, I'm with Andrew Thompson on this: consider creating and posting an SSCCE.

  3. #3
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    I'm the same person :) ...

    Don't know what you mean about posting an SSCCE, i've read what it is there and I think I did everything correctly: My code doesn't have errors, it works...it's just a feature I want to add to the application...And I posted all the methods I thought there are needed to show people what I did and what I want to do and I can't post the whole code, there are thousands of lines... If you think there is something else I should add/edit, please tell me and I will add/edit. :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Johnny22 View Post
    I'm the same person :) ...

    Don't know what you mean about posting an SSCCE, i've read what it is there and I think I did everything correctly: My code doesn't have errors, it works...it's just a feature I want to add to the application...And I posted all the methods I thought there are needed to show people what I did and what I want to do and I can't post the whole code, there are thousands of lines... If you think there is something else I should add/edit, please tell me and I will add/edit. :)
    Read the link. An SSCCE should be short, it should be compilable and runnable for us as is and without need for other resources such as database or images, it should demonstrate your problem and it should contain no code unrelated to your problem. Your posted code does not comply since we cannot compile nor run it. And no, we don't want to see your whole program. Instead we ask you to extract out of your program all the stuff unrelated to your current problem and just show us compilable runnable code that allows us to see for ourselves your problem in action.

    And again, please let us know if you've cross-posted this question elsewhere. Most of us dislike duplicating effort that has already been performed elsewhere.

  5. #5
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    Yes I've posted the question elsewhere, on that link you posted before. And to extract the code I can't do, I have many classes, in more pakages that are connected with each other... the program app won't work ... but still, If you think you need something specific, like a whole class, or a method that you think that would help just ask me and I will post it here...

    Also I tried using the following code, as someone recommended on stackoverflow
    Java Code:
    if ( !menuGame8.isSelected() && !menuGame9.isSelected() ) {
       drawCageLabels();
    }
    , on the paintComponent method, but I can't seem to get the menuGame8 and menuGame9 from MainUI ... maybe here is where I'm doing something wrong ...

  6. #6
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default a step closer

    I did
    Java Code:
     @Override
    	public void paintComponent(Graphics g){
    
                   [COLOR="red"] MainUI menuGame3 = new MainUI();[/COLOR]
    
    		highLightCage =false ;
    		super.paintComponent(g); //clear the off-screen bitmap
    		drawCells(g);
       		drawCages(g);
                    [COLOR="red"]if( menuGame3.menuGame3.isSelected() == true){
                        drawCageLabels(g);
                       System.out.println("test for no op");
                    }
                    System.out.println("test for op");[/COLOR]
       		drawLabels(g);
       		drawGameHint(g);
    	}
    But it's not working, it's not getting if it sellected or not. I also tried with false for the if statement, and it's always saying it's not selected.
    What I'm doing wrong ?

    I added more code to the initial post !
    Last edited by Johnny22; 06-12-2011 at 03:07 PM. Reason: added more code

  7. #7
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    Also I did this:
    Java Code:
        @Override
    	public void paintComponent(Graphics g){
    
                    MainUI menuGame = new MainUI();
    
    		highLightCage =false ;
    		super.paintComponent(g); //clear the off-screen bitmap
    		drawCells(g);
       		drawCages(g);
                    if(menuGame.menuGame3.isSelected()){
                        drawCageLabels(g);
                       System.out.println("test for op");
                    }
                    System.out.println("Buton Label: "+menuGame.menuGame3.getText()+"\nStatus: "+menuGame.menuGame3.isSelected());
       		drawLabels(g);
       		drawGameHint(g);
    	}
    And the results are these:
    Buton Label: Create 3x3 Game
    Status: false

    From these I assume that there is something wrong with the isSelect(), or how I did the menu but I can't figure it out... Can u guys pls help ?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Not many can see what is wrong with your code by looking at bits and pieces of it.
    You'll get the best help if you can create a small working program that we can compile and execute. Known as a SSCCE.
    Looks like you need a menu with items that can be selected, a panel with paintComponent, a listener and whatever else.
    There are so many ways for your code to have problems that we can't guess them all. For example you could have two instances of a menu item, one shown and the other not shown. If you test the one not shown, it will never be selected.
    I'm not making that example up. I've seen it happen.

    I just noticed your code, what does the following do:

    MainUI menuGame = new MainUI();

  9. #9
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    The application is working, I can select a menu and a different grid will display, but somehow I can't manage to insert that if statement where I do the drawCageLabels only for some of the Menu Items.

    I inserted that cod because I can't use the direct MainUI.menuGame3, it will give me the error that a non-static variable is referenced from a static context. And I managed to use that code to reference without error, but as you see I can get any detail from that menu, except if it was selected or not...It's always saying that is not selected, except when I manually declare that menu as selected with menuGame3.setSelected(true); in MainUI class, but it will always detect it as selected even when I select something else beside menuGame3. Do you think I implemented that menu wrong ? Visually it is displayed how it should, the radio button selection changes to the menu that is currently selected.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Is menuGame3 static? If so, you do not need to create a new instance of MainUI, you can reference the static variable by:
    MainUI.menuGame3. That would make that bid of code clearer. Now it looks like you are referencing an instance variable.

  11. #11
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    I tried with MainUI.menuGame3 but it says non-static variable menuGame3 cannot be referenced from a static context.

    You can check the code, how I created the menu items on my first post. As I said this way I can get the text/label/hint/etc. for the menu, but I can't make it work with isSelected(), it is always false(it's saying the menu is not selected, even when I select it, and display everything I need - the 3x3 grid). I should change the way I make the menu ? I don't really know what to do, i'm so stuck :(


    I DID IT, and it was so simple that I can't believe:

    I changed public JMenuItem menuGame3; to public static JMenuItem menuGame3;
    Last edited by Johnny22; 06-12-2011 at 09:00 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Using static can be dangerous. You should make the class a Singleton to be sure there is only one instance ever created.
    There'll be other posters with differing opinions on this. My OOP is marginal.

    So was the last part of my post #8 actually the problem? Two instances - one seen and one invisible.

  13. #13
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    I think the problem was that i didn't declared the menuGame3 as static in the other class, i couldn't refer to it like I should ...now it works ... i can even use MainUI.menuGame3.

    Also, how to make a class Singleton ? Why is it dangerous to use static ? The application works fine, I tested it and everything seems to work fine...what kind of problems do you think I will get ?

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. All the methods
    By N00Bie in forum New To Java
    Replies: 5
    Last Post: 02-12-2011, 03:18 PM
  3. Using methods?
    By erickGotJava in forum New To Java
    Replies: 7
    Last Post: 04-10-2010, 04:58 AM
  4. Methods
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 02-27-2010, 12:49 PM
  5. Replies: 4
    Last Post: 04-03-2009, 07:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •