Phox
Phox

Reputation: 101

Java jpanel that works like window's drag n drop icon rearranging

So when you move an icon to a new position windows automatically pushes other icons down the list( i would also like to push them up). I would like to do this with jcomponents in general, jtables in specific.

Also for bonus points if I can keep track of what table is where (i'll need to save their order in xml).

any ideas?

Upvotes: 0

Views: 309

Answers (1)

sarcan
sarcan

Reputation: 3165

There is no out-of-the-box mechanism for rearranging components via drag and drop, but it is easy enough to do yourself. Here's the basic idea:

1) Use a JLayeredPane as base component, and put your tables on the default layer

2) Add a transparent component on top of that, e.g. a higher layer. This will be your glass pane

3) Install a MouseMotionListener on the glass pane. When you detect a drag that should move a table, go on with 4. Otherwise, redispatch the event to the underlying component.

4) Provide some kind of visual feedback for the drag, e.g. a semitransparent line to indicate the drop location. Show the user where the table would appear when he releases the mouse.

5) If the drag indicates that the tables should be rearranged, simply redo the layout on your table panel to position them accordingly.

If you have specific questions regarding one of the steps, feel free to comment.

Upvotes: 1

Related Questions