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

voidaddComponent(Container container, JComponent component, int xPos, int yPos, int width, int height, double weightX, double weightY, int insetTop, int insetLeft, int insetBottom, int insetRight, int anchor, int stretch)
Define the parameter for the gridbaglayout constraints
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = xPos;
gridConstraints.gridy = yPos;
gridConstraints.gridwidth = width;
gridConstraints.gridheight = height;
gridConstraints.weightx = weightX;
gridConstraints.weighty = weightY;
gridConstraints.insets = new Insets(insetTop, insetLeft, insetBottom, insetRight);
...
voidadjustNarrowField(final JComponent component, final int width)
Adjust the sizes of the specified component to be a narrower field.
component.setMaximumSize(new Dimension(width, HEIGHT_ROW));
component.setPreferredSize(new Dimension(width, HEIGHT_ROW));
voidadjustWidth(JComponent aComponent, int aWidth)
Adjust the size of a component such that it has the specified preferred width.
Dimension preferedSize = aComponent.getPreferredSize();
preferedSize.width = aWidth;
aComponent.setPreferredSize(preferedSize);
voidallignBottom(JComponent lead, JComponent target, int width, int height)
allign Bottom
Rectangle rLead = lead.getBounds();
Rectangle rTarget = target.getBounds();
rTarget.x = rLead.x;
rTarget.y = rLead.y + rLead.height;
if (width > 0) {
    rTarget.width = width;
if (height > 0) {
...
intcalculateCharWidth(JComponent component, int numChars)
calculate Char Width
if (numChars < 0)
    return -1;
if (component == null)
    return -1;
int width = -1;
Font font = component.getFont();
if (font != null) {
    FontMetrics fm = component.getFontMetrics(font);
...
voidclearWidth(JComponent comp)
clear Width
Dimension size = comp.getMinimumSize();
size.width = 0;
comp.setMinimumSize(size);
size = comp.getPreferredSize();
size.width = 0;
comp.setPreferredSize(size);
StringclipString(final JComponent component, final String string, final int avaiableWidth)
Clips the passed string .
if (string == null || string.isEmpty()) {
    return "";
final FontMetrics fm = component.getFontMetrics(component.getFont());
final String clipString = "...";
int width = SwingUtilities.computeStringWidth(fm, clipString);
int nChars = 0;
for (final int max = string.length(); nChars < max; nChars++) {
...
StringclipStringifNeeded(final JComponent component, final String string, final int avaiableWidth)
Clips a string if its longer than the specified width.
if (string == null || string.isEmpty()) {
    return "";
final FontMetrics fm = component.getFontMetrics(component.getFont());
final int width = SwingUtilities.computeStringWidth(fm, string);
if (width > avaiableWidth) {
    return clipString(component, string, avaiableWidth);
return string;
JComponentcreateTitledPanel(JComponent component, String title, int width, int height)
create Titled Panel
component.setPreferredSize(new Dimension(width, height));
component.setMinimumSize(new Dimension(width, height));
return createTitledPanel(component, title);
voidfixWidth(JComponent c, int width)
fix Width
Dimension ps = c.getPreferredSize();
c.setPreferredSize(new Dimension(width, ps.height));
c.setMinimumSize(c.getPreferredSize());
c.setMaximumSize(c.getPreferredSize());