Source Code : Non Wrapping(Wrap) TextPane

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Non Wrapping(Wrap) TextPane

Non Wrapping(Wrap) TextPane
    
/*
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
"
        + ""Eagle" reads:

"
        + ""Here men from the planet Earth first
"
        + "set foot upon the Moon, July, 1969 AD
"
        + "We came in peace for all mankind."

"
        + "It is signed by the astronauts and the
"
        + "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);
  }
}


           
         
    
    
    
  

Thank with us