Java Utililty Methods JComponent Width

List of utility methods to do JComponent Width

Description

The list of methods to do JComponent Width are organized into topic(s).

Method

intgetMinimumWidth(JComponent comp)
get Minimum Width
return comp.getMinimumSize().width;
RectanglegetRectangle(JComponent comp)
Get a Rectangle object representing the given JComponent's position and magnitude in space.
return new Rectangle(comp.getY(), comp.getX(), comp.getWidth(), comp.getHeight());
RectanglegetRectangle(JComponent comp)
Get a Rectangle object representing the given JComponent's position and magnitude in space.
Rectangle Rectangle = new Rectangle(comp.getY(), comp.getX(), comp.getWidth(), comp.getHeight());
return Rectangle;
voidinitComponent(JComponent component, String title, int width, int height)
init Component
component.setPreferredSize(new Dimension(width, height));
component.setMinimumSize(new Dimension(width, height));
component.setBorder(BorderFactory.createTitledBorder(title));
voidmakeEqualWidth(JComponent... components)
Sets the minimum width of all components to the width of the widest component.
int maxSize = 0;
for (JComponent comp : components) {
    Dimension size = comp.getPreferredSize();
    if (size.width > maxSize) {
        maxSize = size.width;
for (JComponent comp : components) {
...
voidmakeSamePreferredWidth(JComponent... comps)
Makes components use same width as the width of the widest component in the group
if (comps.length == 0)
    return;
Arrays.sort(comps, new Comparator<JComponent>() {
    @Override
    public int compare(JComponent o1, JComponent o2) {
        return o1.getPreferredSize().width - o2.getPreferredSize().width;
});
...
voidnormalizeWidth(JComponent... components)
Sets all specified components to the same width.
int width = 0;
for (JComponent component : components) {
    if (component.getPreferredSize().width > width) {
        width = component.getPreferredSize().width;
setPreferredWidth(width, components);
voidsameWidth(JComponent[] components, int width)
same Width
for (int i = 0; i < components.length; i++) {
    Dimension size = components[i].getSize();
    size.width = width;
    components[i].setSize(size);
voidsetBoundsAndCenterHorizontally(JComponent component, int x, int y, int width, int height)
set Bounds And Center Horizontally
Container parent = component.getParent();
int parentWidth = parent.getWidth();
int paddingOnBothSides = parentWidth - width;
x = paddingOnBothSides / 2;
component.setBounds(x, y, width, height);
voidsetComponent3Width(JComponent c, int width)
set Component Width
Dimension d = new Dimension(width, c.getPreferredSize().height);
set3Dimensions(c, d);