How do I only change the width or height of a component that requires a Dimension
object? Currently I do it like this:
jbutton.setPreferredSize(new Dimension(button.getPreferredSize().width, 100));
But I have a feeling that I'm doing it the wrong way. What is the best way to approach this if there is a better way?
The length, width and height are the dimensions of a geometrical figure that depict how long, wide and high a figure is. While length is the longest side of a figure, width is the shorter side and height is the vertical dimension of the figure.
The Graphics' industry standard is width by height (width x height). Meaning that when you write your measurements, you write them from your point of view, beginning with the width. That's important.
All box dimensions are written as length x width x height. For example, 14" x 11" x 4" means 14" (L) x 11" (W) x 4" (H)".
Web performance advocates have often advised to add dimensions to your images for best performance to allow the page to be laid out with the appropriate space for the image, before the image itself has been downloaded.
First of all you are not changing the dimension of JButton. You are specifying the desired preferred size, that can be eventually applied to your JButton depending on the LayoutManager of the component it's inserted into.
For what concern the use of Dimension object that's fine. Eventually you can access directly Dimension field:
Dimension d = button.getPreferredSize();
d.height = 10;
jbutton.setPreferredSize(d);
but that's pretty much the same thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With