aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem adding JScrollPane to JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem adding JScrollPane to JTextArea" Watch "problem adding JScrollPane to JTextArea" New topic
Author

problem adding JScrollPane to JTextArea

abhinav sinha
Greenhorn

Joined: Nov 29, 2007
Posts: 22
Hi,

I have a GUI in which there is a TextArea in which I have to display some text.Sometimes the no. of lines of text exceeds the no. of rows of the TextArea.So I thought of adding a JScrollPane.But I am unable to do so.I am new to Java and Swing that is why I am having problems.I tried out many things.I went through the Swing tutorials at - http://java.sun.com/docs/books/tutorial/uiswing/components
I am providing relevant portion of my codes below -

{code}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setColumns(30);
jTextArea.setEditable(false);
jTextArea.setTabSize(10);
jTextArea.setRows(5);
}
return jTextArea;
}

-----------------------------------
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel3 = new JLabel();
jLabel3.setText("NUMS");
jLabel2 = new JLabel();
jLabel2.setText("PFILE");
jLabel1 = new JLabel();
jLabel1.setText("Arguments");
jLabel1.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
JLabel = new JLabel();
JLabel.setText("Usage");
JLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
JLabel.setPreferredSize(new Dimension(296, 16));
JLabel.setVerticalTextPosition(SwingConstants.CENTER);
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
flowLayout.setVgap(12);
flowLayout.setHgap(22);
jLabel = new JLabel();
jLabel.setText("Select Script");
jLabel.setVerticalAlignment(SwingConstants.TOP);
jLabel.setHorizontalTextPosition(SwingConstants.LEFT);
jLabel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
jLabel.setHorizontalAlignment(SwingConstants.LEFT);
jContentPane = new JPanel();
jContentPane.setLayout(flowLayout);
jContentPane.add(jLabel, null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(JLabel, null);
jContentPane.add(getJTextArea(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJTextField(), null);
buttonGroup = getButtonGroup();
jContentPane.add(getJRadioButton(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJRadioButton1(), null);
jContentPane.add(jLabel3, null);
jContentPane.add(getJTextField2(), null);
}
return jContentPane;
}
{code}

In the getJTextArea method,I tried to use the following lines -
JScrollPane scrollpane=new JScrollPane();
scrollpane.setViewportView(jTextArea);
return jTextArea;

But it is not working.Currently the number of rows in TextArea is 5. Whenever no. of lines of display is exceeding 5,the TextArea is elongating and pushing out the other components out of the window.Please tell me how to add the scrollpane.I read that the scroll bars are added automatically in the text area when necessary.So I do not need to invoke any methods on the JScrollPane object.So the only thing I have to do is to add the scrollpane at the correct position with the correct dimension,which I am not able to do right now.Please correct me if I am wrong.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

You put the text area in a scroll pane, then ignore the scroll pane for the remainder of the code.

You'll need to add the scroll pane with the text area inside it to the panel, not just the text area.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
abhinav sinha
Greenhorn

Joined: Nov 29, 2007
Posts: 22
Thanks for your suggestion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem adding JScrollPane to JTextArea