Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JButton event handler

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation JButton event handler

    //the code works here
    [COLOR="#FF0000"]currentPlayersOnTile[players[1].getPosition()].add(players[1].getPlayerIcon());[/COLOR]
     
    rollDice.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //but doesn't work here
     
            int roll;
            //get roll
            roll = dice.getRoll());
     
            //adjust player's position
            players[1].movePlayer(roll);
     
            //get player 1's icon and add it to currentPlayersOnTile of index player's position
            [COLOR="#FF0000"]currentPlayersOnTile[players[1].getPosition()].add(players[1].getPlayerIcon());[/COLOR]
        }
    });

    Could anyone possibly shed some light on this? It's puzzled me for a number of hours now and i need an answer fairly urgently if at all possible. I have also checked it's getting into the handler etc but the line (currentPlayersOnTile[players[1].getPosition()].add(players[1].getPlayerIcon()); ) doesn't seem to execute inside the handler for some reason. Seemingly because its in the addActionListener method. Any help would be appreciated.

    Thanks Graham
    Last edited by gkelly642; April 3rd, 2013 at 08:18 PM. Reason: Posted a smiley face instead of the brackets


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: JButton event handler

    doesn't work here
    Can you explain what "doesn't work" means?


    Try debugging the code by breaking the long chain of methods into single statements and checking that each one is working.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JButton event handler

    Sorry, realised just after I posted the thread that you had to enclose code in the tags (which I have now done).
    currentPlayersOnTile[players[1].getPosition()].add(players[1].getPlayerIcon());

    The above line of code basically gets the current players position on the board and moves an icon(which is a coloured JPanel) to the relevant JPanel which represents a tile on the board. It works outside of the addActionListener method but it doesn't do anything at all inside it. I have debugged it the line is executed but nothing happens. Strangest thing i've seen coding, no error or anything just doesn't do anything.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: JButton event handler

    but nothing happens
    Try debugging the code by breaking the long chain of methods into single statements and checking that each one is working.
    Which method in that chain is not doing what is expected?

    I'm done for tonight.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Separate Event Handler Class
    By beer-in-box in forum AWT / Java Swing
    Replies: 2
    Last Post: April 1st, 2013, 09:19 AM
  2. Replies: 0
    Last Post: June 19th, 2012, 12:58 PM
  3. JButton event handler
    By Jsri in forum AWT / Java Swing
    Replies: 1
    Last Post: October 25th, 2011, 07:02 AM
  4. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM
  5. JButton event handling.
    By Prof in forum AWT / Java Swing
    Replies: 6
    Last Post: February 3rd, 2010, 09:29 AM

Tags for this Thread