No entiendo el comportamiento de envoltura en un JTextPane. Si inserto un texto corto, luego un JComponent y luego otra vez el texto breve, puedo ver el material insertado en una línea si el marco es lo suficientemente grande, por supuesto. Pero si el texto es mucho más largo, por lo que toma varias líneas, el componente siempre se coloca en una nueva línea.¿Cómo ajustar texto alrededor de componentes en un JTextPane?
He reconocido que después de insertar un componente en un JTextPane, su texto se alarga en un carácter. Entonces, si un componente es considerado por un JTextPane como un personaje, ¿por qué no se comporta como un personaje? ¿Puede depender de la versión de Java? Yo uso Java (TM) SE Runtime Environment (build 1.7.0-B147)
A continuación se muestra el código (una instancia de la variable de currentText con shortText/longtext para reproducir el comportamiento mencionado):
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
String shortText = "one two three four five six seven";
String longText = "A text component that can be marked up with attributes that are represented graphically. You can find how-to information and examples of using text panes in Using Text Components, a section in The Java Tutorial. This component models paragraphs that are composed of runs of character level attributes. Each paragraph may have a logical style attached to it which contains the default attributes to use if not overridden by attributes set on the paragraph or character run. Components and images may be embedded in the flow of text.";
String currentText = shortText;
try {
// insert text before the component
textPane.getDocument().insertString(textPane.getDocument().getLength(), currentText,
new SimpleAttributeSet());
textPane.setSelectionStart(textPane.getDocument().getLength());
textPane.setSelectionEnd(textPane.getDocument().getLength());
JComboBox component = new JComboBox();
component.setMaximumSize(component.getPreferredSize());
textPane.insertComponent(component);
// insert text after the component
textPane.getDocument().insertString(textPane.getDocument().getLength(), currentText,
new SimpleAttributeSet());
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textPane.setEditable(false);
frame.add(new JScrollPane(textPane));
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Sí, funciona. ¿Por qué crees que no es una buena idea usar un JTextPane? Quiero crear un programa para hacer ejercicios. Los ejercicios deben contener cuadros combinados y campos de texto. El usuario también debe ser capaz de "subrayar" (seleccionar) el texto. ¿Cómo hacerlo de otra manera? Use JLabels o deshabilite JTextFields? No puedes seleccionar su texto, ¿verdad? Incluso si puedes, pensé que no sería bueno agregar tantos de estos elementos cada vez que tienes una pieza de texto. – ka3ak
@ ka3ak bien, ese podría ser uno de los pocos casos cuando es la forma más fácil de hacer las cosas :) –