Copy JPanel Contents to Another JPanel

Hey, I'm new to swing/gui programming and I was wondering, if I can copy the contents of one JPanel to be displayed on another? I have a split pane and want to be able to drag the big picture on the left to little boxes on the right but I'm not sure if it's possible.

Thanks in advance,

Mike

[313 byte] By [kerryblue19a] at [2007-11-27 8:34:08]
# 1
Very vague. Drap and drop? http://java.sun.com/docs/books/tutorial/uiswing/dnd/index.html
Hippolytea at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 2
I apologize. To clarify, what I'm looking to see is if it is possible to copy the content of JPanel A so that I can make them appear in JPanelB.
kerryblue19a at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 3
why in the name of dog would you need to do that?
mkoryaka at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 4
That should be straightforward. If you know what the components are, just copy (or alias?) their models.
Hippolytea at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 5

I'm creating a guitar chord creator application and it's split into two sides. On the left, in JPanel A, is an interactive fretboard, and on the right is a visual 2rows x 4columns array of JPanel's. When the user clicks on one JPanel I need the picture of the fretboard that appears in JPanel A to appear in this new JPanel.

Scaling aside, I'm not really sure how to do this.

Thanks.

kerryblue19a at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 6
the thing is, is that what is in JPanel A is interactive and I don't want it to be when it goes to JPanel B.
kerryblue19a at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 7
I suspect what the op wants to do is copy some info in another JComponent such a JTextField that has been placed in one JPanel and place it in another JComponent in the second JPanel.But I'm only guessing...
filestreama at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 8
I just need a visual copy of what is in JPanelA so I can show it in JPanelB. JPanelA is interactive and I don't want the copy that appears in JPanelB to be interactive.
kerryblue19a at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 9
I tried using JPanelA.clone() but I get an error saying that clone is protected in Object
kerryblue19a at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 10

> I'm creating a guitar chord creator application and it's split into two sides.

> On the left, in JPanel A, is an interactive fretboard, and on the right is a visual

> 2rows x 4columns array of JPanel's. When the user clicks on one JPanel

> I need the picture of the fretboard that appears in JPanel A to appear in this new

> JPanel.

The other shoe drops, good! Have you defined a class like Chord or

Fingering or FretboardModel? A class that represents the model or data

behind the state of your interactive panel? If the answer is no, go back to the drawing board.

Seriously, you need to define a data model that is being viewed in the left panel,

so that you can manipulate it. For example, make sure it has a clone or copy method,

which is what you need right now.

Hippolytea at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 11

I do. I have a class that creates the whole interactive fretboard, called FretBoard, and the way I'm getting it to show up is just some code like JPanel temp = new FretBoard(); and it works fine.

Thanks for the clone tip. I guess I can just use it like

return this;

?

kerryblue19a at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 12

I would separate the GUI component:

public class FretboardView extends JComponent

from the model, say FretboardModel. This is the MVC pattern:

http://en.wikipedia.org/wiki/Model-view-controller

But in any case, if your cloning or copy method simply returns this,

you are in big trouble. When you alter the original, the "copy" will change, too!

Hippolytea at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...
# 13

> I do. I have a class that creates the whole

> interactive fretboard, called FretBoard, and the way

> I'm getting it to show up is just some code like

> JPanel temp = new FretBoard(); and it works fine.

>

> Thanks for the clone tip. I guess I can just use it

> like

> return this;

> ?

No, that's the View, he was talking about the Model. It's all part of the MVC pattern, which you should definitely google and read about. You have a class that represents your guitar chords (or whatever it's called, most of what I know about guitars I learned from Guitar Hero). Then you take your FratBoardView() class and show the model data. When the user changes something, the model is what get's changed, then you update the view based on the model. It sounds more complicated, but it's quite the opposite.

As far as using return this; in your clone method, that doesn't actually clone the object, it just returns another reference the same way this would:

MyClass mc1 = new MyClass();

MyClass mc2 = mc1;

You still have one object, now with two references to it.

hunter9000a at 2007-7-12 20:30:21 > top of Java-index,Java Essentials,Java Programming...