user3275944
user3275944

Reputation: 57

Disable automatic scroll

I have added a JScrollPane to a JTextPane , i have try to set the vertical bar to zero , thus it will appear as it was not scroll. In fact my aim is to disable the automatic scroll to the right and down . Below is my code JTextPane jtp1 = new JTextPane(); jtp1.setText("bhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhbhbbbbbbbbbbbbbbbbhbbbbbbbbbbbbbbbbbb b"); jtp1.setEditable(false); JScrollPane scrollpane2 = new JScrollPane(jtp1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollpane2.getVerticalScrollBar().setValue(0);

Upvotes: 0

Views: 282

Answers (1)

Braj
Braj

Reputation: 46881

A simple solution to turn off wrapping in a text pane. The solution is to add the text pane to a JPanel using a BorderLayout and then add the panel to the scroll pane:

Try below code

JTextPane jtp1 = new JTextPane();
jtp1.setText("bhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhbhbbbbbbbbbbbbbbbbhbbbbbbbbbbbbbbbbbb b");
jtp1.setEditable(false);

JPanel noWrapPanel = new JPanel( new BorderLayout() );
noWrapPanel.add( jtp1 );

JScrollPane scrollpane2 = new JScrollPane(noWrapPanel);

JFrame frame=new JFrame();
frame.add(scrollpane2);
frame.setSize(200,100);
frame.setVisible(true);

Also look at JTextPane line wrapping

Upvotes: 1

Related Questions