Is there a way to make a non deep copy of a JPanel
Alright I have a JPanel (I'm using NetBeans and swing) that I would like to make a copy of and use it in another tab, meaning it uses the same layout as the other panel but I dont want to make a deep copy of it. Cause if I make a deep copy o开发者_StackOverflow中文版f it when I change one I change the other. Is there any way to just make a copy of what it looks like without it having the ties to the original
Seems that you have misunderstood the word "deep copy" - a deep copy just has the effect of not being tied to the original, in contrast to a shallow copy, which leaves such ties.
Anyway, creating copies of whole Swing component trees is nothing which is really supported - it could be done by serialization, though.
But why can't you simply use the same code which created your original JPanel (with the components in it) and create a new one?
Here is method for solving the following problem without coding just using design views of JPanels/JFrames.
You can go to Navigator in design view select all JPanel components and copy them by typing ctrl+a and ctrl+c. Then create another one JPanel and in design view, just paste them with ctrl+v.
Result: You get all components same size, dimensions and positions with same properties and values. After you do this, you can easily change whatever you want by using properties of GUI forms.
You actually cannot make a copy of a Swing component that is tied to the original. Components can only share models.
You can make a clone of your panel which will be totally separate panel in no way tied to the original.
You're using Matisse UI-designer in Netbeans IDE? Then you can just select your designed JPanel, copy it to the clipboard and paste into the other form.
精彩评论