Reputation: 1398
I'm making a Java application that has a toolbar at the top of the window. On this toolbar there are 12 buttons, but the customer wants one of the buttons to be 2x the size of all the other buttons as shown below.
How can I do this so that the one big toolbar button overlaps the edges of the toolbar bounds, but still behaves like it is part of the toolbar?
Upvotes: 2
Views: 270
Reputation: 324088
You can't add a button to the toolbar and have it extend outside the bounds of the toolbar. That is Swing components will be clipped if that are painted outside the bounds of its container.
As a hack you can try painting the oversized button on top of all your other components:
Add a regular button to the toolbar to fill the horizontal space of the toolbar to reserve the space on the toolbar for your oversized button.
Add a GlassPane to the frame.
Add your oversized button to the glass pane.
Add a ComponentListener
to the regular button and handle the componentMoved()
event. Whenever the event is generated you would need to position your oversized button on the glass pane to that it is painted on top of the regular button. Check out the SwingUtilities
class. There are methods that will allow you to convert Points relative to different containers.
Upvotes: 1
Reputation: 13
There are various ways of achieving this. One way i would suggest is to create a new class which extends Button class (since these are used in the toolbar). In this class you create a constructor with the parameters "int width" & "int height". When creating the toolbar and it's buttons you simply set the values of these 2 parameters bigger then the other ones.
Don't forget the pass the parameters to the "setSize()" method in the button class
Upvotes: 0