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

Thread: Desktop shortcut drag and drop

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Desktop shortcut drag and drop

    As part of a project, i want to make a panel where i can drag shortcuts from the desktop onto the panel and have them retain their functionality in the panel (they can still be clicked to open their intended program). I'm quite new to java programming, and while i've been googling for ways to do this, i haven't been very succesfull in finding good desriptions. I'm using netbeans, and my intent so far has been to use a jpanel from netbeans and make it drag and drop capable and add a snap grid to it.

    Does someone have any suggestions on how i can do this? or if it's even something a beginner to java should be trying to do as one of his first projects.

    I'm open to suggestions, if for example it would be a better idea not to try and use the preset jpanel or anything of the like.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Desktop shortcut drag and drop

    I wonder what the java program would receive when a file is dragged from the desktop into the program.
    If the java program got the path to the file, it could read the contents of the file and perhaps build an image using file's icon and a command line that could be used with the Process and Runtime classes.
    Have you tried writing the drop receiving code to see what the java program gets?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Desktop shortcut drag and drop

    What's your end goal with this?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    Quote Originally Posted by KevinWorkman View Post
    What's your end goal with this?
    the end goal is to have a desktop application, somewhat like rainmeter or stardock, where you have a panel you can drop shortcuts into, and then i'll add some features to it along the way.

    #2
    no i haven't tried the drop command, i do not know how to connect it with the standard jpanel from netbeans, i thought there should be premade drop events to chose from, but i can't find any.
    Last edited by dragolis; April 18th, 2012 at 09:36 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Desktop shortcut drag and drop

    Have you looked at the tutorials? There are some simple examples there you could experiment with.
    Lesson: Drag and Drop and Data Transfer (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    I have been looking at that guide before, but i guess i'll take another look, i'm just unsure how to implement the methods with a premade jpanel, as they are write protected, and as such i'd think i had to put it into an event of some kind, but mouseReleased, which is closest i can find in Netbeans, doesn't really seem like the right event for this.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Desktop shortcut drag and drop

    I'd suggest you take one of the example programs from the tutorial and play with it to see how DnD works.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    I got the DnD working, but now i need a way to get a filename from a string. I'm assuming i need to use String.split, but since the file paths are written with "\" (C:\dir\dir\dir\file.exe) i need to split with "\" which i get told is an "unclosed string literal" when i try to do this:

    System.out.println(java.util.Arrays.toString(tem.s plit("\") //tem is the string with the filepath

    I don't know how to get around this;/

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Desktop shortcut drag and drop

    Escape the escape character.

    System.out.println("\\");
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Desktop shortcut drag and drop

    You need to escape (tell the compiler to ignore) the \ which is the escape character: "\\"
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Desktop shortcut drag and drop

    Or you could just create a new File instance and use the getName() function. Probably better as it's more platform independent than using a hard-coded character that might change for different systems.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    dragolis (April 30th, 2012)

  13. #12
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    i got .getName to work. thx.
    Last edited by dragolis; April 30th, 2012 at 08:00 AM.

  14. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Desktop shortcut drag and drop

    I recommend you check out the API and the basic tutorials, which incidentally are the first and second respective results for googling "java file".
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #14
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    My program is about finished now, one things i bugging me though, i have added some JButton's to make varius options, and the JButtons work. The things is that they react to mouseovers which leaves them in a selected state, i've tried setFocusable(false) to no effect, and google hasn't been forthcoming either;/ Any suggestions on how make the JButton's ignore mouseovers or just revert back to an unselected state after the mouseover?

    Capture.PNG

  16. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Desktop shortcut drag and drop

    Can you provide an SSCCE that demonstrates this problem? JButtons don't do that for me by default.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #16
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    if you run this and just moves the mouse over the 4 jbuttons you'll see that they get "selected" and keep getting darker and darker the more times you drag the mouse over them

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Test extends JFrame {
     
        JPanel ta;
     
        public static void main(String[] args) {
            new Test();
        }
     
        public Test() {
            setSize(300, 300);
            setUndecorated(true);
            setBackground(new Color(0, 0, 0, 0));        
            ta = new JPanel();
            ta.setLayout(new GridLayout(2, 4));
            ta.setBackground(new Color(0, 0, 0, 10));
            getContentPane().add(ta, BorderLayout.CENTER);
            JButton q = new JButton();
            JButton q1 = new JButton();
            JButton q2 = new JButton();
            JButton q3 = new JButton();
            q.setBackground(new Color(0, 0, 0, 10));
            q1.setBackground(new Color(0, 0, 0, 10));
            q2.setBackground(new Color(0, 0, 0, 10));
            q3.setBackground(new Color(0, 0, 0, 10));
            ta.add(q);
            ta.add(q1);
            ta.add(q2);
            ta.add(q3);
            setVisible(true);
            repaint();
        }
    }
    Last edited by dragolis; May 4th, 2012 at 10:45 AM.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Desktop shortcut drag and drop

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    How do you test with this code to see the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    i remade the previus post.

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Desktop shortcut drag and drop

    How do you test with this code to see the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #20
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    i wrote that above the java code
    if you run this and just moves the mouse over the 4 jbuttons you'll see that they get "selected" and keep getting darker and darker the more times you drag the mouse over them

  22. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Desktop shortcut drag and drop

    The color is almost black now. I don't see any permanent color changes when I move the mouse over the buttons.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #22
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    when i move the mouse over it, this happens:
    Capt123ure.jpgCapt321ure.jpg23213Capture.jpg

    in the first picture i've only touched the upper left jbutton, and then in the other pictures i've moved the mouse a bit in and out of them.

  24. #23
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,387
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Desktop shortcut drag and drop

    I think I see what you mean, and I believe it has to do with specifying an alpha value for the background color of the JButton. Every time you mouse over it, the alpha value gets a little darker. I've shortened your example to make it more obvious:


    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
    public class Test extends JFrame {
     
        public static void main(String[] args) {
            new Test();
        }
     
        public Test() {
            JButton button = new JButton("Mouse Over Me");
            button.setBackground(new Color(0, 255, 0, 10));
            add(button);
     
            add(new JLabel("move mouse back and forth between here and button"), BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }

    I'm looking into it, but in the mean time, somebody smarter than me might know why this happens...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  25. #24
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Desktop shortcut drag and drop

    I fixed it with a timer on repaint

            int delay = 1000;
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              repaint();
          }
      };

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] Question: Drag and drop tut.
    By PierreD in forum AWT / Java Swing
    Replies: 2
    Last Post: June 6th, 2011, 07:24 AM
  3. [SOLVED] Drag and drop in MVC
    By Alice in forum Object Oriented Programming
    Replies: 9
    Last Post: December 9th, 2010, 09:16 PM
  4. Drag and Drop in JTrees
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: February 20th, 2010, 02:52 PM
  5. Drag and Drop in JTrees
    By helloworld922 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 19th, 2010, 10:51 PM

Tags for this Thread