![]() |
JtextArea v. JTextField
In short, when do I use which one? I am trying to make a program to prevent legitimate sites from being DNS-blocked by the corporate warfare that will surely surround SOPA (or facebook being blocked because someone posts a copyrighted picture, or sites being blocked for silly, non-piracy-stopping reasons), and for manually adding sites to the hosts file, I have used two sets of code:
Code:
import java.awt.*;Code:
import java.awt.*;The things that print to console will be removed in final debugging; they are so I can see how the program is thinking. Now, I redid it in a showMessageDialog() for the textarea. The logical indicator is String str[] = new String[count]; (I am testing with two entries). Lets say I enter "codingforums.com overclockers.com". The return is "codingforums.com overclockers.com". |
JTextField = single line
JTextArea = multi line Similar to <input type="text" /> and <textarea></textarea> in html. |
But......
Why, then when I do
Code:
JFrame.setDefaultLookAndFeelDecorated(true);Code:
frame.add(jtf); |
You're packing it, but the layout manager will shrink it to the minimum size required.
Set the .setColumns on the jtf to the number of columns you would like. As for JTextArea, you need to provide the rows as well, and a scroll bar for it. If you don't add the scroll bar, things may get interesting if you exceed the rows available (with the rest of the components). |
about the scroll bar: edit was originally
Edit();, not Manual();. I changed it because of bugs in the class edit that were more easily ignored than fixed, as it was an advanced feature that could be replaced by a more user-friendly feature that accomplished the same ends. In Edit(), it echoed the entire hosts file to a JTextArea(hosts, 50, 50), and there was a scroll pane for x and y.It would not scroll past the bottom of the hosts file. Nothing could be appended to the bottom. It did, however, scroll below the 50-line visible bottom of the text area. Thoughts? |
That doesn't sound quite right. JTextArea shouldn't have an implicit scrollbar on it; AWT uses a built in scrollbar, but JTextArea needs to be added to a scrollable pane.
As for editing, are you meaning that text could not be typed in, or that the text was not visible? If you're using a JTextComponent, the scrollbars won't be there, so it may either expand the field as required, or force the maximum height and become unviewable. |
No; I gave it a ScrollPane.
|
I'm not sure I understand the issue you are having then.
If you insert a new line and append more data to the JTextArea, then it should show up without needing to change anything. The scrollbar will recalculate its area based on the size of the entire Text area component, and keep the view pane to the size specified. The only way to forbid writing to it is by explicitly stating that the JTextArea is non-writable by setting the setEditable to false. |
Quote:
|
By default it will be editable. Hitting enter within the text area will automatically increase the size of the scrollable pane. The viewport size remains the same, but the scrollbar will move down.
What is it you are using to add the component to the frame's content? |
| All times are GMT +1. The time now is 12:54 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.