Home > database >  Text in JLabel goes beyond the panel bounds set
Text in JLabel goes beyond the panel bounds set

Time:04-13

I'm trying to make a loan form but text in the JLabel goes out of the panel.setBounds. I tried using FlowLayout for the panel but it didn't work as I wanted it to.

What I want it to be:

enter image description here

But here's what I got:

enter image description here

The code I used:

lblloanform = new JLabel("Loan Application Form");
lblloanform.setBounds(30, 10, 400, 50);
lblloanform.setFont(new Font("Italic",Font.BOLD,25));
add(lblloanform);
        
pnl1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
pnl1.setSize(550,75);
pnl1.setBounds(30, 60, 550, 75);
pnl1.setBackground(Color.cyan);
pnl1.setVisible(true);
add(pnl1);
        
lbldescription = new JLabel("Please fill in all needed information in the loan application form below to request a loan from your organization. Information regarding income assets are requested for qualification");
pnl1.add(lbldescription);

CodePudding user response:

JLabels are for short amounts of text, without line wrapping. You should use a JTextArea instead.

CodePudding user response:

Wrap the label text in <html>...</html> tags...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JLabel lblloanform = new JLabel("Loan Application Form");
            lblloanform.setBorder(new EmptyBorder(8, 8, 8, 8));
            lblloanform.setFont(new Font("Italic", Font.BOLD, 25));

            JPanel pnl1 = new JPanel(new BorderLayout());
            pnl1.setBorder(new EmptyBorder(8, 8, 8, 8));
            pnl1.setBackground(Color.cyan);

            JLabel lbldescription = new JLabel("<html>Please fill in all needed information in the loan application form below to request a loan from your organization. Information regarding income assets are requested for qualification</html>");
            lbldescription.setVerticalAlignment(JLabel.TOP);
            pnl1.add(lbldescription);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(lblloanform, gbc);
            gbc.weighty = 1;
            add(pnl1, gbc);
        }

    }
}

or just use a JTextArea, with rows and columns properties gives you a little more control over the preferred size...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JLabel lblloanform = new JLabel("Loan Application Form");
            lblloanform.setBorder(new EmptyBorder(8, 8, 8, 8));
            lblloanform.setFont(new Font("Italic", Font.BOLD, 25));

            JPanel pnl1 = new JPanel(new BorderLayout());
            pnl1.setBorder(new EmptyBorder(8, 8, 8, 8));
            pnl1.setBackground(Color.cyan);

            JTextArea lbldescription = new JTextArea("Please fill in all needed information in the loan application form below to request a loan from your organization. Information regarding income assets are requested for qualification");
            lbldescription.setLineWrap(true);
            lbldescription.setWrapStyleWord(true);
            lbldescription.setOpaque(false);
            lbldescription.setColumns(40);
            lbldescription.setRows(3);
            lbldescription.setEditable(false);
            pnl1.add(lbldescription);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(lblloanform, gbc);
            gbc.weighty = 1;
            add(pnl1, gbc);
        }

    }
}
  • Related