JTextPane textPane = new JTextPane();
JPanel noWrapPanel = new JPanel( new BorderLayout() );
noWrapPanel.add( textPane );
JScrollPane scrollPane = new JScrollPane( noWrapPanel );
ソリューション ソース
プラス:
JScrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER);
JScrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
編集:
なしのソリューション JScrollPane
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
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
@Override
public boolean getScrollableTracksViewportWidth() {
Component parent = getParent();
ComponentUI ui = this.getUI();
return parent != null ? (ui.getPreferredSize(this).width <= parent.getSize().width) : true;
}
// Test method
public static void main(String[] args) {
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");
JPanel p = new JPanel();
NonWrappingTextPane nwtp = new NonWrappingTextPane();
nwtp.setText(content);
p.add(nwtp);
f.getContentPane().setLayout(new GridLayout(2, 1));
f.getContentPane().add(p);
f.setSize(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}