Java swing: ¿Etiquetas multilínea? [duplicar]


Posible Duplicado:
Texto multilínea en JLabel

Quiero hacer esto:

JLabel myLabel = new JLabel();
myLabel.setText("This is\na multi-line string");

Actualmente esto resulta en una etiqueta que muestra

This isa multi-line string

Quiero que haga esto en su lugar:

This is
a multi-line string

Alguna sugerencia?

Gracias


EDITAR: Solución implementada

En el cuerpo del método:

myLabel.setText(convertToMultiline("This is\na multi-line string"));

Método de ayuda:

public static String convertToMultiline(String orig)
{
    return "<html>" + orig.replaceAll("\n", "<br>");
}
Author: Community, 2010-01-28

7 answers

Puede usar HTML en JLabels. Para usarlo, su texto tiene que comenzar con <html>.

Establezca su texto en "<html>This is<br>a multi-line string" y debería funcionar.

Ver Tutorial de Swing: JLabely Etiqueta multilínea (HTML) para más información.

 60
Author: Peter Lang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-28 06:46:40
public class JMultilineLabel extends JTextArea{
    private static final long serialVersionUID = 1L;
    public JMultilineLabel(String text){
        super(text);
        setEditable(false);  
        setCursor(null);  
        setOpaque(false);  
        setFocusable(false);  
        setFont(UIManager.getFont("Label.font"));      
        setWrapStyleWord(true);  
        setLineWrap(true);
    }
} 

Se ve totalmente igual para mí, pero es feo

 11
Author: Whimusical,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-14 13:42:02

Otra forma fácil (pero cambia un poco el estilo del texto) es usar un bloque html <pre></pre>.

Esto persistirá cualquier formato que el usuario haya introducido si la cadena que está utilizando proviene de un cuadro de entrada de usuario.

Ejemplo:

JLabel label = new JLabel("<html><pre>First Line\nSecond Line</pre></html>"); 
 6
Author: Austyn Mahoney,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-25 02:00:51

El procedimiento directo de escribir un texto multilínea en un jlabel es:

JLabel label = new JLabel("<html>First Line<br>Second Line</html>"); 
 5
Author: Marco Schmid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-28 08:38:21

El problema con el uso de html en JLabel o cualquier componente Swing es que luego tiene que darle estilo como html, no con el habitual setFont, setForeground, etc. Si estás de acuerdo con eso, bien.

De lo contrario, puede usar algo como MultilineLabel de JIDE, que se extiende JTextArea. Es parte de su capa de código abierto Commom.

 5
Author: Geoffrey Zheng,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-25 19:30:50
 3
Author: Viachaslau Kamkou,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-28 08:36:46

JLabel puede aceptar código html. Tal vez puedas intentar usar la etiqueta <br>.

Ejemplo:

JLabel myLabel = new JLabel();
myLabel.setText("<html> This is a <br> multi-line string </html>");
 2
Author: KWAN TOH CHOONG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-12 16:27:42