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

Thread: exchanging 2 Label locations with MouseListener and MouseMotionListen (Drag & Drop)

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    0
    Thanked 6 Times in 2 Posts

    Default exchanging 2 Label locations with MouseListener and MouseMotionListen (Drag & Drop)

    i have tried to make a Drag and Drop for type of Puzzle. Its my school project and im the only one who is stuck with this trouble ...


    it doesnt exchange me the Location of the Labels .. with MouseListener ...

    package proveproject;
    import javax.swing.*;
    import java.awt.*;;
    import java.io.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.*;
     
    public class DPuzzle implements ActionListener{
        JFrame dpFrame;
        JLabel []lbl;
        JLabel []eastLbl;
        JButton solve;
        JComponent a,b;
        int X1,Y1,X2,Y2;
     
     
        DPuzzle(){
            dpFrame = new JFrame("PUZZLE");
            dpFrame.setDefaultCloseOperation(dpFrame.EXIT_ON_CLOSE);
            dpFrame.setBounds(50,50,1200,500);
            dpFrame.setLayout(new BorderLayout());
     
     
            JPanel centerPane = new JPanel();
            centerPane.setLayout(new GridLayout(3,8,3,3));
            JPanel southPane = new JPanel();
     
     
            //Aggiungo  le immagini
            ImageIcon []img;
            img = new ImageIcon[9];
            img[0] = new ImageIcon("1.jpg");
            img[1] = new ImageIcon("2.jpg");
            img[2] = new ImageIcon("3.jpg");
            img[3] = new ImageIcon("4.jpg");
            img[4] = new ImageIcon("5.jpg");
            img[5] = new ImageIcon("6.jpg");
            img[6] = new ImageIcon("7.jpg");
            img[7] = new ImageIcon("8.jpg");
            img[8] = new ImageIcon("9.jpg"); 
     
     
            //metto le immagini nelle label
            lbl = new JLabel[9];
            for (int i = 0; i < 9; i++) {
                lbl[i]= new JLabel("");
                lbl[i].addMouseMotionListener(new DragMouseAdapter());
                lbl[i].addMouseListener(new DragMouseAdapter());
            }
     
     
            lbl[0].setIcon(img[7]);
            lbl[1].setIcon(img[4]);
            lbl[2].setIcon(img[1]);
            lbl[3].setIcon(img[3]);
            lbl[4].setIcon(img[6]);
            lbl[5].setIcon(img[2]);
            lbl[6].setIcon(img[8]);
            lbl[7].setIcon(img[5]);
            lbl[8].setIcon(img[0]);
     
     
     
            //Finisco di mettere le label nel WestPane
     
     
            //Inizio mettere le immagini nelle Label East 
            ImageIcon whiteImg = new ImageIcon("whiteImg.jpg");
            eastLbl = new JLabel[9];
     
     
            for (int i = 0; i < 9; i++) {
                eastLbl[i] = new JLabel("");
                eastLbl[i].setIcon(whiteImg);
                eastLbl[i].addMouseMotionListener(new DragMouseAdapter());
                eastLbl[i].addMouseListener(new DragMouseAdapter());
            }
     
            //Fine Pannello Ovest
     
     
            //Creare pannello sud 
            solve = new JButton("Fatto");
            solve.addActionListener(this);
            southPane.add(solve);
     
     
     
     
            centerPane.add(lbl[0]);
            centerPane.add(lbl[1]);
            centerPane.add(lbl[2]);
            centerPane.add(new JLabel(""));
            centerPane.add(new JLabel(""));
            centerPane.add(eastLbl[0]);
            centerPane.add(eastLbl[1]);
            centerPane.add(eastLbl[2]);
            centerPane.add(lbl[3]);
            centerPane.add(lbl[4]);
            centerPane.add(lbl[5]);
            centerPane.add(new JLabel(""));
            centerPane.add(new JLabel(""));
            centerPane.add(eastLbl[3]);
            centerPane.add(eastLbl[4]);
            centerPane.add(eastLbl[5]);
            centerPane.add(lbl[6]);
            centerPane.add(lbl[7]);
            centerPane.add(lbl[8]);
            centerPane.add(new JLabel(""));
            centerPane.add(new JLabel(""));
            centerPane.add(eastLbl[6]);
            centerPane.add(eastLbl[7]);
            centerPane.add(eastLbl[8]);
     
     
            dpFrame.add(centerPane,BorderLayout.CENTER);
            dpFrame.add(southPane,BorderLayout.SOUTH);
            dpFrame.setVisible(true);
        }
     
        @Override
        public void actionPerformed(ActionEvent ae) {
     
        }
     
        //MouseListener
     
     
     
        //MouseMotionListener
        public class DragMouseAdapter implements MouseMotionListener,MouseListener{
     
            @Override
            public void mouseDragged(MouseEvent me) {
                a =(JComponent)me.getSource();
                X1 = a.getX();
                Y1 = a.getY();
            }
     
            @Override
            public void mouseMoved(MouseEvent me) {
     
            }
     
            @Override
            public void mouseClicked(MouseEvent me) {
                a.setLocation(X2, Y2);
                b.setLocation(X1, Y1);
            }
     
            @Override
            public void mousePressed(MouseEvent me) {
     
            }
            @Override
            public void mouseReleased(MouseEvent me) {
                b = (JComponent)me.getSource();
                X2 = b.getX();
                Y2 = b.getY();
            }
     
            @Override
            public void mouseEntered(MouseEvent me) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
     
            @Override
            public void mouseExited(MouseEvent me) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        }
     
     
     
     
        public static void main(String[] args) {
            new DPuzzle();
        }
     
    }



    Is there anybody who can help me doing the exchange of the JLabels Location???


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: exchanging 2 Label locations with MouseListener and MouseMotionListen (Drag & Drop)

    Why are you trying to exchange the locations of the JLabels?
    Why not exchange the contents of the JLabels?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    0
    Thanked 6 Times in 2 Posts

    Default Re: exchanging 2 Label locations with MouseListener and MouseMotionListen (Drag & Drop)

    how can i do that ?
    can you explain me with an example ?
    sorry but im at start ...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: exchanging 2 Label locations with MouseListener and MouseMotionListen (Drag & Drop)

    What do you want to change in the display of the labels in the GUI?
    Use the standard code for swapping the contents of two variables.
    Something like this:
    tempVar = var1's value
    var1 = var2's value
    var2 = tempVar

    You will have to use a get method to get the value from the JLabel and a set method to set the value of a JLabel
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    12
    Thanks
    0
    Thanked 6 Times in 2 Posts

    Default Re: exchanging 2 Label locations with MouseListener and MouseMotionListen (Drag & Drop)

    i have done a thing like that .. but it doesnt works ..

     public class DragMouseAdapter implements MouseListener{
            JLabel templbl,lbl1,lbl2;
     
     
            @Override
            public void mouseClicked(MouseEvent me) {
            }
     
            @Override
            public void mousePressed(MouseEvent me) {
                //a =(JComponent)me.getSource();
                lbl1 = (JLabel)(JComponent)me.getSource();
            }
            @Override
            public void mouseReleased(MouseEvent me) {
                lbl2 = (JLabel)(JComponent)me.getSource();
     
                templbl.setIcon(lbl1.getIcon());
                lbl1.setIcon(lbl2.getIcon());
                lbl2.setIcon(templbl.getIcon());
            }
     
            @Override
            public void mouseEntered(MouseEvent me) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
     
            @Override
            public void mouseExited(MouseEvent me) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: exchanging 2 Label locations with MouseListener and MouseMotionListen (Drag & Drop)

    it doesnt work
    What does "doesnt work" mean?
    Please explain what the code does and what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: exchanging 2 Label locations with MouseListener and MouseMotionListen (Drag & Drop)

            @Override
            public void mouseEntered(MouseEvent me) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
     
            @Override
            public void mouseExited(MouseEvent me) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    I doubt you will ever get a mouse click if you throw an exception every time the mouse moves over top of the window you want to click.

Similar Threads

  1. Drag and Drop in JTrees
    By helloworld922 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 21st, 2014, 11:16 AM
  2. Drag & drop MVC: deleting an object
    By Alice in forum Object Oriented Programming
    Replies: 0
    Last Post: December 9th, 2010, 11:09 PM
  3. Looking for help with Walter Zorn's Drag & Drop script
    By yuri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2010, 02:52 AM
  4. Drag and Drop in JTrees
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: February 20th, 2010, 03:52 PM
  5. Drag and Drop in JTrees
    By helloworld922 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 19th, 2010, 11:51 PM