0

I created a JLabel containing an ImageIcon and inserted it into a JFrame with a grid layout after smoothing it to a preferred size.

It mostly worked, but the blank horizontal areas around the JLabel hogged up most of the frame as seen in this image: Outcome

Why is the JLabel ImageIcon not giving me the following outcome?

Desired Outcome

This is my renderer:

ImageIcon imageIcon = new ImageIcon(baseDir + dash + "ErrIco.png");
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(35, 35,  java.awt.Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(newimg);
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Apr 22 '17 at 04:37
  • 1
    *"is that required for it to be a good question.."* That's fairly subjective. I'd say yes. *".. or is it just advice?"* I'd say it's both. OTOH there is not one, but **two** close reasons that mention 'no MCVE', so the question you should be asking yourself is, just how much do you want an answer? I was almost tempted to turn that uncompilable code snippet into an MCVE to test further, but now I think I'll wait for your decision (& MCVE). – Andrew Thompson Apr 22 '17 at 04:42

1 Answers1

3

Try using a different layout, like BorderLayout. GridLayout will force all the cells to have the same size.

frame.setLayout(new BorderLayout());
...
frame.add(new JLabel(imageIcon), BorderLayout.WEST);
frame.add(label,                 BorderLayout.CENTER);

edit:

I see you already accepted my answer but I had put this together in the mean-time, just to see if the JLabel with a scaled image added some additional factor. (Which it didn't. It works fine.)

image example

import java.awt.*;
import javax.swing.*;
import java.net.*;
import javax.imageio.*;

class ImageExample implements Runnable {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ImageExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        JPanel content = new JPanel();
        content.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        content.setBackground(Color.white);
        frame.setContentPane(content);
        //
        frame.setLayout(new BorderLayout(20, 20));
        JLabel icon = new JLabel(new ImageIcon(img));
        JLabel text = new JLabel("<html>" +
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "<br>" +
            "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + "<br>" +
            "cccccccccccccccccccccccccccccccccccccccccc" + "</html>");
        text.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        frame.add(icon, BorderLayout.WEST);
        frame.add(text, BorderLayout.CENTER);
        //
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static final Image img;
    static {
        try {
            URL url = new URL("https://i.stack.imgur.com/7bI1Y.jpg");
            img = ImageIO.read(url).getScaledInstance(48, 48, Image.SCALE_SMOOTH);
        } catch (Exception x) {
            throw new RuntimeException(x);
        }
    }
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • A `FlowLayout` might work too. I had put together an MCVE, so I added it for good measure. – Radiodef Apr 22 '17 at 05:02
  • I thought of using Flow, but I wanted the icon to be to the left of the text, and Flow would make some text appear under the icon. – Cardinal System Apr 22 '17 at 05:03
  • I usually find that I never actually need anything other than `BorderLayout`, except for the occasional `BoxLayout` when I want stuff packed in a really rigid way. I put panels inside of panels if I want something extra. – Radiodef Apr 22 '17 at 05:05
  • The only layouts I've ever heard about were Flow and Grid, but I like your taste and I'm going to start using Border ;) – Cardinal System Apr 22 '17 at 05:05
  • 1
    There's a really great page for layouts in the tutorial here: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – Radiodef Apr 22 '17 at 05:06
  • 1
    Ooh.. nice image! That was a lovely morning. :) – Andrew Thompson Apr 22 '17 at 06:39