14

I want to display a Multiline JLabel into JPanel. So I have this code but I'm not able to show the multiline JLabel.

public class NotificationFrame extends JFrame{
    public NotificationFrame(){
        JPanel panelBody = new JPanel();
        panelBody.setBackground(Color.white);
        GridBagConstraints GBC2 = new GridBagConstraints();
        Container CR2 = new Container();
        GridBagLayout GBL2 = new GridBagLayout();
        CR2.setLayout(GBL2);     
        panelBody.add(CR2);

        GBC2 = new GridBagConstraints();
        CR2.add(labelTesto);
        GBC2.gridx=0;
        GBC2.gridy=0;
        GBC2.insets.left = 10;
        GBC2.insets.top=0;
        GBL2.setConstraints(labelTesto,GBC2);
        panelBody.setLayout(new FlowLayout(FlowLayout.CENTER)); 


        add(panelBody,BorderLayout.CENTER);
    }
}

If I change the last line of code in

add(labelTest,BorderLayout.CENTER);

I can show that I want. But it is not correct because I want to set a padding on JLabel

EDIT

I have use this code now:

JPanel panelBody = new JPanel();
panelBody.setBackground(Color.white);
SpringLayout layout = new SpringLayout();
panelBody.setLayout(layout);
panelBody.add(labelTesto);
layout.putConstraint(SpringLayout.NORTH, labelTesto, 15, SpringLayout.NORTH, panelBody);
add(panelBody,BorderLayout.CENTER);

This is the layout:

enter image description here

This is the all test that I should see: "Il 31 Dicembre scadrà l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti."

bircastri
  • 2,169
  • 13
  • 50
  • 119
  • 2
    For best help: Post an image of what you desire and one of what you're getting, and create and post an [mcve]. – Hovercraft Full Of Eels Oct 16 '15 at 14:14
  • 2
    I've never seen `new Container()` in code before. I suggest you change it to `new JPanel()`. – VGR Oct 16 '15 at 14:18
  • 1
    *"I want to set a padding on JLabel"* Add an `EmptyBorder` to the label. But for a good chance of things better than that comment, follow the sage advice of @HovercraftFullOfEels .. – Andrew Thompson Oct 16 '15 at 14:18
  • Variable names should NOT start with an upper case character. Follow Java conventions when posting code. – camickr Oct 16 '15 at 14:23

1 Answers1

34

Multiline it's not padding. For achieve multiline text in JLabel or JButton you need to use HTML.

To wrap text (or multiline):

btn.setText("<html><p>Il 31 Dicembre scadrà l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti</p></html>");
label.setText("<html>Date:<br></html>"+getDateFromSomeWhere);

To set padding:

    label.setBorder(new EmptyBorder(0,10,0,0));//top,left,bottom,right
tec
  • 999
  • 3
  • 18
  • 40
  • It worked, Now I want to give some border with Rounded corners, how can we do it – Kunal Tyagi Jul 11 '20 at 10:10
  • There are plenty of examples over internet. [This is one of them.](https://stackoverflow.com/questions/15025092/border-with-rounded-corners-transparency) – tec Jul 12 '20 at 11:47