Resize a picture to fit a JLabel

Outline Here are the steps to follow. Read the picture as a BufferedImage. Resize the BufferedImage to another BufferedImage that’s the size of the JLabel. Create an ImageIcon from the resized BufferedImage. You do not have to set the preferred size of the JLabel. Once you’ve scaled the image to the size you want, the … Read more

Align text in JLabel to the right

This can be done in two ways. JLabel Horizontal Alignment You can use the JLabel constructor: JLabel(String text, int horizontalAlignment) To align to the right: JLabel label = new JLabel(“Telephone”, SwingConstants.RIGHT); JLabel also has setHorizontalAlignment: label.setHorizontalAlignment(SwingConstants.RIGHT); This assumes the component takes up the whole width in the container. Using Layout A different approach is to … Read more

Multiline text in JLabel

You can do it by putting HTML in the code, so: JFrame frame = new JFrame(); frame.setLayout(new GridLayout()); JLabel label = new JLabel(“<html>First line<br>Second line</html>”); frame.add(label); frame.pack(); frame.setVisible(true);

How to add hyperlink in JLabel?

You can do this using a JLabel, but an alternative would be to style a JButton. That way, you don’t have to worry about accessibility and can just fire events using an ActionListener. public static void main(String[] args) throws URISyntaxException { final URI uri = new URI(“http://java.sun.com”); class OpenUrlAction implements ActionListener { @Override public void … Read more