Swing JEditorPane ohne Zeilenumbruch nach InputStream

babuschka

Top Contributor
Hallo zusammen,

ich habe ein JEditorPane in ein JScrollPane eingebettet, wobei das editorpane einen InputStream liest. Beim Anzeigen wird nun bei längeren Zeilen nicht die horizontale ScrollBar eingeblendet, sondern der Text am Ende des EditorPanes umgebrochen. Ebenso ist es, wenn man den Text nachträglich erweitert.
Vergrößert n verkleinert anschließend das Fenster, so wird der Scrollbalken angezeigt.
Ich hab es auch schon mit
Java:
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
versucht und den Hilfen aus dieser Diskussion.
Google hat mich irgendwie auch nicht weitergebracht, aber vielleicht wusste ich auch bloß nicht, was ich als richtige Suchwörter eingeben müsste.

Hier der wesentliche Code:
Java:
JEditorPane editor = new JEditorPane();
            
            JScrollPane scroller = new JScrollPane(editor);
            scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            try{
                editor.read(ftp.retrieveFileStream(fileName), null);
                ftp.completePendingCommand();
            }
            catch(IOException ix){
                editor.setText("unable to read file");
            }
            
            tabPane.addTab(fileName, scroller);

wie kann ich denn nun das EditorPane dazu bringen, sofort den Text ohne Umbruch weiterzuschreiben, sodass der horizontale Scrollbalken genutzt wird.

Vielen Dank schon mal im vorraus!
 
Hi mafbo,

kannst Du ein
Code:
JTextArea
statt eines
Code:
JEditorPane
nehmen?
Dann könntest Du das gewünschte Verhalten einschalten:
Java:
editor = new JTextArea();
editor.setLineWrap(false);

Grüße
Momolin
 
Mal länger als 5 min gegooglet, und folgendes gefunden:
Java:
/*
Core SWING Advanced Programming 
By Kim Topley
ISBN: 0 13 083292 8       
Publisher: Prentice Hall  
*/

import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.text.StyledDocument;

public class NonWrappingTextPane extends JTextPane {
  public NonWrappingTextPane() {
    super();
  }

  public NonWrappingTextPane(StyledDocument doc) {
    super(doc);
  }

  // Override getScrollableTracksViewportWidth
  // to preserve the full width of the text
  public boolean getScrollableTracksViewportWidth() {
    Component parent = getParent();
    ComponentUI ui = getUI();

    return parent != null ? (ui.getPreferredSize(this).width <= parent
        .getSize().width) : true;
  }

  // Test method
  public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {}
  
    String content = "The plaque on the Apollo 11 Lunar Module\n"
        + "\"Eagle\" reads:\n\n"
        + "\"Here men from the planet Earth first\n"
        + "set foot upon the Moon, July, 1969 AD\n"
        + "We came in peace for all mankind.\"\n\n"
        + "It is signed by the astronauts and the\n"
        + "President of the United States.";
    JFrame f = new JFrame("Non-wrapping Text Pane Example");
    JTextPane tp = new JTextPane();
    tp.setText(content);

    NonWrappingTextPane nwtp = new NonWrappingTextPane();
    nwtp.setText(content);

    f.getContentPane().setLayout(new GridLayout(2, 1));
    f.getContentPane().add(new JScrollPane(tp));
    f.getContentPane().add(new JScrollPane(nwtp));

    f.setSize(300, 200);
    f.setVisible(true);
  }
}

Quelle: Non Wrapping(Wrap) TextPane : TextFieldSwing JFCJava
 

Zurück
Oben