I have created a custom button class which allows the user to create a button which can be any Polygon size and shape they want.
So far, I am able to make the button get added to a null layout, but I can not figure out how to get the component to get added to a LayoutManager such as FlowLayout or BorderLayout.
Could someone please explain how to make a custom component recognize and get added to a LayoutManager?
Using this code makes the button get drawn at the specified coordinates (xTest and yTest).
This is the paint-method of the CustomShapeButton, not sure if this is where modifications should be made?
(Should I post the whole code for the button somewhere?)
I used extend Component, but changed that to JComponent now
I have not added done an Override on the method getPrefferedSize() or on the setPrefferedSize(). Does FlowLayout call the getPrefferedSize() method then? And does it find its x- and y-coordinates automatically when using the layouts?
When setting the shape I now also set the prefferedSize
That did not work however, not sure if this is the right way to do it
When drawing I am using the getButtonShape() method which returns the Polygon that is supposed to be the Shape of the button.
I am guessing that my Graphics is automatically drawing the Polygon at the x- and y-coordinates that I have in the Polygon, do you think there is a way to make the coordinates change according to the Layout so the Graphics do not draw it at the wrong location?
And assuming that the getPrefferedSize() method gets called by the LayoutManager when drawing the JComponent I don't think that is the problem. The setButtonShape() gets called first thing in the constructor.
I realize that I am not very clear but I am not sure where the problem lies so I do not know where in the code I should focus
First, change your code to override paintComponent() rather than paint(). This is a standard rule for Swing components -- unless your paint() method actually does draw the components border and its children, which yours doesn't.
The button appeared when using GridLayout. That was interesting.
I changed paint to paintComponent and tested your testprograms. The results were really interesting :-) I think I can manage to do something based on that
Thank you VERY much for your help! You are awesome
I use this class for 3 reasons
1. I want to use any shape I want (maybe would work if Overriding paintComponent in JButton ?)
2. I want to learn how to do custom components more effectively I have done some, but they have been too specific, and unusable for other programs so I want to learn how to make more "universal" ones
3. I want to be able to use any color on the button I want. JButtons will not do that for me because I am using a Mac (at least I think that is why) and somehow it will not change the color when calling setBackground or setForeground.