2012-08-27 14 views
5

Non capisco il comportamento del wrapping in un JTextPane. Se inserisco un breve testo, poi un JComponent e poi di nuovo il testo breve posso vedere le cose inserite in una riga se il frame è abbastanza grande, naturalmente. Ma se il testo è molto più lungo e richiede più righe, il componente viene sempre collocato in una nuova riga.Come avvolgere il testo attorno ai componenti in un JTextPane?

Ho riconosciuto che dopo che un componente è stato inserito in un JTextPane il suo testo si allunga di un carattere. Quindi se un componente è considerato da un JTextPane come un personaggio, perché non si comporta come un personaggio? Può dipendere dalla versione java? Io uso Java (TM) SE Runtime Environment (Build 1.7.0-B147)

Qui di seguito è il mio codice (un'istanza della currentText variabile shorttext/LongText per riprodurre il comportamento menzionato):

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

risposta

3

Questo strano comportamento sembra accadere a causa del tipo di contenuto impostato. Prova a rimuovere questa riga:

textPane.setContentType ("text/html"); 

e vedrai che tutto funziona bene dopo. Non sono sicuro del motivo per cui ciò accada: potrebbe trattarsi di un bug di rendering o solo di un comportamento previsto.

P.S. Non penso che l'uso di componenti Swing all'interno del pannello di testo (qualunque sia il motivo) è una buona opzione. Ma questa è solo la mia opinione ...

+0

Sì, funziona. Perché pensi che non sia una buona idea usare un JTextPane? Voglio creare un programma per fare esercizi. Gli esercizi devono contenere caselle combinate e campi di testo. L'utente deve anche essere in grado di "sottolineare" (selezionare) il testo. Come farlo in altro modo? Utilizza JLabels o JTextFields disabilitati? Non puoi selezionare il loro testo, vero? Anche se puoi, ho pensato che non sarebbe stato utile aggiungere tanti di questi elementi ogni volta che hai un pezzo di testo. – ka3ak

+0

@ ka3ak bene, questo potrebbe essere uno dei pochi casi in cui è il modo più semplice per fare le cose :) –

Problemi correlati