I'm currently working on a project in which I need a very simple editor for GUI-like objects. This editor would be a canvas on which well known GUI widgets can be placed. For example one can place a button and a textfield on there, move them around and resize them. No interaction with the widgets themselves is needed.
I've been trying to accomplish this by adapting a very simple paint tutorial, I thought it would be easy to implement it this way, yet I bump into many problems with drawing custom shapes and text on a canvas and dragging and dropping those shapes.
I was wondering if I could reuse real Swing widgets on a JPanel and let the user place, move around and resize them. If so, what are the things I should look into?
I know this might seem very little information but I'm honestly stuck in searching for a solution.
This sounds like a fun project.
I would definitely go on the JPanel as the container, with a layout set to null. In order to be able to move the components like JButton, JLabel in the container, you would have to listen for MouseListener and MouseMotionListener events on the added components and handle them accordingly. You would have to call validate() and repaint() on the container whenever one component is moved or resized.
I would implement this in a mixed approach: Create a class that implements the mouse responses (dragging, resizing). This class has the responsibility of managing the user interaction.
For the actual painting of the components, use real Swing components (the user interaction class would have a reference to the component its supposed to represent and delegate the actual rendering to that component).
This approach avoids most of the complexities that arise when extending the original Swing components, yet you can still reuse all their painting code.
I thought I should remember the good old Swing days and have a little bit of fun writing you a proof of concept for this. It supports adding a button to the main panel and then move and some basic resize.
It is just a proof of concept, but it answers a lot of the questions you already have. I added a few TODOs there, such as you will know what to do next.
Enjoy...