I can't make the horizontal scrollbar appear. I've tried using JTextPane.setSize(), JTextPane.setPreferredSize() and no size method for JTextPane. I'm also using JScrollPane.setPreferredSize().
The vertical scroll bar appears, but the horizontal one doesn't.
Here is an example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
private int textPaneWidth = 500;
private int textPaneHeigth = 200;
private int scrollPaneWidth = 100;
private int scrollPaneHeigth = 100;
private JTextPane textPane;
private JButton button;
private JFrame frame;
private JScrollPane scrollPane;
public static void main(String[] args) {
Test gui = new Test();
gui.go();
}
private void go() {
frame = new JFrame("Test");
button = new JButton("button");
textPane = new JTextPane();
scrollPane = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ButtonListener());
textPane.setFont(new Font("Courier", Font.PLAIN, 12));
// Sizes:
// textPane.setSize(textPaneWidth, textPaneHeigth); // ???
// textPane.setPreferredSize(new Dimension(textPaneWidth, textPaneHeigth)); // ???
scrollPane.setPreferredSize(new Dimension(scrollPaneWidth, scrollPaneHeigth));
frame.add(button);
frame.add(scrollPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
textPane.setText("==================================================================== \n" +
"==================================================================== \n" +
"==================================================================== \n" +
"==================================================================== \n" +
"==================================================================== \n");
}
}
}
When the button is pressed, the string is added to the JTextPane. There is vertical scrolling, but no horizontal scrolling.
This is what I'm seeing after pressing the button:
What am I doing wrong?