Well, i searched all through google and stuff...but in vain..i want to add a functionality in my program to copy and paste buttons which are present on the screen.....I got a wonderful article to copy and paste text.but what about some swing components???????how do we copy and paste them?????

[site]http://www.cafeaulait.org/course/week13/[/site]

Dani AI

Generated

For a GUI editor like what is building, the practical and robust approach is to copy a component's description (a lightweight model), not try to duplicate the live JComponent instance. Swing components have a single parent and internal UI state; moving a live instance or trying to serialize whole components leads to parentage errors, listener leaks, and L&F/state problems. pointed to the clipboard and suggested custom copy behavior — both are on track, but you want a small, serializable representation plus a TransferHandler/Transferable glue layer.

A simple workflow:

  • Define a ComponentModel (DTO) that stores the component class name and the minimal properties you need (bounds/size, text, icon resource path, font, colors, layout constraints, client properties, action id).
  • Keep behavior outside the GUI instance: store an Action id or listener key rather than raw listener objects, and reattach or reuse shared Actions on paste.
  • Support two transfer flavors: a local object flavor (fast, within the JVM) and a serialized/XML flavor (for cross-JVM or clipboard persistence). XMLEncoder can work for bean-like components but explicit mapping is usually clearer and safer.

Example skeleton (conceptual):

class ComponentModel implements Serializable {
  String className;
  Map<String,Object> properties;
  Object layoutConstraints;
}

class ComponentTransferable implements Transferable {
  static DataFlavor MODEL_FLAVOR = new DataFlavor(ComponentModel.class, "ComponentModel");
  ComponentModel model;
  // implement getTransferDataFlavors(), isDataFlavorSupported(), getTransferData()
}

Use a TransferHandler to hook export/import for DnD and clipboard. On paste create a new instance (reflection or factory), apply properties, add to the target container on the EDT, then revalidate()/repaint(). Test layout-constraint translation (GridBag vs BorderLayout), avoid copying listeners directly, and prefer shared Actions to preserve behavior. See the Swing TransferHandler API for integration details: TransferHandler.

Recommended Answers

All 7 Replies

Use the ClipBoard?

are you talking about cutting and pasting complete swing components, like buttons and panels?

are you talking about cutting and pasting complete swing components, like buttons and panels?

Oh Yeah........something which enables the users to copy and paste the buttons which are present on the interface itself........

This is a difficult problem. What is your main goal? Are you trying to build a gui editor?

Just shooting from the hip, but I would think that you would want to create your own buttons that extend the normal components and then extend the current java cut and paste utility. Maybe have an interface called Copyable or something like that and then have the new cut and paste utility look for components that are copyable and go from there...

This is a difficult problem. What is your main goal? Are you trying to build a gui editor?

Oh yes its something like a GUI editor

Just shooting from the hip, but I would think that you would want to create your own buttons that extend the normal components and then extend the current java cut and paste utility. Maybe have an interface called Copyable or something like that and then have the new cut and paste utility look for components that are copyable and go from there...

From all that's written here, i have created my buttons and can drag and drop them too..........and frankly the explanation u gave seemed a bit of greek to me.....I'll be grateful to u if u culd explain it in a bit simpler way.......Thanks..........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.