Szekuns
Szekuns

Reputation: 41

JLabel display too short for the whole text

This is an example of JLabel displayed too short and has not enough space for the "website" (refer to the code) part.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JLabeleg extends JFrame
{
public JLabeleg()
{
    setTitle("Example");
    setSize(500,100);
    setVisible(true);
    setLocationRelativeTo(null);
    setLayout(new BorderLayout());

    Container eg = getContentPane();
    JPanel west = new JPanel(new GridLayout(2,1));
    JPanel center = new JPanel(new GridLayout(2,3));

    eg.add(west,BorderLayout.WEST);
    eg.add(center,BorderLayout.CENTER);


    west.add(new JLabel("Name         :  "));
    west.add(new JLabel("Website     :  "));

    center.add(new JLabel("Szekuns"));
    center.add(new JLabel(""));
    center.add(new JLabel(""));
    center.add(new JLabel("www.example.com.my/example/example"));
    center.add(new JLabel(""));
    center.add(new JLabel(""));

}

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

What is different between my original code and this example is that the source of JLabel is actually a "setText" from an Object load from an IO file. Does that matter? Is there anyway for the "www.example.com/example/example" to have a full view? Maybe by taking the space of the JLabel on the right hand side?

Upvotes: 3

Views: 2919

Answers (1)

Guillaume Polet
Guillaume Polet

Reputation: 47608

One solution is to use pack() after loading the text of your labels. I don't like much this solution because I find it disturbing for the user.

GridLayout will also soon show you some important limitations (if you have one big label and others are quite small, a lot of free space will get lost, since all labels will get the size of the biggest label). In all cases, I would recommend to only set the number of "rows" or the number of "columns" of the GridLayout, this is easier to maintain.

GridBagLayout might be an alternative but takes a wee bit more experience to master.

Finally, a JTable might be a better option in your situation (but we lack the context of what you are trying to achieve).

Here are 2 examples showing how you can get things done (but there are many other possibilities which might be better, explaining your target would help us be more precise).

Example 1 (using pack() and GridLayout):

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JLabeleg extends JFrame {

    private List<JLabel> labels = new ArrayList<JLabel>();

    public JLabeleg() {
        setTitle("Example");
        setLayout(new BorderLayout());

        Container eg = getContentPane();
        JPanel west = new JPanel(new GridLayout(2, 1));
        final JPanel center = new JPanel(new GridLayout(0, 3));
        JButton load = new JButton("Load labels");
        load.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int i = 0;
                for (JLabel label : labels) {
                    label.setText("A very very very long long long long label " + String.valueOf(++i));
                }
                JLabeleg.this.pack();
                JLabeleg.this.setLocationRelativeTo(null);
            }
        });
        eg.add(west, BorderLayout.WEST);
        eg.add(center, BorderLayout.CENTER);
        eg.add(load, BorderLayout.SOUTH);

        west.add(new JLabel("Name         :  "));
        west.add(new JLabel("Website     :  "));

        center.add(new JLabel("Szekuns"));
        center.add(new JLabel(""));
        center.add(new JLabel(""));
        center.add(new JLabel("www.example.com.my/example/example"));
        center.add(new JLabel(""));
        center.add(new JLabel(""));
        for (int i = 0; i < 12; i++) {
            JLabel label = new JLabel();
            center.add(label);
            labels.add(label);
    }
    pack();
    setLocationRelativeTo(null);
    setVisible(true);

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new JLabeleg();
        }
    });

}
}

Example 2 (using GridBagLayout and revalidate())

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JLabeleg extends JFrame {

    private List<JLabel> labels = new ArrayList<JLabel>();

    public JLabeleg() {
        setTitle("Example");
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container eg = getContentPane();
        JPanel west = new JPanel(new GridLayout(0, 2));
        final JPanel center = new JPanel(new GridBagLayout());
        JButton load = new JButton("Load labels");
        load.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int i = 0;
                for (JLabel label : labels) {
                    label.setText("A very long label " + String.valueOf(++i));
                }
                center.revalidate();
            }
        });
        eg.add(west, BorderLayout.WEST);
        eg.add(center, BorderLayout.CENTER);
        eg.add(load, BorderLayout.SOUTH);

        west.add(new JLabel("Name         :  "));
        west.add(new JLabel("Szekuns"));
        west.add(new JLabel("Website     :  "));
        west.add(new JLabel("www.example.com"));
        GridBagConstraints gbc = new GridBagConstraints();
        GridBagConstraints endOfRow = new GridBagConstraints();
        endOfRow.gridwidth = GridBagConstraints.REMAINDER;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                JLabel label = new JLabel();
                if (j + 1 < 4) {
                    center.add(label, gbc);
                } else {
                    center.add(label, endOfRow);
                }
                labels.add(label);
            }
        }
        setSize(1000, 300);
        setLocationRelativeTo(null);
        setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JLabeleg();
            }
        });

    }
}

Upvotes: 4

Related Questions