Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-29-2011, 04:03 AM   PM User | #1
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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.*;
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
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");
		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".

Last edited by Scriptr; 12-29-2011 at 05:31 AM..
Scriptr is offline   Reply With Quote
Old 12-29-2011, 02:36 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
JTextField = single line
JTextArea = multi line

Similar to <input type="text" /> and <textarea></textarea> in html.
Fou-Lu is offline   Reply With Quote
Old 12-29-2011, 04:59 PM   PM User | #3
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
But......

Why, then when I do
Code:
JFrame.setDefaultLookAndFeelDecorated(true);
	JFrame frame = new JFrame("Hosts Editor");
	frame.setLayout(new FlowLayout());
	JTextField jtf = new JTextField("");
	JButton submit = new JButton("Submit Addresses");
.........
Code:
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.

Last edited by Scriptr; 12-29-2011 at 05:28 PM..
Scriptr is offline   Reply With Quote
Old 12-29-2011, 07:18 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Old 12-29-2011, 08:57 PM   PM User | #5
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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?
Scriptr is offline   Reply With Quote
Old 12-29-2011, 09:31 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-29-2011, 10:10 PM   PM User | #7
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
No; I gave it a ScrollPane.
Scriptr is offline   Reply With Quote
Old 12-29-2011, 10:33 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-29-2011, 11:44 PM   PM User | #9
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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.
Scriptr is offline   Reply With Quote
Old 12-30-2011, 01:33 AM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:12 AM.


Advertisement
Log in to turn off these ads.