I'm a little puzzled that when I create a basic JComponent and make it draggable by setting the location during the event it defaults and goes back to the original location once I let go of the mouse button and move the mouse. Can anyone kindly lead me in the right direction on what to do?
Thanks for the reply and while I have read the PaintComponent was the correct place to work with the object I believe I had tried working with it but couldn't make it work correctly for some reason. I'll try it again but as for the Layout manager I am not specifying any so does it adopt one automatically anyway? I had previously tried to set the layout to null but it would never show the component I was making so once I took that line out it showed up. I'll post my main panel code as it's very basic since I am just trying to get the hang of writing custom components.
Yes as expected when I set the layout to null it no longer paints my custom control. Also I moved all the code from paint to paintcomponent and it worked this time so must have been something in my old code that I was experimenting with that caused it not to work earlier. The component still goes back to 100,100,100,100 which is where it was originally created at using the following line of code.
So long story short I still am having the same problem as before.
Thanks for your help and if you offer additional comments or help to resolve this it would greatly be appreciated.
My goal is that the object stays where I drag and let it go at.
posted 6 years ago
as for the Layout manager I am not specifying any so does it adopt one automatically anyway?
Your component is added to the content pane (which is just a JPanel) of the frame. The default LayoutManager for the content pane is a BorderLayout. So the size of you component is set to be whatever space is available in the frame (minus the space used for the borders and titlebar). That is the only reason your component displays.
Yes as expected when I set the layout to null it no longer paints my custom control.
I already explained what you need to do in my first reply in order to get a component to display when using a null layout.
The component still goes back to 100,100,100,100 which is where it was originally created
No, the component is displayed at location (0, 0). Your custom painting takes places at (100, 100). This is NOT the way to create a custom component. Your painting should be done at (0, 0) with a size of (100, 100). Now when you drag the component around the screen you just change the location of the component, but you need to use a null layout otherwise the layout manager will keep resetting the location to (0, 0).
Read the Swing tutorial on Custom Painting for an explanation and example of how to create a custom component. You should also be overriding the getPreferredSize() method to specify the size of your component.
Ok I think I understand most of that with the exception of the location being at (0, 0). Are you saying that since it has a BorderLayout then that is the border Layouts (0,0) position not the actual frame correct? The reason I brought up the 100,100,100,100 dimensions is that if I change it to 50,50,100,100 or 500,500,100,100 it does paint the component at those positions and does return the component to those locations in the frame. This is why I am a little confused about the (0,0) statement.
Please forgive me as I am just trying to understand and am not in any way trying to test or bother you in any way.
Thank you for being patiant with me. Thank you for the reference.
posted 6 years ago
Read the Swing tutorial on LayoutManagers. I gave you the link to the tutorial, search the table of contents for the layout tutorial.
One of the jobs of a layout manager is to position a component within the panel. This is done invoking the setLocation() method on the component. In your case you are adding the component to the CENTER of the BorderLayout. Since it is the only component added to the content pane it will be displayed at location (0, 0). You can verify this by displaying the bounds of your component AFTER the frame has been made visible.
if I change it to 50,50,100,100 or 500,500,100,100 it does paint the component at those positions
All Swing painting is done relative to the component, in the first case at (50, 50). Your component is located at (0, 0) of the content pane so the painting looks like it is being painted at (50, 50). If however you more your component to location (100, 100) then your painting is still done at (50, 50) but it will look like it is done at (150, 150) relative to the content pane.
Thank you for taking the time to help me understand. I will continue to look at the tutorials and really read up on Border Layouts. Appreciate it and I'm going to mark this as resolved as the rest is just a learning step for myself.