Results 1 to 10 of 10
  1. #1
    Cleite is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default [SOLVED] Drag buttons

    I've just started making a board game simulator in Java, and I want to make it possible to drag pieces from one place to another. I don't know if JButtons or JToggleButtons are the best for the job, but that's what I came up with, and it's the only thing I think I can handle anyways. But I don't know how to handle mouse dragging combined with buttons, so I was wondering if anyone could help me with this little SSCCE:
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import java.awt.BorderLayout;
    
    public class Example extends JFrame{
        JPanel panel;
        JButton one, two, three;
    
        public static void main(String[] args) {
    	new Example();
        }
    
        public Example() {
    	super("Example");
    
    	panel = new JPanel(new BorderLayout());
    
    	one = new JButton("Press here");
    	panel.add(one, BorderLayout.WEST);
    
    	two = new JButton("Don't release here");
    	panel.add(two, BorderLayout.CENTER);
    
    	three = new JButton("But release here");
    	panel.add(three, BorderLayout.EAST);
    
    	panel.add(new JLabel("Press-hold-drag-and-release example"),
    		  BorderLayout.NORTH);
    
    	add(panel);
    
    	setVisible(true);
    	pack();
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    }
    How can the code be extended to for instance print "Congratulations" if you manage to drag correctly? Or are there easier options for handling board game pieces?

  2. #2
    FaxMaxo's Avatar
    FaxMaxo is offline Member
    Join Date
    Apr 2009
    Location
    Sweden
    Posts
    3
    Rep Power
    0

    Default New virus?

    Hello all!

    At me something happened with the computer. Looked a video on youtube.com and suddenly has appeared one more sound. As advertising.
    I have switched off IE7 but the sound proceeds! It can a virus new?
    Prompt or help me, pls!

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  4. #4
    Cleite is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    I did allready, and I couldn't figure out how to use what was there to decide what JButton I'm over when I release the mouse button. Is the solution to have a listener/adapter on the first button to listen for pressing mouse button and one on the third button to listen for releasing over there? Or is it possible to listen to the entire drag and when the drag stops, decide which button I'm over?

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    It's probably best to create your own class to use as the pieces. A subclass of JPanel should work. Then you don't have to worry about button semantics getting in the way when you don't need them.

    If you put the mouse listeners in the containing JPanel, you can use getComponentAt() etc. to keep track of everything and you'll get the same coordinate space for all your pieces.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    Cleite is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Then I will try that. I'll get back here if it doesn't work. Thanks, OrangeDog and mtyoung. Now, OrangeDog, how do I mark it as [solved]?

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Go to Thread Tools at the top of the thread. General forum instructions are under FAQ on the sidebar (for some reason).
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  8. #8
    Cleite is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Thanks again >.<

  9. #9
    FaxMaxo's Avatar
    FaxMaxo is offline Member
    Join Date
    Apr 2009
    Location
    Sweden
    Posts
    3
    Rep Power
    0

    Default New virus?

    Hello all!

    At me something happened with the computer. Looked a video on youtube.com and suddenly has appeared one more sound. As advertising.
    I have switched off IE7 but the sound proceeds! It can a virus new?
    Prompt or help me, pls!

  10. #10
    FaxMaxo's Avatar
    FaxMaxo is offline Member
    Join Date
    Apr 2009
    Location
    Sweden
    Posts
    3
    Rep Power
    0

    Default New virus?

    Hello all!

    At me something happened with the computer. Looked a video on youtube.com and suddenly has appeared one more sound. As advertising.
    I have switched off IE7 but the sound proceeds! It can a virus new?
    Prompt or help me, pls!

Similar Threads

  1. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 03:04 PM
  2. drag-drop in applet.
    By makpandian in forum Java Applets
    Replies: 0
    Last Post: 12-11-2008, 08:36 AM
  3. Simple DND (Drag and Drop) Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:37 PM
  4. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 02:10 PM
  5. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 10:19 PM

Posting Permissions

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