Search code examples
javaswingjlabellayout-managergridbaglayout

swing - JLabel in GridBagLayout with long text


I'm trying to design a popup to notify receiving a new e-mail. This is the code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author luca
 */
public class Popup extends JDialog {


    public Popup() {
        super.setSize(260, 100);

        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets = new Insets(5, 5, 5, 5);


        JLabel header = new JLabel("Hai ricevuto una nuova email");
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.anchor = GridBagConstraints.WEST;
        add(header, gbc);

        JLabel mittente = new JLabel("[email protected]");
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        add(mittente, gbc);

        JLabel argomento = new JLabel("info voto esame");
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridheight = 1;
        add(argomento, gbc);

        JLabel icona = new JLabel(new ImageIcon("img/email.png"));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 3;
        gbc.fill = GridBagConstraints.BOTH;
        add(icona, gbc);


        this.setLocation(400, 400);
        this.setUndecorated(true);
        this.setVisible(true);
        mittente.setMaximumSize(new Dimension(180, 16));
    }

    public static void main(String args[]){
        new Popup();
    }

}

When the text in the labels isn't too long everything works fine, i get something like this: enter image description here

But when the text is too long, this is what i get: enter image description here

Is there a way of getting something like this? enter image description here

I found other questions about this thing and i tried to work on label.setMaximumSize(), i tried to write text with html tags...But nothing worked out. Can someone help me?


Solution

  • In this case it helps to set the weightx of the image to 1, while everything else gets a 0. There are also various other changes. Inspect the code closely for the changes.

    I would also recommend setting the full text as the tool tip for each label. If the user wants to see the entire details, they just need to point the mouse at the label.

    enter image description here

    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    
    public class Popup extends JDialog {
    
        public Popup() {
            this.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
    
            gbc.insets = new Insets(5, 5, 5, 5);
    
            JLabel header = new JLabel("Hai ricevuto una nuova email");
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.anchor = GridBagConstraints.WEST;
            add(header, gbc);
    
            gbc.weightx = 0;
            JLabel mittente = new JLabel("[email protected]");
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridheight = 1;
            add(mittente, gbc);
    
            JLabel argomento = new JLabel("info voto esame");
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.gridheight = 1;
            add(argomento, gbc);
    
            JLabel icona = new JLabel(new ImageIcon(
                    new BufferedImage(40,60,BufferedImage.TYPE_INT_RGB)));
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridheight = 3;
            gbc.fill = GridBagConstraints.BOTH;
            add(icona, gbc);
    
            this.setLocation(400, 400);
            this.setUndecorated(true);
            pack();
            super.setSize(260, 100);
            this.setVisible(true);
            mittente.setMaximumSize(new Dimension(180, 16));
        }
    
        public static void main(String args[]) {
            new Popup();
        }
    }