Mega Code Archive

 
Categories / Java / Swing JFC
 

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