masonc15
masonc15

Reputation: 1081

JButton orientation within a JPanel

I am designing a GUI for an image editor in which I have multiple buttons which apply different effects to the selected image. Currently I have the Display button in the top center of the window, the Load button in the bottom center of the window, and I want the effect buttons stacked on the left side of the window. Here is the image of my GUI, with the ButtonPanel JPanel in red, the FileChooser JPanel in green, and the ImageDisplay JPanel in blue:

test

As you can see, the buttons in the ButtonPanel are stacking horizontally instead of vertically. I would like them to stack vertically. Thank you for the help.

Upvotes: 1

Views: 790

Answers (3)

Paul Samsotha
Paul Samsotha

Reputation: 209062

Another option is to use a JToolBar (kind of like in PhotoShop) where you can change the orientation or float the tool bar around. See more at How to Use Tool bars

tool bar with top orientation tool bar with side orientation tool bar floating

Upvotes: 0

Braj
Braj

Reputation: 46871

the buttons in the ButtonPanel are stacking horizontally instead of vertically. I would like them to stack vertically.

You can try with BoxLayout as well that allows multiple components to be laid out either vertically or horizontally.

Please have a look at How to Use BoxLayout where you will find the sample code as well.

enter image description here

Upvotes: 2

camickr
camickr

Reputation: 324147

the buttons in the ButtonPanel are stacking horizontally instead of vertically. I would like them to stack vertically.

The default layout for a JPanel is a FlowLayout which displays all components horizontally.

Use a different Layout Manager. Maybe a GridLayout. With a GridLayout you can display components vertically if you set the constructor parameters properly.

Read the section from the Swing tutorial on Layout Managers for more information and working examples.

Upvotes: 3

Related Questions