Sizing srollpane on textarea & textfield - java

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.

Related

JTextArea vertical scrollbar showing up when not needed

I need a vertical scrollbar in my JTextArea to only appear when it's needed, and for that I know I need to use Vertical Scrollbar As Needed. But the scrollbar keeps showing up, like this:
enter image description here
even when it's clearly not needed. What am I doing wrong?
// make top panel where output from the menu selections will appear
topP = new JPanel(new BorderLayout());
topP.setSize(new Dimension(500,150));
// make default text message to be displayed in top panel
output = new JTextArea("Output printed here...", 20, 20);
// styles the text in the textarea
output.setForeground(Color.BLUE);
output.setFont(new Font("Times New Roman", Font.BOLD, 20));
topP.add(output, BorderLayout.NORTH); // add default text to the top panel
right.add(output);
// here's the scrollbar guys
top = new JScrollPane(output); // applies to the textarea
top.setPreferredSize(new Dimension(500,150));
top.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
top.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
topP.add(top, BorderLayout.NORTH);
Take a look at the documentation for JTextArea. If you scroll down to the constructor summary section, you'll see the following:
JTextArea​(String text, int rows, int columns)
Constructs a new TextArea with the specified text and number of rows and columns.
This is the same as the constructor you're using:
output = new JTextArea("Output printed here...", 20, 20);
By specifying rows and columns, you're effectively telling the JTextArea that it has to be a certain size, and the scrollpane is just responding to your specifications by showing scroll controls. Those scroll controls are displayed based on the number of rows and columns you asked for, as opposed to the amount of visible text within the text area.
If you construct a JTextArea without specifying rows and columns, there won't be any scrollbars. Since you're setting the size of the text area, specifying rows and columns is redundant anyhow. Just don't do it.
Note: You should avoid setting the absolute size of Swing components. Set a preferred size only, and when you call pack(), Swing will try to accommodate your specification as much as possible. When you specify absolute sizes, your UI can look really bad on different platforms or when a user resizes the frame.

How to use scrollbar for a JTextField in JAVA using eclipse GUI drag and drop tool?

I am using the eclipse GUI drag and drop tool. I have a JTextField in my program which shows content from a line in a textfile. Now, I want to add a horizontal scrollbar underneath the JTextField so that the user can view the content no matter how long it is.
I have the scrollbar and JTextField ready in my JFrame. Can someone provide me with guidance on how to link them please?
Don't try to use a JTextField and a JScrollBar.
Instead, use a JTextArea which is designed to work with a JScrollPane:
JTextArea ta = new JTextArea(1, 10);
ta.getDocument().putProperty("filterNewlines", Boolean.TRUE);
JScrollPane sp = new JScrollPane( ta );
sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
You need to reserve space for the scrollbar otherwise it will paint over top of the text. The scrollbar will be activated when required.
This "filterNewLines" property just removes newline characters from the text so only a single line of text can be displayed in the text area.

JTextArea forces the parent JScrollPane to scroll down

I have a panel inside a JScrollPane and I dynamically populate the panel with components as the data gets received from the service. The panel uses GridBagLayout (but that should be irrelevant). For each record that comes back from the service, I create several components dynamically and append them to the bottom of the panel. Everything works fine, but the problem is that JTextArea that gets created for each record forces the main JScrollPane to scroll down and show the last added JTextArea, as shown here:
I tried to disable everything I could think of to dumb down the JTextArea, but still doesn't help
JTextArea descriptionArea = new JTextArea(project.getDescription().replace("<br>", "\n"));
descriptionArea.setEditable(false);
descriptionArea.setFont(thumbnailLabel.getFont());
descriptionArea.setLineWrap(true);
descriptionArea.setWrapStyleWord(true);
descriptionArea.setFocusable(false);
descriptionArea.setRequestFocusEnabled(false);
DefaultCaret caret = (DefaultCaret) descriptionArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
How can I prevent it from moving the scrollbar? I tried replacing JTextArea with JLabel and that works, but I can't get the JLabel to word wrap the text as good. Any ideas would be highly appreciated.
You could do something like:
Point p = srcollPane.getViewport().getViewPostition();
// add the components to the panel in the viewport of the scrollpane
scrollpane.getViewport().setViewPosition( p );
Now the scrollpane should be reset to its original position before you added the components.

Resizing JTextArea while using lineWrap

I want to create three JTextArea in my swing application.
Each JTextArea has a different size.
The first JTextArea should have 8 columns
The second one should only have 1 column
And the last one should have 50 columns.
My initial problem is that:
Whenever I type something, the JTextArea will keep on re-sizing its width.
This has been fixed by JScrollPane, setLineWrap(true), and setWrapStyleWord(true).
So here's my problem.
Whenever I add setLineWrap() to a JTextArea, the JTextArea will be resized.
My first and second JTextArea have been resized to 12 columns.
I searched and found some solution but they use MigLayout.
Is there any way to add word and line wrap in JTextArea without resizing it (and ofcourse, without the use of MigLayout)?
What's the easiest way to set the columns of JTextArea with word and line wrap?
What's the easiest way to set the columns of JTextArea with word and line wrap?
You create the JTextArea with code like:
JTextArea textArea = new JTextArea(5, 50);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel panel = new JPanel();
panel.add(scrollPane);
frame.add(panel, BorderLayout.PAGE_START);
By default a JPanel uses a FlowLayout which respects the size of any component added to it. The BorderLayout.PAGE_START will repect the height of any component added to it.
Scrollbars will appear as required what text is added to the text area. So the key is to use a layout manager (or combination of layout managers) that meet your requirement.

Textfield data is getting too small, when we enter more data into the textbox

If I apply,
text.setOptions(TextField.MULTILINE | TextField.REQUIRED);
to my text field, then I can enter more data, but the problem is the data is getting too small, then it becomes scrollable.
Can we get a scrollbar instead, before it gets too small?
I think you should use textarea instead of textfield as you want multiple lines. And then you need to put textArea to ScrollPane to get scrollbar.
JTextArea textArea = new JTextArea(10, 30);
ScrollPane scrollPane = new JScrollPane(textArea);

Categories