GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Adding a custom component to a layout? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding a custom component to a layout?" Watch "Adding a custom component to a layout?" New topic
Author

Adding a custom component to a layout?

Frederick Winsnes
Greenhorn

Joined: Feb 22, 2011
Posts: 17

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?


Thank you!

(Should I post the whole code for the button somewhere?)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Assuming it extends JComponent, you add it to a LayoutManager exactly as you did in line 10 of your posted code.

But your comment at line 4 suggests that you can't see it if the LayoutManager is a FlowLayout. As the API documentation says:
API documentation wrote:A flow layout lets each component assume its natural (preferred) size.

So this suggests your component's preferred size is zero. Did you implement the getPreferredSize() method?
Frederick Winsnes
Greenhorn

Joined: Feb 22, 2011
Posts: 17

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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Frederick Winsnes wrote:Does FlowLayout call the getPrefferedSize() method then?


That's how I interpret the line of documentation which I posted.

Your other question should be... when does the LayoutManager call that method? (If it calls getPreferredSize before you set the preferred size, it might still get 0,0.)
Frederick Winsnes
Greenhorn

Joined: Feb 22, 2011
Posts: 17

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
Frederick Winsnes
Greenhorn

Joined: Feb 22, 2011
Posts: 17

The whole button code. If anyone has got the energy to help it would be VERY appreciated. It is a lot of code so I understand if it is an appalling thought tho
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Okay, here you go.

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.

Second, try this test program:



You'll see where your component appears by looking for the red border.

Now change the test program to use "new GridLayout(1, 3)" instead of "new FlowLayout()". Do you see anything interesting?

(By the way: why don't you just override JButton, instead of trying to redo all of its features yourself?)
Frederick Winsnes
Greenhorn

Joined: Feb 22, 2011
Posts: 17

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.

Thank you again
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Moving to our GUI forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
GeeCON Prague 2014
 
subject: Adding a custom component to a layout?