Results 1 to 19 of 19
  1. #1
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default Problem Actionlistener

    Hey guys,

    I think it's a problem pretty easy to solve, I just don't see it, i guess ;D

    Few weeks ago I was trying to set buttons in a JTable, but i got this problem when i wanted to call a function from another class, it said: cannot use this in a static context. guess it's actually cannot use "this" in a static context...
    I've blown off the whole project because i totally began to go mad ;D
    this morning I couldn't believe my eyes, the same problem... I worked too hard on this program to just let it go, so could anyone help me? ^^

    The problem is the following:
    WhenI want to add actionlisteners to my RadioButtons I use
    RadioButtonName.addActionListener(this);
    this code is placed in the actionlistener itself; with other words it's like 'if you click on this button, create a few radiobuttons, place them etc,... and add THIS actionlistener to it.

    I know Ii could easily work it out making an actionlistener in another class, but things get pretty messy then, so I wanted to know how to solve the problem, not how to evade ;) because in fact, I would need to add a JTable with buttons to this project too, and if I get the same problem as few weeks ago, it's all screwed :p

    Thx anyways, here's the relevant code:

    Java Code:
    public class chesswindow extends JFrame implements ActionListener, WindowListener {
    ...
    public static JRadioButton[][] WhiteWins = new JRadioButton[100][1000];
    public static JRadioButton[][] BlackWins = new JRadioButton[100][1000];
    public static JRadioButton[][] Draw = new JRadioButton[100][1000];
    static ButtonGroup[][] bg = new ButtonGroup[100][1000];
    static JPanel[] Ronde = new JPanel[100];
    
    static JTabbedPane RondeTabs = new JTabbedPane();
    ...
    
    public void actionPerformed(ActionEvent actie) {
    
    if (actie.getSource() == nieuwrnde) 
    {
    			...
                                      int AantalRondes = SomeIntegerFromAnotherClass; 
    			SetUpPanel(Ronde[AantalRondes],AantalRondes);
    			//code of SetUpPanel beneath, where i get errors.
    			//SetUpPanel is used to add a panel to my JTabbedPane.
    }
    ...
    }
    
    
    public static void SetUpPanel(JPanel p,int i){
    		...
               int a = SomeIntegerFromAnotherClass;
    	WhiteWins[i][a] = new JRadioButton();
    	BlackWins[i][a] = new JRadioButton();
    	Draw[i][a] = new JRadioButton();
    	
    	bg[i][a] = new ButtonGroup();
    	bg[i][a].add(WhiteWins[i][a]);
    	bg[i][a].add(BlackWins[i][a]);
    	bg[i][a].add(Draw[i][a]);
    	
    	p.add(WhiteWins[i][a]);
    	p.add(BlackWins[i][a]);
    	p.add(Draw[i][a]);
    	
    	SetRadioButton(WhiteWins[i][a],600,a*50+100,p);
    	SetRadioButton(BlackWins[i][a],550,a*50+100,p);
    	SetRadioButton(Draw[i][a],500,a*50+100,p);
    /*SetRadioButton is one of the functions i wrote to easily place objects in a panel that works with SpringLayout. I know there are other ways, but this is more easily to adjust ;)*/
    
    	//AND NOW:
    	
               WhiteWins[i][a].addActionListener(this);
    	BlackWins[i][a].addActionListener(this);
    	Draw[i][a].addActionListener(this);
    
               //ERRORS ON "THIS", like i said above...
               //Cannot use "this" in a static context
    	
    }
    }
    Thx! And sorry if any spelling errors, i'm Belgian ;D

    Grtz,
    Timber


    edit: Forgot to say, but everything works perfectly, it's just the actionlistener that won't do it ;D
    Last edited by Questionmark; 08-03-2010 at 02:31 PM. Reason: see bottom
    Never too young to start learning Java :D
    15y. ;)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,008
    Blog Entries
    7
    Rep Power
    20

    Default

    Methods and variables belong to a certain object (of a certain class). You can refer to īthisī in those methods to refer to that particular object. Not so with static variable and methods: they donīt ībelongī to any object, they belong to the class itself and so there isnīt a īthisī.

    kind regards,

    Jos

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Agree with everything Jos has stated above. In addition, there is no reason to be using static variables in this situation, but rather you should be working with instance variables only here.

  4. #4
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Methods and variables belong to a certain object (of a certain class). You can refer to īthisī in those methods to refer to that particular object.
    Ok, This is really helpful ;D i knew all that, but it didn't occur to me that static would make the variable like "not belonging to the class"... :D thx, this is really helpful!

    Quote Originally Posted by JosAH View Post
    Not so with static variable and methods: they donīt ībelongī to any object, they belong to the class itself and so there isnīt a īthisī.
    So this would mean that if I would make the actionlistener static, it'd work?

    static ActionListener MyActListener;

    now i'd have a static Actionlistener, but how can I then implement this actionlistener? and do I have to add it to my frame? :p

    Got to go now, search the solution tomorrow, or maybe late this evening =)


    kind regards,

    Jos[/QUOTE]
    Never too young to start learning Java :D
    15y. ;)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Questionmark View Post
    So this would mean that if I would make the actionlistener static, it'd work?

    static ActionListener MyActListener;

    now i'd have a static Actionlistener, but how can I then implement this actionlistener? and do I have to add it to my frame? :p
    For heaven's sake, no, that's the exact opposite of what you should do! The real solution is to get rid of everything static except for the main method.

  6. #6
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Agree with everything Jos has stated above. In addition, there is no reason to be using static variables in this situation, but rather you should be working with instance variables only here.
    unfortunately, those variables have to be static ;) other classes must be able to edit them...
    Never too young to start learning Java :D
    15y. ;)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Questionmark View Post
    unfortunately, those variables have to be static ;) other classes must be able to edit them...
    Again, no as this is not a reason for something to be static. To allow other classes to interact with fields, you may give your class public mutator methods (or other techniques as the situation dictates). Making these variables static is using a kludge to fix a design issue and will continue sabotage your program if you don't correct it (as you are currently finding out). The solution is to improve your design, and if you need help with your design, then please give us more information about your project and the classes it contains and we'll be happy to help.
    Last edited by Fubarable; 08-03-2010 at 07:48 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    By the way, it appears as if your program attempts to display 300,000 JRadioButtons. Is this correct?
    Java Code:
    public class chesswindow extends JFrame implements ActionListener, WindowListener {
    ...
    public static JRadioButton[][] WhiteWins = new JRadioButton[100][1000];
    public static JRadioButton[][] BlackWins = new JRadioButton[100][1000];
    public static JRadioButton[][] Draw = new JRadioButton[100][1000];
    static ButtonGroup[][] bg = new ButtonGroup[100][1000];
    static JPanel[] Ronde = new JPanel[100];
    Would you not be better off to perhaps use a database to deal with a large amount of data, and then when needed, displaying a reasonable chunk of the data in a JTable?
    Last edited by Fubarable; 08-03-2010 at 07:49 PM.

  9. #9
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    For heaven's sake, no, that's the exact opposite of what you should do! The real solution is to get rid of everything static except for the main method.
    so, it's better to get everything in one class then to have 5 classes that work together? Why? do static variables etc require more memory?
    Never too young to start learning Java :D
    15y. ;)

  10. #10
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    By the way, it appears as if your program attempts to display 300,000 JRadioButtons. Is this correct?
    Would you not be better off to perhaps use a database to deal with a large amount of data, and then when needed, displaying a reasonable chunk of the data in a JTable?
    Yeah i'll have to change that at the end of the project. 300,000 will never be needed. It's a program to pair people in a chessTournament. so, every tab represents one round. in each round, there are +- 50 games to be played. each game has 3 possible outcomes: white wins, black wins, or draw.

    ==> maximum 20 rounds or something, i don't know, maximum 500 players, that'll mean 20 * 250 * 3 radiobuttons... 15,000 will do.

    to test my function to pick the best pairs of all, I need to test until about 80 rounds, to see if there are no bugs, and the maximum amount of players is currently 1000, so that's why I'm using [100][1000]. That'll change afterwards ;)
    Never too young to start learning Java :D
    15y. ;)

  11. #11
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Okey, this is what it roughly looks like;

    There's a class MainProgram, that opens the startupwindow (class startupWindow), loads the saved tournament(class startupWindow), loads the stored variables(class LoadVariables), and then opens the main window(class ChessTournament).

    The class chessTournament is the class that makes the main window, and has the actionlistener. this is where this problem occurs.
    chessTournament has a menuitem 'new player', that opens a new class 'addplayer' ('deelnemer toevoegen' in my language).


    addplayer opens a new window and asks the information of the player.
    it's actually an array of an object in the class chessTournament. so every player has it's own 'addplayer' window. that makes it easier to change data of a player.

    in chessTournament there's also a menuitem 'new round'. it first opens the last class "pair2". this class pairs the players and puts the pairs in static variables in LoadVariables and saves them. then further in the class chessTournament it initializes the new radiobuttons, the new panel ( the new Tab in the tabbedpane ) and uses the function SetUpPanel to place the objects with the static pairs from LoadVariables in the panel, shown above.

    if (actie.getSource() == newRound){
    new pair2();
    ... (initialize variables, not interesting...)
    SetUpPanel(Ronde[AantalRondes],AantalRondes);
    }


    i could post the whole code, but I rather don't; someone offered me a little sum to have this program, don't want everyone to have it for free :p nearly finished so... =/

    but if it has to be done, i will :) guess noone here has intentions to steal code ^^
    Never too young to start learning Java :D
    15y. ;)

  12. #12
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    By the way, how can i give people points for helpful information or something likely? =)
    Never too young to start learning Java :D
    15y. ;)

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Questionmark View Post
    so, it's better to get everything in one class then to have 5 classes that work together? Why? do static variables etc require more memory?
    I can see no where in this thread where anyone has told you to put everything in one class. I think that you're assuming that objects of different classes can't communicate with each other (please correct me if I'm assuming incorrectly), and this is most definitely not true. No, it is much better to subdivide distinct logical entities into distinct classes and use proper OOP techniques so that classes can communicate with each other. I recommend that you get a book that focuses on just OOP techniques alone as it's worth learning, believe me.

    Myself, I'd consider creating several classes including: ChessPlayer (with last name, first name, birthdate, player id, ELO score, ...), GameResult (an enum containing WHITE, BLACK, and DRAW), ChessGame(with two ChessPlayer variables one for white and one for black, a Date variable, and a GameResult variable), a custom table model class that perhaps extends AbstractTableModel and would hold an ArrayList<ChessGame> as the core of the model, a class that overrides JTable and allows for custom renderers and editors, a GUI to hold the JTable and to allow data entry, and a class or two for data persistence, and this would likely interact with a database, especially if I were to create a program that is going to actually be used by someone.
    Last edited by Fubarable; 08-04-2010 at 10:16 PM.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Questionmark View Post
    i could post the whole code, but I rather don't; someone offered me a little sum to have this program, don't want everyone to have it for free :p nearly finished so... =/
    but if it has to be done, i will :) guess noone here has intentions to steal code ^^
    I wouldn't worry about anyone stealing your code, not unless it has been developed by professional Java developers. Most of our small utility apps here are not exactly steal-worthy.
    Last edited by Fubarable; 08-04-2010 at 10:07 PM.

  15. #15
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Yeah I already knew that classes can communicate without making an awful lot of static variables, I was just interpreting incorrectly what you said, my fault ;)

    you know, what you are telling me to do is actually what I have right now. The table hasn't been finished yet, the public class ChessPlayer has exactly the variables you just named, ... the only difference is that there isn't (yet) a class for the chessgames, maybe it'd be better to make one, thanks!

    But now back to the actionlistener, in fact, I still am having this problem... =( is it because my Radiobuttons are still static? or because the actionlistener is static and not given a name? (implemented JActionListener but haven't given it a name)
    Never too young to start learning Java :D
    15y. ;)

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    It would be better not to have any static members. Have you tried removing the static and using object reference variables?

  17. #17
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    It would be better not to have any static members. Have you tried removing the static and using object reference variables?
    not yet, but i don't understand why sometimes the actionlisteners work with static's and why sometimes not. i'll remove the static's this afternoon.
    Never too young to start learning Java :D
    15y. ;)

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,008
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Questionmark View Post
    not yet, but i don't understand why sometimes the actionlisteners work with static's and why sometimes not. i'll remove the static's this afternoon.
    You're not thinking in an object oriented way yet; SmallTalk does a much better job than Java does: in SmallTalk every scenario goes like: an actor performs an action on an actee where the actee can be zero or even plural, so: "the man (actor) eats (action) an apple (actee)", or "the man (actor) walks (action and no actee(s)). The actor and the actee(s) are the objects and the action is a method.

    An ActionListener is an actor or actee (a noun) and it performs some action ( a verb). If you think in those terms there's (almost) no place for static methods because a static methods is an action, optionally acting on actees but there is no actor.

    If you just remove the keyword 'static' from everything most likely it won't work either because there is still no actor in your scenario; you have to invent (or design) one. Designing is easy: just doodle a couple of blobs on paper; each blob is an object so it can be an actor. When you draw little arrows between those blobs, the arrows represent the actions and the blobs to where the arrows point are the actees. First design your entire scenario like that and only then start coding.

    kind regards,

    Jos

  19. #19
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Thx Jos, think i finally got it ;D

    i'll have to make a new object that will perform this command?

    Anyways, thanks a lot, got to go for 10 days, but i think from here i can manage it ;)

    Thx a lot!
    Never too young to start learning Java :D
    15y. ;)

Similar Threads

  1. ActionListener NOT working
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 03:40 PM
  2. Problem w/ ActionListener on a button
    By qwertyuiop23 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2009, 06:25 AM
  3. ActionListener run automatically problem
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:42 PM
  4. ActionListener Error
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 08:36 AM
  5. ActionListener Applet problem
    By xander5511 in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:42 AM

Tags for this Thread

Posting Permissions

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