How to set fix size of jlabel?
Asked Answered
K

4

16

I am trying to make a java desktop application where I am using multiple JLabel I want to set fix height and width of JLabel. How can I achieve this?

Here is my code

public class Second extends JFrame
{
    JPanel panel1,panel2;
    JLabel label=new JLabel();
    ArrayList<JLabel> lbllist = new ArrayList<>();

    public Second()  
    {
        super("Simple Timer");
        {
            getContentPane().setBackground(new java.awt.Color(255,255,255));
        } 
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Container c = getContentPane();
        c.setLayout( new FlowLayout());
        setUndecorated(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0,0,screenSize.width, screenSize.height);

        panel2=new JPanel();
        panel2.setBounds(360, 180, 360, 1020);
        panel2.setBackground(new java.awt.Color(255,153,51));
        c.add(panel2);
        JPanel panel3 = createPanel();
        panel1.setLayout(new GridBagLayout());
        panel1.add(panel3);

        JPanel panel4 = createPanel(); 
        panel2.setLayout(new GridBagLayout());
    //  jPanel2.setLayout(null);
        panel2.add(panel4);

        setLayout(null);
   }

   private JPanel createPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 1, 10, 5));
        EmptyBorder panelBorder = new EmptyBorder(10, 5, 10, 10);
        panel.setBorder(panelBorder);
        panel.setBackground(new java.awt.Color(255, 153, 51));
        panel.setOpaque(true);
        EmptyBorder border1 = new EmptyBorder(5, 20, 15, 18);

        Border border = BorderFactory.createLineBorder(Color.BLUE, 2);
        for (int i = 0; i <11; i++) {
            label = new JLabel("<html>Case &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CaseNum<br><font color=yellow>Party1<br>Party2</font></html>");
            label.setFont(new java.awt.Font("Times New Roman", 1, 18));
            label.setBorder(border);
            label.setSize(300, 100);
            label.setBorder(border1);
            Dimension d = label.getPreferredSize();
            label.setBackground(Color.GRAY);
            label.setForeground(new java.awt.Color(255, 255,255 ));
            label.setOpaque(true);
            lbllist.add(label);
            panel.add(label);
        }
        return panel;
    }

When I reduce text size or text from JLabel then size of JLabel reduced but I want that when i reduced or remove any text from JLabel but size of Jlabel should not reduced it should fix

How can I get this?

Thanks in advance

Kwangju answered 7/4, 2014 at 18:16 Comment(1)
A
20

You can set a fixed the size by setting the minimum, preferred and maximum size:

setMinimumSize(width, height);
setPreferredSize(width, height);
setMaximumSize(width, height);

As the Link from MadProgrammer, you need to override these methods, not using them from outside, based on the reasons mentioned in this link.

Ambience answered 7/4, 2014 at 18:20 Comment(8)
i will have to use all three or any oneKwangju
You can use setMaximumSize, but i want to show you what the API offer.Ambience
To be clear, you want fixed size for the width of JLabel or its text contents?Ambience
i want to fix size of j label and it should not reduced when i remove text or content form jlabelKwangju
Then try to use the method above all.Ambience
i used but when im remove any content from jlabel then sized of jlabel reducedKwangju
You may wish to consider taking a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?Dannadannel
setPreferedSize must be setPreferredSize and (width, height) must be (new Dimension(width, height))Lucienlucienne
S
5

if this method not work

setMinimumSize(width, height);
setPreferredSize(width, height);
setMaximumSize(width, height);

then use it

setMinimumSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
Shiftless answered 27/6, 2015 at 6:34 Comment(0)
R
2

Use JLabel.setPreferredSize(width, height); The jpanel does not have a layout, try Jpanel pane =new JPanel(new FlowLayout());

Roundfaced answered 7/4, 2014 at 18:24 Comment(0)
L
2

An easy work around (without overriding existing classes) is to;

  • Create a JPanel specifically for the component

  • Set the size for the JPanel

  • Place the component within the JPanel

  • Place the JPanel where you would have originally placed the component

e.g.

JPanel mainPanel = new JPanel(); //Assume this is the panel you are placing things into.

JLabel nameLabel = new JLabel("Name:");
JPanel nameLabelPanel = new JPanel();
nameLabelPanel.setPreferredSize(new Dimension(100, 25));
JPanel.add(nameLabel);
mainPanel.add(nameLabelPanel);
Lowe answered 18/7, 2017 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.