I use a Jpane with a GridBagLayout. In the GridBags I put a JTextPane component. But I can't linewrap my text in the JTextPane when I resize the JFrame. Here is my code...
package problems;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.*;
public class ScrollbarTest {
private JFrame frame;
private JPanel panel;
private JScrollPane [] scrollpane = new JScrollPane[4];
private JTextPane [] textPane = new JTextPane[4];
private JScrollPane scrollbar;
ScrollbarTest() {
//initialize jtextarea
for(int i=0;i<textPane.length;i++) {
textPane[i]=new JTextPane();
textPane[i].setText("xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx | ");
scrollpane[i] = new JScrollPane(textPane[i]);
}
panel = new JPanel();
frame = new JFrame();
createList(panel);
scrollbar = new JScrollPane(panel);
frame.add(scrollbar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(new Dimension(400,400));
} //constructor
//method to easily add components to GridBags
static void addComponent(JPanel panel,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbl.setConstraints( c, gbc );
panel.add( c );
}//addComponent
public void createList(JPanel panel) {
//setting layout: "GridBagLayout"
GridBagLayout gbl = new GridBagLayout();
panel.setLayout(gbl);
//put created components to the gridbags of gridbaglayout
// x y w h wx wy
addComponent( panel, gbl, scrollpane[0] , 0, 1, 1, 1, 1 ,0 );
addComponent( panel, gbl, scrollpane[1] , 0, 2, 1, 1, 1 ,0 );
addComponent( panel, gbl, scrollpane[2] , 1, 1, 1, 1, 1 ,0 );
addComponent( panel, gbl, scrollpane[3] , 1, 2, 1, 1, 1 ,0 );
} //createList
public static void main (String[] args) {
new ScrollbarTest();
} //main
} //end of class
Where is the problem?
Many thanks in advance
You are missing setLineWrap on your JTextArea.
for(int i=0;i<textArea.length;i++) {
textArea[i]=new JTextArea();
textArea[i].setLineWrap(true);
textArea[i].setText("xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx | ");
}
EDIT
in the case of JTextPane you shall create a scrollpane with no horizontal scroll policy and set your JtextPane as its viewPort