0

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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Don't use `setBounds`. Layouts do the job better. 2) Text can be wrapped in a `JLabel` using HTML formatting. 3) For better help sooner, [edit] to add a [mre]. 4) The IDE has nothing to do with the problem, no need to mention it. – Andrew Thompson Apr 13 '22 at 03:54
  • 1
    Wrap the labels text in `...` – MadProgrammer Apr 13 '22 at 04:00
  • For a (good) example of wrapping text using HTML, see [this answer](https://stackoverflow.com/a/7861833/418556). – Andrew Thompson Apr 13 '22 at 04:01

2 Answers2

4

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);
        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • `lbldescription.setColumns(40); lbldescription.setRows(3);` Using [CSS to set the width](https://stackoverflow.com/a/7861833/418556) of HTML in a `JLabel` is more convenient, IMO. – Andrew Thompson Apr 13 '22 at 04:18
  • 1
    @AndrewThompson What ever work - side benefit of using a `JTextArea` like this, is you get "copy" for free – MadProgrammer Apr 13 '22 at 04:20
  • Good point. BTW: I see a blinking cursor before the first character. You could make that disappear with `setDisabled(true)` - but then the 'easy copy' is lost. With a text field *or* label we might add a button to copy the text. – Andrew Thompson Apr 13 '22 at 04:41
  • @AndrewThompson Actually, when I took the snapshot, I hadn't applied `setEditable(false)` ... just can't be bothered to update the snapshot – MadProgrammer Apr 13 '22 at 04:45
  • 1
    *"just can't be bothered"* The amount of time I've spent looking at text input controls waiting for the moment when the cursor blinks out of view to take a screenshot . . just, no, not talkin' about it. – Andrew Thompson Apr 13 '22 at 05:09
3

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

nitind
  • 19,089
  • 4
  • 34
  • 43