javaswingjpanelimageicon

How to move a imageIcon on the left side of JPanel?


I'm trying to move ImageIcon torrentIcon to the left side of JPanel jp, because it's located in the center of panel near the JLabel jl. Neither setBounds() nor setHorizontalTextPosition(), setVerticalTextPosition() methods didn't help with that. I also tried making it by adding torrenIcon to another imageL JLabel and moving by setLocation(), but it didn`t work too. What should I do then?

This is a code part of creating panel:

private static JPanel setPanel() {
    JLabel jl = new JLabel("Авторизуйтесь на сумнівний сайт");
    jl.setForeground(Color.white);
    jl.setFont(new Font("Times New Roman", Font.PLAIN, 40));

    ImageIcon torrentIcon = new ImageIcon("Icon.png");
    Image im = torrentIcon.getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH);
    torrentIcon = new ImageIcon(im);

    JLabel imageL = new JLabel();
    imageL.setIcon(torrentIcon);
    imageL.setHorizontalTextPosition(SwingConstants.LEFT);
    imageL.setVerticalTextPosition(SwingConstants.CENTER);

    jl.setHorizontalTextPosition(0);
    jl.setVerticalTextPosition(0);
    
    JPanel p = new JPanel();
    p.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    p.setBackground(Color.RED);
    p.add(imageL);
    p.add(jl);

    return p;
}

Solution

  • I took your code and created the following GUI.

    Example

    Don't use static methods with Swing.

    Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the Laying Out Components Within a Container section.

    When you type:

    JPanel panel = new JPanel();
    

    the default is a FlowLayout. The Swing components will "flow" from left to right.

    Here's the same statement with all of the default values explicitly defined.

    JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
    

    I read the icon from a URL, so anyone can run this code and get the same result.

    Here's the complete runnable code. Save this so you can easily test different JPanels.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Image;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class LayoutExample implements Runnable {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new LayoutExample());
        }
    
        private Image image;
    
        public LayoutExample() {
            URL url;
            try {
                url = new URL("https://vectorified.com/images/torrent-icon-27.png");
                this.image = ImageIO.read(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void run() {
            JFrame frame = new JFrame("Layout Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.add(createMainPanel(), BorderLayout.CENTER);
    
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JPanel createMainPanel() {
            JLabel jl = new JLabel("Авторизуйтесь на сумнівний сайт");
            jl.setForeground(Color.white);
            jl.setFont(new Font("Times New Roman", Font.PLAIN, 40));
    
            Image im = image.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
            ImageIcon torrentIcon = new ImageIcon(im);
    
            JLabel imageL = new JLabel();
            imageL.setIcon(torrentIcon);
    
            JPanel p = new JPanel();
            p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
            p.setBackground(Color.RED);
            p.add(imageL);
            p.add(jl);
    
            return p;
        }
    
    }