• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Adding a custom component to a layout?

 
Greenhorn
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?)
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our GUI forum.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic