How to break line automatically in a text component?

0

I would like you to break automatically when you reach the maximum number of letters / words on that line.

When I put a pizza it works quietly:

ButwhenIputmorethanonepizzathetextgetsbiggerthanthejLabelthereisnottoseeallthepizzasnorthetotal:

Mycode:

privatevoidbtnAdicionarActionPerformed(java.awt.event.ActionEventevt){//Verificarseolocaldeentregafoiduplamenteselecionadoousenenhumif(((checkLocal.isSelected())&&(checkCasa.isSelected()))||((checkLocal.isSelected()==FALSE)&&(checkCasa.isSelected()==FALSE))){JOptionPane.showMessageDialog(null,"Foram escolhidos dois locais de entrega ou nenhum foi selecionado.\nPor favor verifique o local de entrega");
    }else if(checkLocal.isSelected()){

    String Entregar =checkLocal.getText();
    }else if(checkCasa.isSelected()){

    String Entregar =checkCasa.getText();
    }

    //Os dados foram verificados


    String Nome      = txtNomeVenda.getText();
    String Endereco  = txtEndereco.getText();
    String Sabor1    = (String) SaborPizza1.getSelectedItem();
    String Sabor2    = (String) SaborPizza2.getSelectedItem();   
    String Borda     = (String) BordaEscolha.getSelectedItem();
    String SaborTodo = "";


    if(Borda.equals("Nenhuma")){
    Borda = "";
    }

    if(Sabor2.equals("Nenhum")){
    SaborTodo = "1 "+Sabor1+" Borda:"+Borda;
    }else{
    SaborTodo = " 1/2"+Sabor1+" 1/2"+Sabor2+" \nBorda:"+Borda;
    }

    PizzaNota.setText(PizzaNota.getText()+"\n"+SaborTodo+"");

    txtNomeNota.setText(txtNomeVenda.getText());
}                         
    
asked by anonymous 15.07.2017 / 01:04

1 answer

3

JLabels also accept html tags , although it is a nut solution that gets bad used, will make your code difficult to read and maintain, for sporadic cases you can use the following:

PizzaNota.setText("<html>"+PizzaNota.getText()+"<br>"+SaborTodo+"</html>");

This will break the text between the two strings concatenated in JLabel .

Using JTextArea is much easier, since it already has methods that you set to break the line automatically, using JTextArea#setLineWrap , and together with this method, you can also configure the component to break the line and text correctly, avoiding breaking a word that does not fit at the end of the line, using the JTextArea#setWrapStyleWord . See the example below:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class JTextAreaQuebraLinhaTest extends JFrame {

    private JPanel contentPane;
    private JScrollPane scrollPane;
    private JTextArea textArea;

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new JTextAreaQuebraLinhaTest().setVisible(true);
        });
    }

    public JTextAreaQuebraLinhaTest() {
        initComponents();
    }

    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(450, 300));

        this.contentPane = new JPanel();
        this.contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        setContentPane(this.contentPane);

        this.textArea = new JTextArea(5, 15);
        // quebra a linha ao chegar no limite
        // da textarea
        this.textArea.setLineWrap(true);
        // define a quebra de linha sem quebrar
        // a palavra no final da linha, caso
        // nao caiba inteira
        this.textArea.setWrapStyleWord(true);

        this.scrollPane = new JScrollPane(this.textArea);
        this.contentPane.add(this.scrollPane);

        pack();
        setLocationRelativeTo(null);
    }
}

Testing the line break:

    
15.07.2017 / 01:29