问题描述:

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);

网友答案:

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.

相关阅读:
Top