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 16 of 16

Thread: MouseReleased event not being called

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default MouseReleased event not being called

    Hi,

    i am trying to make a chess game and in order to do that I'm using a drag and drop method to move the characters.

    I have been able to implement a mouse adapter to drag and drop the images. I am now trying to add a mouseReleased function where i can then check if the player has made a valid move.

    When i run the program i am able to get the mousePressed function to work but the mouseReleased function is only called sometimes on a quick click and never after a drag.

    button.addMouseListener(new MouseAdapter(){ //function that drags and drops the icons
        @Override
        public void mousePressed(MouseEvent e){
            JButtonExt previousSquare = (JButtonExt)e.getSource();
            TransferHandler handle = previousSquare.getTransferHandler();
            handle.exportAsDrag(previousSquare, e, TransferHandler.COPY);
            System.out.println(previousSquare.location[0] + " " + previousSquare.location[1]);
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println("test");
        }
    });

    any help would be appreciated


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MouseReleased event not being called

    That looks like it should work. Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: MouseReleased event not being called

    What is type of object is button? Are you moving the mouse outside of button and then releasing? My initial thought is that the focus might be changing.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: MouseReleased event not being called

    @aussiemcgr the button is an extension of JButton where i have added a couple of extra properties that i can use in other parts of the game. I am dragging the image from one button and then dropping it on another one.

    @norm the problem is that i can't get the program to print "test" after dragging the image from one button and onto another. but it will output "test" when i just click a single button and release it.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MouseReleased event not being called

    Is another method "eating" the release? The drag and drop methods would use the release to know when the drop has happened.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: MouseReleased event not being called

    Have you implemented this on all of your buttons, or just one? If just one, then I suspect the focus is changing from the button you were hovering over when you pressed the mouse to the button you were hovering over when you released it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: MouseReleased event not being called

    after testing another program i think the new button isn't gaining focus and so the mouseReleased isn't being called. Is there a simple way of allowing the new button to gain the focus? maybe using something like mouseEntered?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MouseReleased event not being called

    If you get a mouse pressed event, I'd expect you'd get the matching mouse released event.

    Can you make a small testing program that compiles, executes and show the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: MouseReleased event not being called

    what exactly do you want in the test program? would you like me to upload the whole package?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MouseReleased event not being called

    A small program that compiles, executes and show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: MouseReleased event not being called

    sorry for the late reply

     
    import javax.swing.*;
    import java.util.ArrayList;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
     
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Mitch
     */
     
    enum PieceType {PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING};
     
    enum PieceColour {BLANK, BLACK, WHITE};
    public class ChessBoard extends JFrame {
     
     
     
     
        private Container c;
        private JButtonExt[][] chessBoard = new JButtonExt[8][8];
        private Piece[][] players = new Piece[2][16];
        private Piece player;
     
        public static void main(String[] args) {
            ChessBoard window = new ChessBoard();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
        }
     
        class buttonClick implements ActionListener {    // Listen class to launch function for close menu item
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("click");
            }
        }
     
        public ChessBoard() {
     
            c = getContentPane();
     
            //create buttons for the game board
            //buttons will be able to drag and drop images
            for (int x=0; x<8; x++) {
                for (int y=0; y<8; y++) {
                    int g = x+y;
                    JButtonExt label;
     
                    if (g%2==0) {//white squares
                        label = new JButtonExt(PieceColour.WHITE, new int[]{x,y});
                    }
                    else {//black square
                        label = new JButtonExt(PieceColour.BLACK, new int[]{x,y});
                    }
     
     
     
     
                    //System.out.println(label.getIcon());
     
                    TransferHandler transfer = new TransferHandler("icon");
     
     
                    if (g % 2 == 0) { //white square
                        label.setFont(new java.awt.Font("Dialog", Font.BOLD, 14));
                    }
                    else { //black squares
     
                    }
     
                    //set the location and size of each button depending on where they are located on the board
                    label.setLocation((y*60)+60, (x*60)+60);
                    label.setSize(60, 60);
     
     
                    label.addActionListener(new ChessBoard.buttonClick()); //allow the user to click on a square and show available moves
                    label.setTransferHandler(transfer); //allow the button to accept drag and drop functions
                    label.addMouseListener(new MouseAdapter(){ //function that drags and drops the icons
                        @Override
                        public void mousePressed(MouseEvent e){
                            JButtonExt previousSquare = (JButtonExt)e.getSource();
                            TransferHandler handle = previousSquare.getTransferHandler();
                            handle.exportAsDrag(previousSquare, e, TransferHandler.COPY);
                            System.out.println(previousSquare.location[0] + " " + previousSquare.location[1]);
     
     
                        }
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            System.out.println("test");
                        }
                    });
     
     
                    chessBoard[x][y] = label;
                }
            }
     
            for(JButtonExt a[] : chessBoard) {
                for (JButtonExt button: a) {
                    int x = button.location[0];
                    int y = button.location[1];
     
                    String col;
                    if (button.defaultColour == PieceColour.BLACK) {
                        col = "Dark";
                    } else {
                        col = "Light";
                    }
     
                    if (x == 1) {
                        ImageIcon icon = new ImageIcon("images/whitePawn" + col + ".jpg");
                        button.setIcon(icon);
     
                    }
                    else if (x == 6) {
                        ImageIcon icon = new ImageIcon("images/blackPawn" + col + ".jpg");
                        button.setIcon(icon);
                    }
                    else if (x == 0) {
                        if (y == 0 || y == 7) {
                            ImageIcon icon = new ImageIcon("images/whiteRook" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 1 || y == 6) {
                            ImageIcon icon = new ImageIcon("images/whiteBishop" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 2 || y == 5) {
                            ImageIcon icon = new ImageIcon("images/whiteKnight" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 3) {
                            ImageIcon icon = new ImageIcon("images/whiteQueen" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else {
                            ImageIcon icon = new ImageIcon("images/whiteKing" + col + ".jpg");
                            button.setIcon(icon);
                        }
                    }
                    else if (x == 7) {
                     if (y == 0 || y == 7) {
                            ImageIcon icon = new ImageIcon("images/blackRook" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 1 || y == 6) {
                            ImageIcon icon = new ImageIcon("images/blackBishop" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 2 || y == 5) {
                            ImageIcon icon = new ImageIcon("images/blackKnight" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 3) {
                            ImageIcon icon = new ImageIcon("images/blackQueen" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else {
                            ImageIcon icon = new ImageIcon("images/blackKing" + col + ".jpg");
                            button.setIcon(icon);
                        }
                    }
                    else {
                        ImageIcon icon = new ImageIcon("images/"+ col + ".jpg");
                        button.setIcon(icon);
                    }
                    c.add(button);
                }
            }
     
            JLabel label = new JLabel("");
            c.add(label);
     
            /*for (int h=0; h<2;h++) {
                for (int i=0;i<9;i++) {
                    if (h==1) {
                        players[h][i] = new Piece(PieceType.PAWN, PieceColour.WHITE, {h,i}, "whitePawn");
                    }
                }
     
            }*/
     
            this.setTitle("Chess");
            this.setContentPane(c);
            this.setBounds(0, 0, 650, 650);
            this.setLocationRelativeTo(null);
        }
    }

    this code should compile

    the program should show the location of the square you clicked on, in the command line, and then the location of the square you released the mouse on. However the last part isn't working as i want it to.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MouseReleased event not being called

    The posted code does not compile. It's missing some class definitions.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: MouseReleased event not being called

     
     
    import javax.swing.*;
    import java.util.ArrayList;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
     
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Mitch
     */
     
     
    import javax.swing.*;
    import java.awt.event.*;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Mitch
     */
     
     
    class JButtonExt extends JButton {
        PieceColour holdsColour;
        PieceColour defaultColour;
        int[] location;
     
        public JButtonExt(PieceColour newColour, int[] newLocation) {
            this.location = newLocation;
            this.defaultColour = newColour;
            this.holdsColour = null;
        }
    }
     
    enum PieceType {PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING};
     
    enum PieceColour {BLANK, BLACK, WHITE};
    class ChessBoard extends JFrame {
     
     
     
     
        private Container c;
        private JButtonExt[][] chessBoard = new JButtonExt[8][8];
     
        public static void main(String[] args) {
            ChessBoard window = new ChessBoard();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
        }
     
        class buttonClick implements ActionListener {    // Listen class to launch function for close menu item
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("click");
            }
        }
     
        public ChessBoard() {
     
            c = getContentPane();
     
            //create buttons for the game board
            //buttons will be able to drag and drop images
            for (int x=0; x<8; x++) {
                for (int y=0; y<8; y++) {
                    int g = x+y;
                    JButtonExt label;
     
                    if (g%2==0) {//white squares
                        label = new JButtonExt(PieceColour.WHITE, new int[]{x,y});
                    }
                    else {//black square
                        label = new JButtonExt(PieceColour.BLACK, new int[]{x,y});
                    }
     
     
     
     
                    //System.out.println(label.getIcon());
     
                    TransferHandler transfer = new TransferHandler("icon");
     
     
                    if (g % 2 == 0) { //white square
                        label.setFont(new java.awt.Font("Dialog", Font.BOLD, 14));
                    }
                    else { //black squares
     
                    }
     
                    //set the location and size of each button depending on where they are located on the board
                    label.setLocation((y*60)+60, (x*60)+60);
                    label.setSize(60, 60);
     
     
                    label.addActionListener(new ChessBoard.buttonClick()); //allow the user to click on a square and show available moves
                    label.setTransferHandler(transfer); //allow the button to accept drag and drop functions
                    label.addMouseListener(new MouseAdapter(){ //function that drags and drops the icons
                        @Override
                        public void mousePressed(MouseEvent e){
                            JButtonExt previousSquare = (JButtonExt)e.getSource();
                            TransferHandler handle = previousSquare.getTransferHandler();
                            handle.exportAsDrag(previousSquare, e, TransferHandler.COPY);
                            System.out.println(previousSquare.location[0] + " " + previousSquare.location[1]);
     
     
                        }
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            System.out.println("test");
                        }
                    });
     
     
                    chessBoard[x][y] = label;
                }
            }
     
            for(JButtonExt a[] : chessBoard) {
                for (JButtonExt button: a) {
                    int x = button.location[0];
                    int y = button.location[1];
     
                    String col;
                    if (button.defaultColour == PieceColour.BLACK) {
                        col = "Dark";
                    } else {
                        col = "Light";
                    }
     
                    if (x == 1) {
                        ImageIcon icon = new ImageIcon("images/whitePawn" + col + ".jpg");
                        button.setIcon(icon);
     
                    }
                    else if (x == 6) {
                        ImageIcon icon = new ImageIcon("images/blackPawn" + col + ".jpg");
                        button.setIcon(icon);
                    }
                    else if (x == 0) {
                        if (y == 0 || y == 7) {
                            ImageIcon icon = new ImageIcon("images/whiteRook" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 1 || y == 6) {
                            ImageIcon icon = new ImageIcon("images/whiteBishop" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 2 || y == 5) {
                            ImageIcon icon = new ImageIcon("images/whiteKnight" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 3) {
                            ImageIcon icon = new ImageIcon("images/whiteQueen" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else {
                            ImageIcon icon = new ImageIcon("images/whiteKing" + col + ".jpg");
                            button.setIcon(icon);
                        }
                    }
                    else if (x == 7) {
                     if (y == 0 || y == 7) {
                            ImageIcon icon = new ImageIcon("images/blackRook" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 1 || y == 6) {
                            ImageIcon icon = new ImageIcon("images/blackBishop" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 2 || y == 5) {
                            ImageIcon icon = new ImageIcon("images/blackKnight" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else if (y == 3) {
                            ImageIcon icon = new ImageIcon("images/blackQueen" + col + ".jpg");
                            button.setIcon(icon);
                        }
                        else {
                            ImageIcon icon = new ImageIcon("images/blackKing" + col + ".jpg");
                            button.setIcon(icon);
                        }
                    }
                    else {
                        ImageIcon icon = new ImageIcon("images/"+ col + ".jpg");
                        button.setIcon(icon);
                    }
                    c.add(button);
                }
            }
     
            JLabel label = new JLabel("");
            c.add(label);
     
            /*for (int h=0; h<2;h++) {
                for (int i=0;i<9;i++) {
                    if (h==1) {
                        players[h][i] = new Piece(PieceType.PAWN, PieceColour.WHITE, {h,i}, "whitePawn");
                    }
                }
     
            }*/
     
            this.setTitle("Chess");
            this.setContentPane(c);
            this.setBounds(0, 0, 650, 650);
            this.setLocationRelativeTo(null);
        }
    }

    ok this code will compile

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MouseReleased event not being called

    I suspect the DnD code is eating the mouse release. Perhaps the DnD/transfer code has a way to determine the target component.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    huddo (May 7th, 2012)

  16. #15
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: MouseReleased event not being called

    I believe once the DND machinery is activated, MouseEvents are consumed by the DND itself (and your example supports this claim). If you wish to listen for events during a DND, take a look at registering a DropTargetListener, or overriding the appropriate methods in the TransferHandler class (eg exportDone)

  17. The Following User Says Thank You to copeg For This Useful Post:

    huddo (May 7th, 2012)

  18. #16
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: MouseReleased event not being called

    thanks for that. i shall have a play around with the transfer handler

Similar Threads

  1. Integer set in void, is being called upon.
    By tomthebeast in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 13th, 2012, 05:19 AM
  2. [SOLVED] .show is called in card layout, but the frame didn't update
    By nggdowt in forum AWT / Java Swing
    Replies: 2
    Last Post: November 9th, 2011, 02:16 AM
  3. Replies: 1
    Last Post: November 2nd, 2011, 11:00 AM
  4. Replies: 10
    Last Post: July 12th, 2011, 02:27 PM
  5. Replies: 0
    Last Post: February 2nd, 2010, 08:20 AM