Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    JTextField = single line
    JTextArea = multi line

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

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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).

  • #5
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    No; I gave it a ScrollPane.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #9
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •