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.