...

View Full Version : JtextArea v. JTextField



Scriptr
12-29-2011, 05:03 AM
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:



import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.*;
import java.net.*;


public class Manual extends JFrame{

private String s;

public Manual() throws IOException{

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Hosts Editor");
frame.setLayout(new FlowLayout());

JButton submit = new JButton("Submit Addresses");
JTextArea jtf = new JTextArea("");
s = jtf.getText();
System.out.println(s + "\n\n");
frame.add(jtf);
submit.addActionListener(new HC(s));
frame.add(submit);
frame.setVisible(true);
frame.pack();

}
private class HC implements ActionListener{
private String S;
public HC(String s){
S = s;
}

public void actionPerformed(ActionEvent arg0) {
Scanner getDir;
try {
getDir = new Scanner(new FileInputStream("dir.ads"));
String dir = getDir.next();
System.out.println(dir + "\n\n");
System.out.println(S + "\n\n\n");

int count = 0;
int start = 0;
int e;
while((e = S.indexOf(" ", start)) != -1){
count++;
start = e++;
}
count++;
System.out.println(count);

String str[] = new String[count];
str = S.split(" ");
int ct = 0;
FileWriter wt = new FileWriter(new File("hosts"));
wt.write("");
String st[] = new String[2];
while (ct < S.length()){
str[ct] = InetAddress.getByName(str[ct]).toString();
st = str[ct].split("/");
wt.append(st[0] + "\t" + st[1]);
ct++;
System.out.println(java.util.Arrays.toString(st));
}

Scanner oH = new Scanner(new FileInputStream(dir));
String prevH = "";
while (oH.hasNext()){
prevH += oH.nextLine();
}
Scanner nH = new Scanner(new FileInputStream("hosts"));
while (nH.hasNext()){
prevH += nH.nextLine();
}
FileWriter w = new FileWriter(new File(dir));
w.write("");
w.append(prevH);
} catch (IOException e1) {
e1.printStackTrace();
}





}


}


}


and


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.*;
import java.net.*;


public class Manual extends JFrame{

private String s;

public Manual() throws IOException{

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Hosts Editor");
frame.setLayout(new FlowLayout());

JButton submit = new JButton("Submit Addresses");
JTextField jtf = new JTextField("");
s = jtf.getText();
System.out.println(s + "\n\n");
frame.add(jtf);
submit.addActionListener(new HC(s));
frame.add(submit);
frame.setVisible(true);
frame.pack();

}
private class HC implements ActionListener{
private String S;
public HC(String s){
S = s;
}

public void actionPerformed(ActionEvent arg0) {
Scanner getDir;
try {
getDir = new Scanner(new FileInputStream("dir.ads"));
String dir = getDir.next();
System.out.println(dir + "\n\n");
System.out.println(S + "\n\n\n");

int count = 0;
int start = 0;
int e;
while((e = S.indexOf(" ", start)) != -1){
count++;
start = e++;
}
count++;
System.out.println(count);

String str[] = new String[count];
str = S.split(" ");
int ct = 0;
FileWriter wt = new FileWriter(new File("hosts"));
wt.write("");
String st[] = new String[2];
while (ct < S.length()){
str[ct] = InetAddress.getByName(str[ct]).toString();
st = str[ct].split("/");
wt.append(st[0] + "\t" + st[1]);
ct++;
System.out.println(java.util.Arrays.toString(st));
}

Scanner oH = new Scanner(new FileInputStream(dir));
String prevH = "";
while (oH.hasNext()){
prevH += oH.nextLine();
}
Scanner nH = new Scanner(new FileInputStream("hosts"));
while (nH.hasNext()){
prevH += nH.nextLine();
}
FileWriter w = new FileWriter(new File(dir));
w.write("");
w.append(prevH);
} catch (IOException e1) {
e1.printStackTrace();
}





}


}


}



The difference is that the latter lets me get as far as a working window (neither does anything but wipe the hosts file clean, but I will ask that somewhere else, as this thread is for learning the differences between JTextArea and Field)

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];
str = S.split(" ");
System.out.println(str[0] + "\n" + str[1]); (I am testing with two entries). Lets say I enter "codingforums.com overclockers.com". The return is "codingforums.com overclockers.com".

Fou-Lu
12-29-2011, 03:36 PM
JTextField = single line
JTextArea = multi line

Similar to <input type="text" /> and <textarea></textarea> in html.

Scriptr
12-29-2011, 05:59 PM
Why, then when I do


JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Hosts Editor");
frame.setLayout(new FlowLayout());
JTextField jtf = new JTextField("");
JButton submit = new JButton("Submit Addresses");

.........


frame.add(jtf);
submit.addActionListener(new HC(s));
frame.add(submit);
frame.setVisible(true);
frame.pack();

it returns a barely-visible vertical line in which to type (expected, as I have no width arguments; works perfectly if I put arguments), but if I replace JTextField with JTextArea in all instances, it returns just the button; no text field or area at all.

Fou-Lu
12-29-2011, 08:18 PM
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).

Scriptr
12-29-2011, 09:57 PM
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?

Fou-Lu
12-29-2011, 10:31 PM
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.

Scriptr
12-29-2011, 11:10 PM
No; I gave it a ScrollPane.

Fou-Lu
12-29-2011, 11:33 PM
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.

Scriptr
12-30-2011, 12:44 AM
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.

That's just it: it didn't recalculate it's size. Also, I want it editable.

Fou-Lu
12-30-2011, 02:33 AM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum