• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem adding JScrollPane to JTextArea

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
abhinav sinha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your suggestion.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic