Java Utililty Methods JComponent Width

List of utility methods to do JComponent Width

  1. HOME
  2. Java
  3. J
  4. JComponent Width

Description

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

Method

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 adjustNarrowField(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));
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 allignBottom(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) {
...
int calculateCharWidth(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);
...
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++) {
...
String clipStringifNeeded(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;
JComponent createTitledPanel(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);
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());


AltStyle によって変換されたページ (->オリジナル) /