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.

Page 3 of 8 FirstFirst 12345 ... LastLast
Results 51 to 75 of 192

Thread: drag and drop word to match image

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

    Default Re: drag and drop word to match image

    can i get an example of some sort
    Look at the code in post#11.
    Where does it get the String it appends to the currently displayed Strings?
    There are several references to a reject method. Add code to call the reject method if the String that is received is the one to be rejected. Otherwise do the append as the code currently does.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #52
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    i dont get it.. im confused

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

    Default Re: drag and drop word to match image

    Which part don't you understand? I'll list them one at a time so that you don't confuse them:
    Where does the target class object get the dragged String from?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #54
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    Quote Originally Posted by Norm View Post
    Which part don't you understand? I'll list them one at a time so that you don't confuse them:
    Where does the target class object get the dragged String from?
    DraggableLabel

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

    Default Re: drag and drop word to match image

    How does the DropTargetTextArea class get the String that was dropped on it?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #56
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: drag and drop word to match image

    This has become much more painful than it needs to be. I suggest you do this:

    Edit: Oops, I forgot to mention the starting point: the code at post #11.

    1. Update the existing code to use Swing components, JTextArea, JLabel, etc. While the code you have may ultimately work (not sure), the recommendation is to use Swing and then to not mix AWT and Swing components.
    2. Add a setPreferredSize() to the DropTargetTextArea in the constructor. I used 200 x 200, but suit yourself.
    3. After the code has been updated, copy the existing class DropTargetTextArea and paste it into the Example class at the same level.
    4. Rename the pasted code DropTargetJLabel, creating a new class. Modify it to extend JLabel and leave implements DropTargetListener.
    5. In the new class, fix the name of the constructor.
    6. Update your main() method to:
        public static void main( String[] args )
        {
            // Create the main container and its contents
            JFrame frame = new JFrame( "Test" );
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            JPanel panel = new JPanel();
            JTextArea textArea = new DropTargetTextArea();
            JLabel jLabel = new DropTargetJLabel();
     
            JLabel testLabel = new DraggableLabel( "test" );
            JLabel testingLabel = new DraggableLabel( "testing" );
     
            // construct the final GUI
            panel.add( testLabel );
            panel.add( testingLabel );
            frame.add( textArea, BorderLayout.WEST );
            frame.add( jLabel, BorderLayout.EAST );
            frame.add( panel, BorderLayout.SOUTH );
     
            // pack the frame and set it visible
            frame.pack();
            frame.setVisible( true );
        }
    7. Update the imports.

    Let us know if you have any questions.

    In the future, please don't be stymied by "I see red squigglies." They are there to help you move forward, not send you backwards. If you need help with them but don't feel like typing them out, you can hover over them, mouse into the resulting detail, select the text, <CTRL-C> to copy, and then paste the text directly into a post.

    Good luck. Let us know if you have remaining questions.

  7. #57
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    Quote Originally Posted by Norm View Post
    How does the DropTargetTextArea class get the String that was dropped on it?
    n
    Last edited by stresstedout; March 4th, 2014 at 01:37 AM.

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

    Default Re: drag and drop word to match image

    Where is there a String in that code?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #59
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    Quote Originally Posted by Norm View Post
    Where is there a String in that code?
    sorry i pasted the wrong code updated it and here is the string
    Last edited by stresstedout; March 4th, 2014 at 01:37 AM.

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

    Default Re: drag and drop word to match image

    Ok, that looks like it.

    Now add code to test what is in the String and to either accept it or reject it. The earlier code had two Strings to Drag: "test" and "testing". Chose one to accept and reject the other. Compile and test the code to see if the logic works.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #61
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    so at the moment it drags the text and drops in to the label which has been defined near the beginning of the code.

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

    Default Re: drag and drop word to match image

    Did you change the code to reject one of the Drops? Does it work? The DnD from one label should be accepted and the DnD from the other label rejected.

    the label which has been defined
    You should use the variable name so there is no confusion.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #63
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    not sure how to do this.

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

    Default Re: drag and drop word to match image

    Look at the code. There are statements to accept and to reject the drop.

    Use an if statement to compare the Dropped String against the one to be accepted.
    if (String matches) {
       accept
    }else{
       reject
    }
    If you don't understand my answer, don't ignore it, ask a question.

  15. #65
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

     
    try {
     
        Transferable transferable = evt.getTransferable();
     
        if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
     
      evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
     
     
    }
    Last edited by stresstedout; March 4th, 2014 at 01:37 AM.

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

    Default Re: drag and drop word to match image

    What is that for?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #67
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    reject the drop... i dont know what im doing

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

    Default Re: drag and drop word to match image

    I originally said that DnD was complicated and you said: i know how to do drag and drop
    I'd suggest that you study the tutorial on DnD using the link in post#2
    If you don't understand my answer, don't ignore it, ask a question.

  19. #69
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    ive only really worked on drag and drop no reject drops.

    Please can you explain to me, i dont have much time

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

    Default Re: drag and drop word to match image

    Look back over the last posts. It's mostly explained there.

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

  21. #71
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    please help me

    --- Update ---

    please please please please someone help me

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

    Default Re: drag and drop word to match image

    Choose one of the Strings that are dropped on the target to be accepted. The earlier code had two Strings: "test" and "testng". Write an if statement that tests if the dropped String is the one to be accepted and print out a message: "accepted" if the String matches and print out "rejected" if the String does not match.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #73
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    Quote Originally Posted by Norm View Post
    Choose one of the Strings that are dropped on the target to be accepted. The earlier code had two Strings: "test" and "testng". Write an if statement that tests if the dropped String is the one to be accepted and print out a message: "accepted" if the String matches and print out "rejected" if the String does not match.
    is this correct??? if not please help me
    Last edited by stresstedout; March 4th, 2014 at 01:37 AM.

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

    Default Re: drag and drop word to match image

    Where does the code compare the dropped String against the String that is to be accepted?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #75
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drag and drop word to match image

    i dont think it does.

Page 3 of 8 FirstFirst 12345 ... LastLast

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. [SOLVED] Drag and drop in MVC
    By Alice in forum Object Oriented Programming
    Replies: 9
    Last Post: December 9th, 2010, 10:16 PM
  3. Drag and Drop in JTrees
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: February 20th, 2010, 03:52 PM
  4. Drag and Drop in JTrees
    By helloworld922 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 19th, 2010, 11:51 PM
  5. 'MouseUp' Drag and Drop Events
    By copeg in forum AWT / Java Swing
    Replies: 0
    Last Post: November 10th, 2009, 07:21 PM