JLabel DnD

I found a tutorial on how to get a JLabel to work with drag and drop. I modified the code given to use a JList and a JLabel. I got the JList to be able to drag a list element onto the JLabel and the label display the text. However, if you drag one element, and then drag another element, the first element is replaced.

I want to be able to drag multiple elements onto the JLabel from the JList. Here is my code, can anyone give me a hand?

package components;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

publicclass LabelDnDextends JPanel{

JList list;

JLabel label;

String[] data =new String[]{"Red","Green","Blue"};

public LabelDnD(){

super(new GridLayout(2, 1));

list =new JList(data);

list.setDragEnabled(true);

JPanel tfpanel =new JPanel(new GridLayout(1,1));

TitledBorder t1 = BorderFactory.createTitledBorder("JList");

tfpanel.add(list);

tfpanel.setBorder(t1);

label =new JLabel("I'm a Label!", SwingConstants.LEADING);

label.setTransferHandler(new TransferHandler("text"));

MouseListener listener =new DragMouseAdapter();

label.addMouseListener(listener);

JPanel lpanel =new JPanel(new GridLayout(1,1));

TitledBorder t2 = BorderFactory.createTitledBorder("JLabel");

lpanel.add(label);

lpanel.setBorder(t2);

add(tfpanel);

add(lpanel);

setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

}

privateclass DragMouseAdapterextends MouseAdapter{

publicvoid mousePressed(MouseEvent e){

JComponent c = (JComponent)e.getSource();

TransferHandler handler = c.getTransferHandler();

handler.exportAsDrag(c, e, TransferHandler.COPY);

}

}

privatestaticvoid createAndShowGUI(){

//Create and set up the window.

JFrame frame =new JFrame("LabelDnD");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane =new LabelDnD();

newContentPane.setOpaque(true);

frame.setContentPane(newContentPane);

frame.pack();

frame.setVisible(true);

}

publicstaticvoid main(String[] args){

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowGUI();

}

});

}

}

[4391 byte] By [SidewinderXa] at [2007-11-27 10:36:21]
# 1

Hey WideSinder :-)

You should post this in the Swing forum.

_helloWorld_a at 2007-7-28 18:40:44 > top of Java-index,Java Essentials,Java Programming...
# 2

You've been asked before to post Swing related questions in the Swing forum. I'm not going to remind you every time. I'll just stop answering the questions.

camickra at 2007-7-28 18:40:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> You've been asked before to post Swing related

> questions in the Swing forum. I'm not going to remind

> you every time. I'll just stop answering the

> questions.

It's not like you have answered them.

SidewinderXa at 2007-7-28 18:40:44 > top of Java-index,Java Essentials,Java Programming...
# 4

> It's not like you have answered them.

I answered this one:

http://forum.java.sun.com/thread.jspa?threadID=5193381

Of course you never bothered to reply.

And I answered this one:

http://forum.java.sun.com/thread.jspa?threadID=5193927

Of course I'm not going to answer all your questions. I don't know all the answers.

And since you don't appreciate the answers don't expect much help in the future until your attitude changes.

camickra at 2007-7-28 18:40:44 > top of Java-index,Java Essentials,Java Programming...
# 5

SidewinderX, just a word to the wise. Be careful who you piss off. You'll never know when you'll need their help.

petes1234a at 2007-7-28 18:40:44 > top of Java-index,Java Essentials,Java Programming...
# 6

> > You've been asked before to post Swing related

> > questions in the Swing forum. I'm not going to

> remind

> > you every time. I'll just stop answering the

> > questions.

>

> It's not like you have answered them.

Are you familiar with the term fuck you?

cotton.ma at 2007-7-28 18:40:45 > top of Java-index,Java Essentials,Java Programming...
# 7

The fact I'm being flamed for posting a question in the wrong forum it quite amusing. And you wont answer a question because of it? How childish. Some of you need to learn some netiquette.

p.s. camickr the reason I didnt respond to one [http://forum.java.sun.com/thread.jspa?threadID=5193381] of your posts was because I already had the solution and never knew you replied.

SidewinderXa at 2007-7-28 18:40:45 > top of Java-index,Java Essentials,Java Programming...
# 8

> The fact I'm being flamed for posting a question in

> the wrong forum it quite amusing. And you wont answer

> a question because of it? How childish. Some of you

> need to learn some netiquette.

The fact that you think that not following the etiquette is somehow acceptable is quite amusing. And you won't follow a simple request from those you expect to answer your question? How fucking stupid. You should find your brain because I can't begin to comprehend your logic.

How do you figure that being rude, not following simple etiquette rules and several request to follow them and lying is going to encourage people to help you?

Would you help you? Well bad example I guess, I am sure you would, because you are fucking stupid.

cotton.ma at 2007-7-28 18:40:45 > top of Java-index,Java Essentials,Java Programming...
# 9

> Some of you need to learn some netiquette.

The proper netiquette is to do some work yourself before posting. This includes reading the API and tutorials (which you appear to have done, but if you would have read the whole tutorial you would have found an example of creating your own custom transfer handler to do what you want), but it also involved searching the forum to see if you question has been asked and answered previously.

Since the vast majority of people who have Swing related questions post in the Swing forum, then you have the best chance to find the answer there.

If you don't find an answer then you post a question and we attempt to answer it.

Now the next person who searches the forum has a better chance of finding the answer on a related question.

If all the information is in one place then we don't have to keep repeating ourselves in multiple forums all the time.

Try answering a few questions before you become a netiquette expert.

camickra at 2007-7-28 18:40:45 > top of Java-index,Java Essentials,Java Programming...
# 10

> The fact I'm being flamed for posting a question in

> the wrong forum it quite amusing. And you wont answer

> a question because of it? How childish. Some of you

> need to learn some netiquette.

>

> p.s. camickr the reason I didnt respond to one

> [http://forum.java.sun.com/thread.jspa?threadID=519338

> 1] of your posts was because I already had the

> solution and never knew you replied.

Whose fault is that? If you kept things in one thread, and that thread in the swing forum, this wouldn't be a problem. But that's water under the bridge. Your problem right now is that you're burning that self same bridge and all the others. If you want help in the future, you may do well to change your name.

petes1234a at 2007-7-28 18:40:45 > top of Java-index,Java Essentials,Java Programming...
# 11

> > Some of you need to learn some netiquette.

>

> The proper netiquette is to do some work yourself

> before posting. This includes reading the API and

> tutorials (which you appear to have done), but it

> also involved searching the forum to see if you

> question has been asked and answered previously.

>

> Since the vast majority of people who have Swing

> related questions post in the Swing forum, then you

> have the best chance to find the answer there.

>

> If you don't find an answer then you post a question

> and we attempt to answer it.

>

> Now the next person who searches the forum has a

> better chance of finding the answer on a related

> question.

>

> If all the information is in one place then we don't

> have to keep repeating ourselves in multiple forums

> all the time.

>

> Try answering a few questions before you become a

> netiquette expert.

First I would like to apologize for my comment earlier, it was a little rude. However, as an Administrator of a small programming forum, I'm used to giving detailed answers to posts, even if a similar question was previously asked. To be honest, every question I have answered in my forum could have been found if the user did a minute of searching, but sometimes its the one on one help, and question specific response, that allows the user to actually understand what they are doing. Perhaps I have spoiled myself.

I can understand in a forum as large as this, your probably tired of answering the same questions over and over again. And its your right to answer them or not. I did do a quick search on google and the only thing I was able to find was as the code above that got me this far. I figured if there were any queries relevant to my question posted in this forum, google would have found them.

Anyway, this topic can be locked / deleted as I have used a different solution.

Thanks anyway, maybe next time I'll remember the correct forum to post in. :|

SidewinderXa at 2007-7-28 18:40:45 > top of Java-index,Java Essentials,Java Programming...