user2303984
user2303984

Reputation: 19

Sizing srollpane on textarea & textfield

I am writing a code, where i have one textfield & another one is textarea with seperate scrollpane for each.

Now what i need is i want to set the size of scrollpane such that it should automatically move my cursor to next line, whenever user reaches the end of first row of textarea or textfield. Please tell me if anyone can help me out.

Below the code to set scrollpane for textarea & textfield:

   JScrollPane talkPane = new JScrollPane(talkArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      talkPane.setViewportView(talkArea);

    JScrollPane inputPane = new JScrollPane(inputField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

Upvotes: 1

Views: 84

Answers (1)

camickr
camickr

Reputation: 324197

A text field only displays a single line of text so this makes no sense for a text field. You generally don't add a text field to a scrollpane. The user just uses the caret to scroll forwards/backwards to see more text.

For a JTextArea you can turn wrapping on. Then the text will automatically wrap to the next line along with the caret as your type.

textArea.setLineWrap(true);

Now what i need is i want to set the size of scrollpane

You don't set the size of a scrollpane. You give the scrollpane a hint by doing:

JTextArea textArea = new JTextArea(row, columns);

Then the viewport of the scrollpane will be sized based on the row/column you specify.

Upvotes: 1

Related Questions