Example usage for javax.swing JLabel getPreferredSize

List of usage examples for javax.swing JLabel getPreferredSize

Introduction

In this page you can find the example usage for javax.swing JLabel getPreferredSize.

Prototype

@Transient
public Dimension getPreferredSize() 

Source Link

Document

If the preferredSize has been set to a non-null value just returns it.

Usage

From source file:Main.java

public static void main(String[] args) {
    String s = "The quick brown fox jumps over the lazy dog!";
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D b = fm.getStringBounds(s, g);
    System.out.println(b);/*w w  w.j  a  v  a  2  s  .c om*/
    bi = new BufferedImage((int) b.getWidth(), (int) (b.getHeight() + fm.getDescent()),
            BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.drawString(s, 0, (int) b.getHeight());

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));

    // Technique 3 - JLabel
    JLabel l = new JLabel(s);
    l.setSize(l.getPreferredSize());
    bi = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 400, 100);
    l.paint(g);

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = (JPanel) frame.getContentPane();
    panel.setLayout(null);/*w  ww .j a  v a2s .  co  m*/

    JLabel label = new JLabel("aaa");
    panel.add(label);
    Dimension size = label.getPreferredSize();
    label.setBounds(100, 100, size.width, size.height);

    frame.setSize(300, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
            Graphics2D imageGraphics = image.createGraphics();
            GradientPaint gp = new GradientPaint(20f, 20f, Color.red, 380f, 280f, Color.orange);
            imageGraphics.setPaint(gp);/*from ww  w .ja  v  a 2s.co  m*/
            imageGraphics.fillRect(0, 0, 400, 300);

            JLabel textLabel = new JLabel("java2s.com");
            textLabel.setSize(textLabel.getPreferredSize());

            Dimension d = textLabel.getPreferredSize();
            BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.createGraphics();
            g.setColor(new Color(255, 200, 255, 128));
            g.fillRoundRect(0, 0, bi.getWidth(f), bi.getHeight(f), 15, 10);
            g.setColor(Color.black);
            textLabel.paint(g);
            Graphics g2 = image.getGraphics();
            g2.drawImage(bi, 20, 20, f);

            ImageIcon ii = new ImageIcon(image);
            JLabel imageLabel = new JLabel(ii);

            f.getContentPane().add(imageLabel);
            f.pack();

            f.setVisible(true);
        }
    });
}

From source file:JColorChooserWithCustomPreviewPanel.java

public static void main(String[] a) {

    final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
    previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    previewLabel.setSize(previewLabel.getPreferredSize());
    previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0));

    JColorChooser colorChooser = new JColorChooser();
    colorChooser.setPreviewPanel(previewLabel);

    JDialog d = colorChooser.createDialog(null, "", true, colorChooser, null, null);

    d.setVisible(true);//from   ww w .  j a v  a2 s .  c  o  m
}

From source file:MainClass.java

public static void main(String[] a) {

    final JColorChooser colorChooser = new JColorChooser();
    final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
    previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    previewLabel.setSize(previewLabel.getPreferredSize());
    previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0));
    colorChooser.setPreviewPanel(previewLabel);

    ActionListener okActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("OK Button");
            System.out.println(colorChooser.getColor());
        }/*from  w  w  w . jav a  2s.c o  m*/
    };

    ActionListener cancelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Cancel Button");
        }
    };

    final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser,
            okActionListener, cancelActionListener);

    dialog.setVisible(true);
}

From source file:Main.java

/**
 * Returns the preferred size for a label.
 * /*from   w w w  .  j  a  v  a2 s.c om*/
 * @param label The label.
 * @param sampleText The sample text used to get the size.
 * @return The preferred size.
 */
public static Dimension getLabelPreferredSize(JLabel label, String sampleText) {
    String text = label.getText();
    label.setText(sampleText);
    Dimension size = label.getPreferredSize();
    label.setText(text);
    return size;
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

public static void updateImage(JLabel imageContainer, File imageFile, Format format) {
    if (imageFile == null || !imageFile.exists()) {
        return;//from  w  w  w .j  a va 2  s .  c  om
    }
    BufferedImage img = null;
    try {
        img = ImageIO.read(imageFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (img == null) {
        return;
    }
    int imageWidth = img.getWidth();
    int imageHeight = img.getHeight();
    int imageViewWidth = (int) imageContainer.getPreferredSize().getWidth();
    int imageViewHeight = (int) imageContainer.getPreferredSize().getHeight();
    double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight),
            new Dimension(imageViewWidth, imageViewHeight));
    imageWidth = (int) (factor * imageWidth);
    imageHeight = (int) (factor * imageHeight);
    if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) {
        return;
    }
    BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    int x = (imageViewWidth - imageWidth) / 2;
    int y = (imageViewHeight - imageHeight) / 2;
    if (format == Format.PNG || format == Format.XML) {
        g2.drawImage(img, x, y, imageWidth, imageHeight, null);
    } else {
        g2.drawImage(img, x, y, imageWidth, imageHeight, Color.WHITE, null);
    }
    g2.dispose();
    imageContainer.setIcon(new ImageIcon(tmp));
}

From source file:generadorqr.jifrNuevoQr.java

public static void Mostrar_Visualizador(JLabel Pantalla, String RutaDestino) {
    try {/*from  w  w w. ja v a2s  .  co  m*/
        Image capturarImgSoloLectura = ImageIO.read(new File(RutaDestino));
        Image obtenerImagen = capturarImgSoloLectura.getScaledInstance(Pantalla.getPreferredSize().width,
                Pantalla.getPreferredSize().height, Image.SCALE_SMOOTH);
        Icon iconoEscalado = new ImageIcon(obtenerImagen);
        Pantalla.setIcon(iconoEscalado);
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel p = new JPanel(new BorderLayout());
    JTextField tf = new JTextField(5);
    JLabel label = new JLabel(new ImageIcon("Test.gif"));
    label.setOpaque(true);//from   w w  w . ja  v  a 2s .  c o  m
    label.setBackground(tf.getBackground());
    label.setPreferredSize(new Dimension(label.getPreferredSize().width, tf.getPreferredSize().height));
    p.setBorder(tf.getBorder());
    tf.setBorder(null);
    p.add(label, BorderLayout.WEST);
    p.add(tf, BorderLayout.CENTER);
    JPanel p1 = new JPanel();
    p1.add(p);
    getContentPane().add(p1);
    pack();
    setLocationRelativeTo(null);
}

From source file:generadorqr.jifrGestionArticulos.java

public static void MostrarVisualizador(JLabel Pantalla, String RutaDestino) {
    try {/* w  w  w  . j  av  a  2 s . c  o m*/
        Image capturarImgSoloLectura = ImageIO.read(new File(RutaDestino));
        Image obtenerImagen = capturarImgSoloLectura.getScaledInstance(Pantalla.getPreferredSize().width,
                Pantalla.getPreferredSize().height - 10, Image.SCALE_SMOOTH);
        Icon iconoEscalado = new ImageIcon(obtenerImagen);
        Pantalla.setIcon(iconoEscalado);
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }
}