Java Code Examples for javax.swing.JLabel#getPreferredSize()

The following examples show how to use javax.swing.JLabel#getPreferredSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TitledBorder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the minimum dimensions this border requires
 * in order to fully display the border and title.
 * @param c the component where this border will be drawn
 * @return the {@code Dimension} object
 */
public Dimension getMinimumSize(Component c) {
    Insets insets = getBorderInsets(c);
    Dimension minSize = new Dimension(insets.right+insets.left,
                                      insets.top+insets.bottom);
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        int position = getPosition();
        if ((position != ABOVE_TOP) && (position != BELOW_BOTTOM)) {
            minSize.width += size.width;
        }
        else if (minSize.width < size.width) {
            minSize.width += size.width;
        }
    }
    return minSize;
}
 
Example 2
Source File: TitledBorder.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the minimum dimensions this border requires
 * in order to fully display the border and title.
 * @param c the component where this border will be drawn
 * @return the {@code Dimension} object
 */
public Dimension getMinimumSize(Component c) {
    Insets insets = getBorderInsets(c);
    Dimension minSize = new Dimension(insets.right+insets.left,
                                      insets.top+insets.bottom);
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        int position = getPosition();
        if ((position != ABOVE_TOP) && (position != BELOW_BOTTOM)) {
            minSize.width += size.width;
        }
        else if (minSize.width < size.width) {
            minSize.width += size.width;
        }
    }
    return minSize;
}
 
Example 3
Source File: EditorPanel.java    From jclic with GNU General Public License v2.0 6 votes vote down vote up
protected JLabel createTitleLabel(int preferredWidth) {
  JLabel result = new JLabel(getTitle());
  result.setHorizontalAlignment(SwingConstants.CENTER);
  if (getIcon() != null) {
    result.setIcon(getIcon());
    result.setIconTextGap(10);
  }
  result.setBackground(titleBgColor);
  result.setForeground(titleForeColor);
  result.setOpaque(true);
  result.setBorder(titleBorder);
  result.validate();
  Dimension d = result.getPreferredSize();
  result.setPreferredSize(new Dimension(Math.max(d.width, preferredWidth), d.height));
  result.setMinimumSize(result.getPreferredSize());
  return result;
}
 
Example 4
Source File: TitledBorder.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the minimum dimensions this border requires
 * in order to fully display the border and title.
 * @param c the component where this border will be drawn
 * @return the {@code Dimension} object
 */
public Dimension getMinimumSize(Component c) {
    Insets insets = getBorderInsets(c);
    Dimension minSize = new Dimension(insets.right+insets.left,
                                      insets.top+insets.bottom);
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        int position = getPosition();
        if ((position != ABOVE_TOP) && (position != BELOW_BOTTOM)) {
            minSize.width += size.width;
        }
        else if (minSize.width < size.width) {
            minSize.width += size.width;
        }
    }
    return minSize;
}
 
Example 5
Source File: MaterialIcons.java    From MobyDroid with Apache License 2.0 6 votes vote down vote up
private static BufferedImage buildImage(String text, Font font, Color color) {
    JLabel icon = new JLabel(text);
    icon.setForeground(color);
    icon.setFont(font);
    Dimension dim = icon.getPreferredSize();
    int width = dim.width + 1;
    int height = dim.height + 1;
    icon.setSize(width, height);
    BufferedImage bufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufImage.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    icon.print(g2d);
    g2d.dispose();
    return bufImage;
}
 
Example 6
Source File: DeltaRobot3Panel.java    From Robot-Overlord-App with GNU General Public License v2.0 5 votes vote down vote up
protected CollapsiblePanel createSpeedPanel() {
	double speed=robot.getSpeed();
	int speedIndex;
	for(speedIndex=0;speedIndex<speedOptions.length;++speedIndex) {
		if( speedOptions[speedIndex] >= speed )
			break;
	}
	speedNow = new JLabel(Double.toString(speedOptions[speedIndex]),JLabel.CENTER);
	java.awt.Dimension dim = speedNow.getPreferredSize();
	dim.width = 50;
	speedNow.setPreferredSize(dim);

	CollapsiblePanel speedPanel = new CollapsiblePanel("Speed");

	GridBagConstraints con2 = PanelHelper.getDefaultGridBagConstraints();
	con2.weightx=0.25;
	speedPanel.getContentPane().add(speedNow,con2);

	speedControl = new JSlider(0,speedOptions.length-1,speedIndex);
	speedControl.addChangeListener(this);
	speedControl.setMajorTickSpacing(speedOptions.length-1);
	speedControl.setMinorTickSpacing(1);
	speedControl.setPaintTicks(true);
	con2.anchor=GridBagConstraints.NORTHEAST;
	con2.fill=GridBagConstraints.HORIZONTAL;
	con2.weightx=0.75;
	con2.gridx=1;
	speedPanel.getContentPane().add(speedControl,con2);
	
	return speedPanel;
}
 
Example 7
Source File: LoadingSplashScreen.java    From webanno with Apache License 2.0 5 votes vote down vote up
public SplashWindow(URL aUrl)
{
    JLabel l = new JLabel(new ImageIcon(aUrl));
    JLabel info = new JLabel("Application is loading...");
    getContentPane().add(l, BorderLayout.CENTER);
    getContentPane().add(info, BorderLayout.SOUTH);
    pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension labelSize = l.getPreferredSize();
    setLocation(screenSize.width / 2 - (labelSize.width / 2),
            screenSize.height / 2 - (labelSize.height / 2));
    setVisible(true);
}
 
Example 8
Source File: TableRowHeader.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private int preferredHeaderWidth() {
    final JLabel longestRowLabel = new JLabel("1.1.1");
    final JTableHeader header = table.getTableHeader();
    longestRowLabel.setBorder(header.getBorder());
    longestRowLabel.setHorizontalAlignment(SwingConstants.CENTER);
    longestRowLabel.setFont(header.getFont());
    return longestRowLabel.getPreferredSize().width;
}
 
Example 9
Source File: AbstractProductsRepositoryPanel.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
protected final void refreshLabelWidths() {
    int maximumLabelWidth = computeLeftPanelMaximumLabelWidth();
    for (int i=0; i<this.parameterComponents.size(); i++) {
        JLabel label = this.parameterComponents.get(i).getLabel();
        Dimension labelSize = label.getPreferredSize();
        labelSize.width = maximumLabelWidth;
        label.setPreferredSize(labelSize);
        label.setMinimumSize(labelSize);
    }
}
 
Example 10
Source File: TableRowHeader.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private int preferredHeaderWidth() {
    final JLabel longestRowLabel = new JLabel("1.1.1");
    final JTableHeader header = table.getTableHeader();
    longestRowLabel.setBorder(header.getBorder());
    longestRowLabel.setHorizontalAlignment(SwingConstants.CENTER);
    longestRowLabel.setFont(header.getFont());
    return longestRowLabel.getPreferredSize().width;
}
 
Example 11
Source File: JLabelled.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public JLabelled(String text, String tooltip, JComponent main, int labelWidth)
{
  // This panel is horizontal box.
  setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
  setAlignmentX(Component.LEFT_ALIGNMENT);

  // Shorten text to last part after a dot
  // if it's too long
  final FontMetrics fm = main.getFontMetrics(main.getFont());
  if (fm != null) {
    int ix;
    while (-1 != (ix = text.indexOf("."))
           && fm.stringWidth(text) > labelWidth) {
      text = text.substring(ix + 1);
    }
  }

  final JLabel label = new JLabel(text);
  final Dimension size = label.getPreferredSize();
  label.setPreferredSize(new Dimension(labelWidth, size.height + 2));

  if (tooltip != null && !"".equals(tooltip)) {
    label.setToolTipText("<html>" + tooltip + "</html>");
  }

  // Since we are in a scroll pane with glue at the bottom we do not want the
  // components to stretch vertically.
  main.setMaximumSize(new Dimension(Integer.MAX_VALUE, main
      .getPreferredSize().height));
  label.setMaximumSize(label.getPreferredSize());

  add(label);
  add(main);
}
 
Example 12
Source File: ExportImage.java    From Logisim with GNU General Public License v3.0 4 votes vote down vote up
OptionsPanel(JList<?> list) {
	// set up components
	formatPng = new JRadioButton("PNG");
	formatGif = new JRadioButton("GIF");
	formatJpg = new JRadioButton("JPEG");
	ButtonGroup bgroup = new ButtonGroup();
	bgroup.add(formatPng);
	bgroup.add(formatGif);
	bgroup.add(formatJpg);
	formatPng.setSelected(true);

	slider = new JSlider(SwingConstants.HORIZONTAL, -3 * SLIDER_DIVISIONS, 3 * SLIDER_DIVISIONS, 0);
	slider.setMajorTickSpacing(10);
	slider.addChangeListener(this);
	curScale = new JLabel("222%");
	curScale.setHorizontalAlignment(SwingConstants.RIGHT);
	curScale.setVerticalAlignment(SwingConstants.CENTER);
	curScaleDim = new Dimension(curScale.getPreferredSize());
	curScaleDim.height = Math.max(curScaleDim.height, slider.getPreferredSize().height);
	stateChanged(null);

	printerView = new JCheckBox();
	printerView.setSelected(true);

	// set up panel
	gridbag = new GridBagLayout();
	gbc = new GridBagConstraints();
	setLayout(gridbag);

	// now add components into panel
	gbc.gridy = 0;
	gbc.gridx = GridBagConstraints.RELATIVE;
	gbc.anchor = GridBagConstraints.NORTHWEST;
	gbc.insets = new Insets(5, 0, 5, 0);
	gbc.fill = GridBagConstraints.NONE;
	addGb(new JLabel(Strings.get("labelCircuits") + " "));
	gbc.fill = GridBagConstraints.HORIZONTAL;
	addGb(new JScrollPane(list));
	gbc.fill = GridBagConstraints.NONE;

	gbc.gridy++;
	addGb(new JLabel(Strings.get("labelImageFormat") + " "));
	Box formatsPanel = new Box(BoxLayout.Y_AXIS);
	formatsPanel.add(formatPng);
	formatsPanel.add(formatGif);
	formatsPanel.add(formatJpg);
	addGb(formatsPanel);

	gbc.gridy++;
	addGb(new JLabel(Strings.get("labelScale") + " "));
	addGb(slider);
	addGb(curScale);

	gbc.gridy++;
	addGb(new JLabel(Strings.get("labelPrinterView") + " "));
	addGb(printerView);
}
 
Example 13
Source File: TitledBorder.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Reinitialize the insets parameter with this Border's current Insets.
 * @param c the component for which this border insets value applies
 * @param insets the object to be reinitialized
 */
public Insets getBorderInsets(Component c, Insets insets) {
    Border border = getBorder();
    insets = getBorderInsets(border, c, insets);

    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        switch (getPosition()) {
            case ABOVE_TOP:
                insets.top += size.height - edge;
                break;
            case TOP: {
                if (insets.top < size.height) {
                    insets.top = size.height - edge;
                }
                break;
            }
            case BELOW_TOP:
                insets.top += size.height;
                break;
            case ABOVE_BOTTOM:
                insets.bottom += size.height;
                break;
            case BOTTOM: {
                if (insets.bottom < size.height) {
                    insets.bottom = size.height - edge;
                }
                break;
            }
            case BELOW_BOTTOM:
                insets.bottom += size.height - edge;
                break;
        }
        insets.top += edge + TEXT_SPACING;
        insets.left += edge + TEXT_SPACING;
        insets.right += edge + TEXT_SPACING;
        insets.bottom += edge + TEXT_SPACING;
    }
    return insets;
}
 
Example 14
Source File: TitledBorder.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Returns the baseline.
 *
 * @throws NullPointerException {@inheritDoc}
 * @throws IllegalArgumentException {@inheritDoc}
 * @see javax.swing.JComponent#getBaseline(int, int)
 * @since 1.6
 */
public int getBaseline(Component c, int width, int height) {
    if (c == null) {
        throw new NullPointerException("Must supply non-null component");
    }
    if (width < 0) {
        throw new IllegalArgumentException("Width must be >= 0");
    }
    if (height < 0) {
        throw new IllegalArgumentException("Height must be >= 0");
    }
    Border border = getBorder();
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();
        Insets insets = getBorderInsets(border, c, new Insets(0, 0, 0, 0));

        int baseline = label.getBaseline(size.width, size.height);
        switch (getPosition()) {
            case ABOVE_TOP:
                return baseline;
            case TOP:
                insets.top = edge + (insets.top - size.height) / 2;
                return (insets.top < edge)
                        ? baseline
                        : baseline + insets.top;
            case BELOW_TOP:
                return baseline + insets.top + edge;
            case ABOVE_BOTTOM:
                return baseline + height - size.height - insets.bottom - edge;
            case BOTTOM:
                insets.bottom = edge + (insets.bottom - size.height) / 2;
                return (insets.bottom < edge)
                        ? baseline + height - size.height
                        : baseline + height - size.height + insets.bottom;
            case BELOW_BOTTOM:
                return baseline + height - size.height;
        }
    }
    return -1;
}
 
Example 15
Source File: TitledBorder.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Reinitialize the insets parameter with this Border's current Insets.
 * @param c the component for which this border insets value applies
 * @param insets the object to be reinitialized
 */
public Insets getBorderInsets(Component c, Insets insets) {
    Border border = getBorder();
    insets = getBorderInsets(border, c, insets);

    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        switch (getPosition()) {
            case ABOVE_TOP:
                insets.top += size.height - edge;
                break;
            case TOP: {
                if (insets.top < size.height) {
                    insets.top = size.height - edge;
                }
                break;
            }
            case BELOW_TOP:
                insets.top += size.height;
                break;
            case ABOVE_BOTTOM:
                insets.bottom += size.height;
                break;
            case BOTTOM: {
                if (insets.bottom < size.height) {
                    insets.bottom = size.height - edge;
                }
                break;
            }
            case BELOW_BOTTOM:
                insets.bottom += size.height - edge;
                break;
        }
        insets.top += edge + TEXT_SPACING;
        insets.left += edge + TEXT_SPACING;
        insets.right += edge + TEXT_SPACING;
        insets.bottom += edge + TEXT_SPACING;
    }
    return insets;
}
 
Example 16
Source File: BriefAppDescriptionBuilder.java    From raccoon4 with Apache License 2.0 4 votes vote down vote up
@Override
protected JPanel assemble() {
	JPanel panel = new JPanel();
	panel.setLayout(new GridBagLayout());
	GridBagConstraints gbc = new GridBagConstraints();

	JLabel appNameLabel = new JLabel(doc.getTitle(), SwingConstants.CENTER);
	Font font = appNameLabel.getFont();
	Font boldFont = new Font(font.getFontName(), Font.BOLD, font.getSize() + 2);
	appNameLabel.setFont(boldFont);
	appNameLabel.setToolTipText(doc.getTitle());
	Dimension tmp = appNameLabel.getPreferredSize();
	tmp.width = 150;
	appNameLabel.setPreferredSize(tmp);

	JLabel vendorNameLabel = new JLabel(doc.getCreator(), SwingConstants.CENTER);
	tmp = vendorNameLabel.getPreferredSize();
	tmp.width = 150;
	vendorNameLabel.setPreferredSize(tmp);
	vendorNameLabel.setToolTipText(doc.getCreator());

	button = new JButton();
	button.addActionListener(this);
	button.setIcon(SPINNER);

	globals.get(ImageLoaderService.class).request(this,
			DocUtil.getAppIconUrl(doc));

	JPanel stars = new StarPanel(5,
			doc.getAggregateRating().getStarRating() / 5);
	DecimalFormat df = new DecimalFormat("#.## \u2605");
	stars.setToolTipText(df.format(doc.getAggregateRating().getStarRating()));

	gbc.gridx = 0;
	gbc.gridy = 0;
	gbc.insets.bottom = 10;
	panel.add(button, gbc);

	gbc.insets.bottom = 0;
	gbc.gridy++;
	panel.add(appNameLabel, gbc);

	gbc.gridy++;
	panel.add(vendorNameLabel, gbc);

	gbc.gridy++;
	gbc.fill = GridBagConstraints.HORIZONTAL;
	gbc.insets.top = 10;
	gbc.insets.left = 15;
	gbc.insets.right = 15;
	gbc.insets.bottom = 15;
	panel.add(stars, gbc);

	return panel;
}
 
Example 17
Source File: ProductPlacemarkView.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
private int getColumnMinWith(TableColumn column, TableCellRenderer renderer, int margin) {
    final Object headerValue = column.getHeaderValue();
    final JLabel label = (JLabel) renderer.getTableCellRendererComponent(null, headerValue, false, false, 0, 0);
    return label.getPreferredSize().width + margin;
}
 
Example 18
Source File: Option.java    From CQL with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Creates a new label/component pair. If given multiple compnents, will add
 * them to a JPanel and then set that as the component. Note that this is better
 * than putting them into a JPanel yourself: the label will be centered on the
 * first component passed-in.
 *
 * @param label      the JLabel of the option
 * @param components
 */
public Option(JLabel label, JComponent... components) {
	setLabel(label);

	if (components.length > 1) {
		JPanel panel = new JPanel();
		FlowLayout flow = (FlowLayout) panel.getLayout();

		flow.setAlignment(FlowLayout.LEFT);
		flow.setVgap(0);
		flow.setHgap(0);

		for (JComponent jc : components) {
			panel.add(jc);
		}

		JUtils.fixHeight(panel);
		setComponent(panel);
	} else {
		setComponent((components.length > 0) ? components[0] : null);
	}

	// We try to align the label with the first component (descending into
	// JPanels), by creating a border that aligns the middle of the border
	// with the middle of the first component.
	if (components.length > 0) {
		int labelOffset = -label.getPreferredSize().height;
		Component comp = components[0];

		while (comp instanceof JPanel) {
			labelOffset += 2 * (((JPanel) comp).getInsets().top);
			comp = ((JPanel) comp).getComponent(0);
		}

		if (comp != null) {
			labelOffset += comp.getPreferredSize().height;

			if (labelOffset > 0) { // Only do it if the first non-JPanel
									// component is bigger
				label.setBorder(new EmptyBorder(labelOffset / 2, 0, 0, 0));
			}
		}
	}
}
 
Example 19
Source File: TitledBorder.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Reinitialize the insets parameter with this Border's current Insets.
 * @param c the component for which this border insets value applies
 * @param insets the object to be reinitialized
 */
public Insets getBorderInsets(Component c, Insets insets) {
    Border border = getBorder();
    insets = getBorderInsets(border, c, insets);

    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();

        switch (getPosition()) {
            case ABOVE_TOP:
                insets.top += size.height - edge;
                break;
            case TOP: {
                if (insets.top < size.height) {
                    insets.top = size.height - edge;
                }
                break;
            }
            case BELOW_TOP:
                insets.top += size.height;
                break;
            case ABOVE_BOTTOM:
                insets.bottom += size.height;
                break;
            case BOTTOM: {
                if (insets.bottom < size.height) {
                    insets.bottom = size.height - edge;
                }
                break;
            }
            case BELOW_BOTTOM:
                insets.bottom += size.height - edge;
                break;
        }
        insets.top += edge + TEXT_SPACING;
        insets.left += edge + TEXT_SPACING;
        insets.right += edge + TEXT_SPACING;
        insets.bottom += edge + TEXT_SPACING;
    }
    return insets;
}
 
Example 20
Source File: TitledBorder.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the baseline.
 *
 * @throws NullPointerException {@inheritDoc}
 * @throws IllegalArgumentException {@inheritDoc}
 * @see javax.swing.JComponent#getBaseline(int, int)
 * @since 1.6
 */
public int getBaseline(Component c, int width, int height) {
    if (c == null) {
        throw new NullPointerException("Must supply non-null component");
    }
    if (width < 0) {
        throw new IllegalArgumentException("Width must be >= 0");
    }
    if (height < 0) {
        throw new IllegalArgumentException("Height must be >= 0");
    }
    Border border = getBorder();
    String title = getTitle();
    if ((title != null) && !title.isEmpty()) {
        int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
        JLabel label = getLabel(c);
        Dimension size = label.getPreferredSize();
        Insets insets = getBorderInsets(border, c, new Insets(0, 0, 0, 0));

        int baseline = label.getBaseline(size.width, size.height);
        switch (getPosition()) {
            case ABOVE_TOP:
                return baseline;
            case TOP:
                insets.top = edge + (insets.top - size.height) / 2;
                return (insets.top < edge)
                        ? baseline
                        : baseline + insets.top;
            case BELOW_TOP:
                return baseline + insets.top + edge;
            case ABOVE_BOTTOM:
                return baseline + height - size.height - insets.bottom - edge;
            case BOTTOM:
                insets.bottom = edge + (insets.bottom - size.height) / 2;
                return (insets.bottom < edge)
                        ? baseline + height - size.height
                        : baseline + height - size.height + insets.bottom;
            case BELOW_BOTTOM:
                return baseline + height - size.height;
        }
    }
    return -1;
}