The list of methods to do JComponent Width are organized into topic(s).
void
addComponent(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);
...
void
adjustWidth(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);
void
clearWidth(JComponent comp) clear Width
Dimension size = comp.getMinimumSize();
size.width = 0;
comp.setMinimumSize(size);
size = comp.getPreferredSize();
size.width = 0;
comp.setPreferredSize(size);
String
clipString(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++) {
...
void
fixWidth(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());