JTextField or JTextArea to string?

I am a bit rusty on my Java and have recently been kinda reteaching myself it. I created an applet to write some HTML code. my first problem is that i can not figure out how to use the input in a JTextField or JTextArea as part of a StringBuffer. I have tried a couple different things and can't figure it out. my next problem is i am using JOptionPane's for an undertermined number of variables. is there some way to add an undertermined number of pane.add(). also, i need to beable to paste things into the input areas, is there some way to do it? thanks

An important skill to have as a software developer is the ability to look up the API documentation.
http://java.sun.com/j2se/1.4.2/docs/api/index.html (for 1.4.2 - substitute 1.5.0 in the URL for 1.4.2 for that version if you wish)
For example, for JTextField, it would appear you'd need to simply invoke the getText() method to get the text contained therein.

Similar Messages

  • JTextField and JTextArea don't get focus occuasionally

    I am developing a Java Swing Applet. At edit mode, JComboBox, JCheckBox, JRadioBox, and JButton work fine, but JTextField and JTextArea can't get focus occuasionally while I click them.
    Anyone has idea?
    Thanks

    Thank you Himanshu,
    but that's not exactly what my issue is. This link describes the undesired effect of ending up outisde of the player window and in the browser.
    My problem is kind of the opposite. When I launch the page in the html file I cannot use the tab key to get to any of the controls in the captivate window. I'm stuck in the browser unless I click on the captivate window. That is not acceptable if we want it accessible without using a mouse. I found a way to make it work for chrome like I stated above, but not in FF. Do you have any ideas for this?
    Thanks!
    Monique

  • How to Convert JTextArea to String

    How would i convert JTextArea to string?

    elaborate, whats RTFAPI? I searched for it and got
    nothing.Since I'm not quite sure if you are being sarcastic, that translates to "read the f*cking application programmer interface". :)

  • Giving focus to JTextfield or JTextArea

    Hello,
    I want to know how to give the focus to a JTextField.To be more precise,
    I would like the cursor to be placed in the JTextField or JtextArea so the user does not have to click on the field and then enter a text.I must specify there a lot of components in my panel(JComboBox,JLabel,JList,JTextField).
    I tried jText.setFocus(),jText.setFocusable(true), and all the other focus' methods but it doesn't work.I also tried the caret method.
    Thanks in advance.

    This thread may explain your problem:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=290339

  • Tokenizing a JTextArea(how do i make the JTextArea a string?)

    I am working on this text analyzer program which is kinda like a replica of what you find when you a do a word count in many office suites. It tells you stuff like number of sentences, longest sentence, shortest sentence and average length. Number of words, longest shortest and average word length. In addtion there is a word search to it.
    It took me a good 3 hours to realize that there was such a thing as a panel and I could impliment this to add more than one object to a borderlayout section.
    I think I have figured out everything with the StringTokenizer part, well except for how do add it to an array. Someone mentioned something to me about an
    islist.StringTokenizer or something, but I haven't been able to find anything else on this.
    Right now, however, my primary concern is how can I take input from a JTextArea and be able to parse it with the StringTokenizer, which to the best of my knowledge has to be a string. It this as simple as a getValue(); type thing or do i have to add some sort of action listener that finds out when it changes or something?
    thank you for your time

    I just figued out the JTextArea thing
    it is JTextArea.getText();
    im stupid :P

  • How to get InputStream from JTextArea.getText() string?

    hello,
    well the postname pretty much says it, i need to get an InputStream for the String of a JTextArea.
    thing is, i want to save the string in a textfile, but the new line isnt recognized when i write it to the file.
    FileWriter fw = new FileWriter ("text.txt");
    fw.write(editField.getText());  // editField is my JTextArea instanceso i thought of doing smth like this
    String lineSeparator = System.getProperty("line.separator");
    // somehow getting the InputStream for my editField and save it in "is"
    BufferedReader br = new BufferedReader(new InputStreamReader (is));
    String line;
    while ((line = br.readLine()) != null) {
    fw.write(line);
    fw.write(lineSeparator);
    }

    pSaiko wrote:
    hello,
    well the postname pretty much says it, i need to get an InputStream for the String of a JTextArea.
    thing is, i want to save the string in a textfile, but the new line isnt recognized when i write it to the file.
    FileWriter fw = new FileWriter ("text.txt");
    fw.write(editField.getText());  // editField is my JTextArea instanceso i thought of doing smth like this
    String lineSeparator = System.getProperty("line.separator");
    // somehow getting the InputStream for my editField and save it in "is"
    BufferedReader br = new BufferedReader(new InputStreamReader (is));
    String line;
    while ((line = br.readLine()) != null) {
    fw.write(line);
    fw.write(lineSeparator);
    This is very simple, it isn't buffered, but it should work for you.
    Sting s = editfield.getText();
    for(int i=0; i<s.length();i++) fw.write((int)s.charAt(i));
    fw.flush();
    fw.close();

  • JTextArea methods + String Handling

    Hi There,
    My Question was, that i have made a small text editor application, and i was hoping that some one could tell me how to add the text, which is opened in a JTextArea to a Vector (or another data structure if this is more appropriate), which i can then parse using Regular expressions??
    Also, could u point me in the direction of a resource that has examples of how to use regular expressions in java 1.4 (util.regex)?
    Thanks in advance,
    much appreciated!

    I'd use getText() and put the text in a String, like the previous poster said. From there you can parse the string using the Pattern and Matcher objects.
    Here's a link that has some examples of regular expressions:
    http://developer.java.sun.com/developer/technicalArticles/releases/1.4regex/

  • JTextArea.setText("String ") not updating the GUI.

    Hi,
    When i tried to update the JTextArea with a new String it is not replacing the string in the TextArea.
    If I try to print the content by retrieving the data in the TextArea using JTextArea.getText() it is showing the latest string set to the JTextArea.
    What might be the problem?
    DP

    Here is the code. I am calling the displayMessage() from onMessage() method.
    private void displayMessage(String str){
         textArea.setText(str);
         textArea.revalidate();
         System.out.println("Message Received from QUE: "+ textArea.getText());
    public void onMessage(Message msg) {
    try {
    if (msg instanceof TextMessage) {
         msgText = ((TextMessage)msg).getText();
    } else {
         msgText = msg.toString();
         msgText = msgText.trim();
    System.out.println(msgText );
    displayMessage(msgText);

  • Question on JTextArea.append(String str)

    Hi All,
    When i provide JTextArea.append("FMTL") it is showing me on the JTextArea but when i give it as
    String item="Viswanadh";
    JTextArea.append(item).this particular statement is remaming blank on the TextArea.
    Any idea why this is happening and is there any other method to be used instead of append.???
    Thanks in advance..
    regards,
    Viswanadh

    class OnApplyFormat implements ActionListener{
         public void actionPerformed(ActionEvent formatevent){
              JComboBox source = (JComboBox) formatevent.getSource();
                   String item =  source.getSelectedItem().toString();
                     System.out.println("Selected Format is:"+item);
                     try
                       if ( item!=null )
                              System.out.println("Format is:"+item);
                     infoarea.append("FMTL");
                   infoarea.append(item);
                     infoarea.append("\r\n");
                   infoarea.append("FMTU");
                   infoarea.append("\r\n");
                   infoarea.append("wait 5");
                   infoarea.append("\r\n");
              else
                   throw new Exception("FormatNotFoundException");
                          }catch(Exception e){
                          e.printStackTrace();
    }That is my code and still i am facing the same FMTL is not getting appended with the item variable.
    Help me in solving this issue.
    Thanks in advance.
    regards,
    Viswanadh

  • JTextField and JTextArea in Mac with Java 6 does not work

    Hi there,
    I just installed the new version of Java 6 for Mac and I my text fields and text areas just stoped working. I can paste text to them, but they do not respond to my typing (only backspace works!!). Did anyone have this problem?
    I guess it is still beta and they are going to fix it in time. But I would be happy if I was not the only one and if there is any way to fix it for now.
    I just tried simple code like:
    main... {
      JFrame f = new JFrame();
      f.add(new JTextField(10), BorderLayout.NORTH));
      f.add(new JTextArea(5,2), BorderLayout.CENTER));
      f.pack();
      f.setVisible(true);
    }All the best,
    RC

    ...hm, nope, works for me...

  • JTextArea to String

    I'm writing a program that is essentially a basic text editor. I am trying to pass the text from the JTextArea to a seperate class which will be a "Find Text" dialog. What is the best way to go about doing this?
    Thanks in advance for any help!

    I think its better to pass the actual text component because you will ultimately want to hightlight the found text so you need access to the text component. Here's an [url http://www.discoverteenergy.com/files/FindReplace.java]example.

  • Problems with string array, please help!

    I have a String array floor[][], it has 20 rows and columns
    After I do some statement to modify it, I print this array
    out in JTextArea, why the output be like this?
    null* null....
    null null...
    null null...
    How to correct it?

    a turtle graphics applet:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TG extends JApplet implements ActionListener {
      private int x, y;
      private int pendown, command, movement;
      String direction, output, temp;
      JLabel l1;
      JTextField tf1;
      JTextArea ta1;
      String floor[][] = new String[20][20];;
      public void init()
        x = 0;
        y = 0;
        pendown = 0;
        direction = "r";
        Container c = getContentPane();
        c.setLayout( new FlowLayout() );
           l1 = new JLabel( "Please type a command:" );
           c.add( l1 );
           tf1 = new JTextField(20);
           tf1.addActionListener( this );
           c.add( tf1 );
           ta1 = new JTextArea(20,20);
           ta1.setEditable( false );
           c.add( ta1 );
    public void actionPerformed( ActionEvent e )
           temp = tf1.getText();
           if( temp.length() > 1)
           command = Integer.parseInt(temp.substring(0,1));
           movement = Integer.parseInt(temp.substring(2,temp.length()));
           else
           command = Integer.parseInt(temp);
           switch(command)
                case 1:
                pendown=0;
                break;
                case 2:
                pendown=1;
                break;
                case 3:
                direct("r");
                break;
                case 4:
                direct("l");
                break;
                case 5:
               move(movement);           
                break;
                case 6:
                print();
                break;                                     
      public void direct(String s)
           if(direction == "r" && s =="r")
              direction = "d";
           else if(direction == "r" && s =="l")
              direction = "u";
           else if(direction == "l" && s =="r")
              direction = "u";
           else if(direction == "l" && s =="l")
              direction = "d";
           else if(direction == "u" && s =="r")
              direction = "r";
           else if(direction == "u" && s =="l")
              direction = "l";
           else if(direction == "d" && s =="r")
              direction = "l";
           else if(direction == "d" && s =="l")
              direction = "r";
      public void move(int movement)
           if(pendown == 1)
                if(direction == "u")
                for(int b=0;b<movement;b++)
                     floor[x][y+b] = "*";
                else if(direction == "d")
                for(int b=0;b<movement;b++)
                     floor[x][y-b] = "*";
                else if(direction == "l")
                for(int b=0;b<movement;b++)
                     floor[x-b][y] = "*";
                else if(direction == "r")
                for(int b=0;b<movement;b++)
                     floor[x+b][y] = "*";
            else if(pendown == 0)
                 if(direction == "u")
                for(int b=0;b<movement;b++)
                     floor[x][y+b] = "-";
                else if(direction == "d")
                for(int b=0;b<movement;b++)
                     floor[x][y-b] = "-";
                else if(direction == "l")
                for(int b=0;b<movement;b++)
                     floor[x-b][y] = "-";
                else if(direction == "r")
                for(int b=0;b<movement;b++)
                     floor[x+b][y] = "-";
          public void print()
         for(int row=0;row<20;row++)
           for( int column=0;column<20;column++)
                output += floor[row][column];
                if(column == 19)
                 output+="\n";
            ta1.setText(output);
    }

  • Applet chatroom question

    I create a client program. I can run it in the local drive. However, when I post it to the web server, it cannot work properly. Why this happens?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.net.*;
    import java.io.*;
    public class Client extends JApplet
                        implements ActionListener, Runnable {
       JLabel enterLabel;
       JTextField enter;
       JTextArea textarea;
       String display = "";
       private Socket connection;
       private DataInputStream input;
       private DataOutputStream output;
       private Thread outputThread;
       public void init() {
          Container c = getContentPane();
          enterLabel = new JLabel( "" );
          c.add( enterLabel );
          enter = new JTextField( 5 );
          enter.addActionListener( this );
          c.add( enter, BorderLayout.SOUTH );
          textarea = new JTextArea( 29, 5 );
          textarea.setEditable( false );
          textarea.setFont( new Font( "Courier", Font.PLAIN, 12 ) );
          c.add( new JScrollPane( textarea ) , BorderLayout.NORTH );
       public void start() {
          try {
             connection = new Socket( InetAddress.getByName( "63.90.181.35" ), 5000 );
             input = new DataInputStream( connection.getInputStream() );
             output = new DataOutputStream( connection.getOutputStream() );
             output.flush();
          catch ( IOException e ) {
             e.printStackTrace();        
          outputThread = new Thread( this );
          outputThread.start();
       public void run() {
          while(true) {
            try {
              display = input.readUTF();
              textarea.append( display + "\n" );
            catch ( IOException e ) {
              e.printStackTrace();        
       public void actionPerformed( ActionEvent e ) {
           display = e.getActionCommand();
           enter.setText("");
         try {
           output.writeUTF( display );
           output.flush();
          catch ( IOException io ) {
            io.printStackTrace();        
    }

  • Strange problems..........

    I write a Turtle Graphics applet, when the applet output
    the result , it outputs a strange grahic in my
    JTextArea like:
    null
    null
    null
    Can someone help me out? Thanks.
    Here is my code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TG extends JApplet implements ActionListener {
      private int x, y;
      private int penup, command, movement;
      String direction, output, temp;
      JLabel l1;
      JTextField tf1;
      JTextArea ta1;
      String floor[][] = new String[20][20];
      public void init()
        x = 0;
        y = 0;
        penup = 1;
        direction = "right";
        Container c = getContentPane();
        c.setLayout( new FlowLayout() );
           l1 = new JLabel( "Please type a command:" );
           c.add( l1 );
           tf1 = new JTextField(20);
           tf1.addActionListener( this );
           c.add( tf1 );
           ta1 = new JTextArea(20,20);
           ta1.setEditable( false );
           c.add( ta1 );
    public void actionPerformed( ActionEvent e )
           temp = tf1.getText();
           if( temp.length() > 1)
           command = Integer.parseInt(temp.substring(0,1));
           movement = Integer.parseInt(temp.substring(2,temp.length()));
           else
           command = Integer.parseInt(temp);
           switch(command)
                case 1:
                penup=1;
                break;
                case 2:
                penup=0;
                break;
                case 3:
                direct("r");
                break;
                case 4:
                direct("l");
                break;
                case 5:
               move(movement);           
                break;
                case 6:
                print();
                break;                                     
      public void direct(String s)
           if(direction == "r" && s =="r")
              direction = "d";
           else if(direction == "r" && s =="l")
              direction = "u";
           else if(direction == "l" && s =="r")
              direction = "u";
           else if(direction == "l" && s =="l")
              direction = "d";
           else if(direction == "u" && s =="r")
              direction = "r";
           else if(direction == "u" && s =="l")
              direction = "l";
           else if(direction == "d" && s =="r")
              direction = "l";
           else if(direction == "d" && s =="l")
              direction = "r";
      public void move(int movement)
           if(penup == 1)
                if(direction == "u")
                for(int b=0;b<movement;b++)
                     floor[x][y+b] = "*";
                else if(direction == "d")
                for(int b=0;b<movement;b++)
                     floor[x][y-b] = "*";
                else if(direction == "l")
                for(int b=0;b<movement;b++)
                     floor[x-b][y] = "*";
                else if(direction == "r")
                for(int b=0;b<movement;b++)
                     floor[x+b][y] = "*";
            if(penup == 0)
                 if(direction == "u")
                for(int b=0;b<movement;b++)
                     floor[x][y+b] = " ";
                else if(direction == "d")
                for(int b=0;b<movement;b++)
                     floor[x][y-b] = " ";
                else if(direction == "l")
                for(int b=0;b<movement;b++)
                     floor[x-b][y] = " ";
                else if(direction == "r")
                for(int b=0;b<movement;b++)
                     floor[x+b][y] = " ";
          public void print()
         output += "result";
         for(int row=0;row<20;row++)
           for( int column=0;column<20;column++)
                output+="\n"+floor[row][column];
            ta1.setText(output);

    At first glance i would suggest changing your
    if(direction == "r" && s =="r")
              direction = "d";with appropiate
    direction.equals("r") && s.equals("s")You obviously do not want to compare for object identity.
    There might be other mistakes though...

  • Help me   a TowersOfHanoi problem

    // Lab 3: TowersOfHanoi.java
    // Program solves the Towers of Hanoi problem
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TowersOfHanoi extends JApplet implements ActionListener {
    JLabel label;
    JTextField input;
    JTextArea outputArea;
    String output;
    public void init()
    output = "";
    // create components
    label = new JLabel( "Enter number of disks ( 1-9 ): " );
    input = new JTextField( 5 );
    input.addActionListener( this );
    outputArea = new JTextArea( 15, 20 );
    /* Write code that creates a JScrollPane and attach outputArea to it */
    // outputArea.setText( output );
    JScrollPane scroll = new JScrollPane(outputArea);
    // add components to applet
    Container container = getContentPane();
    container.setLayout( new FlowLayout() );
    /* Write code to add the components to the content pane */
    input.addActionListener(this);
    container.add(label);
    container.add(input);
    container.add(scroll);
    // recusively move disks through towers
    /* write header for method tower */
    String tower(int n,String peg1,String peg2,String peg3)
    /* Write code here that tests for the base case (i.e., one disk).
    In this case, move the last disk from peg 1 to peg 3 and return. */
    if(n==1)
    return output+="\n"+peg1+"--->"+peg3;
    // move ( disks - 1 ) disks from peg1 to peg2 recursively
    /* Write a recursive call to method tower that moves
    ( disks - 1 ) disks from peg1 to peg2 */
    else
    tower(n-1,peg1,peg3,peg2);
    // move last disk from peg1 to peg3 recursively
    output += "\n" + peg1 + " --> " + peg3;
    // move ( disks - 1 ) disks from peg2 to peg3 recursively
    /* Write a recursive call to method tower that moves
    ( disks - 1 ) disks from peg2 to peg3 */
    tower(n-1,peg2,peg1,peg3);
    return output;
    // actually sort the number of discs specified by user
    public void actionPerformed( ActionEvent e )
    output = "";
    /* call method tower and pass it the number input by the user,
    a starting peg of 1, an ending peg of 3 and a temporary peg of 2 */
    int n=Integer.parseInt(input.getText());
    String peg1="1";
    String peg2="2";
    String peg3="3";
    output+=tower(n,peg1,peg2,peg3);
    outputArea.setText( output );
    } // end class TowersOfHanoi
    when n=3 , its answer : &#65297;&#65293;&#65293;&#65310;&#65299;&#65307;&#65297;&#65293;&#65293;&#65310;&#65298;&#65307;&#65299;&#65293;&#65293;&#65310;&#65298;&#65307;&#65297;&#65293;&#65293;&#65310;&#65299;&#65307;&#65298;&#65293;&#65293;&#65310;&#65297;&#65307;&#65298;&#65293;&#65310;&#65299;&#65307;&#65297;&#65293;&#65293;&#65310;&#65299;
    I really don't understand it , Who can explain the operation course of this procedure ? thank you so much!!!

    when n=3, the result is 1-->3 , 1-->2, 3-->2,1-->3,
    2-->1, 2-->3, 1-->3. I don't
    understand 1-->2, 3-->2.It means move the top disk of peg 1 to be on top of
    all the disks on peg 2, then move the top disk on peg
    3 to be on top of all disks on peg 3.Two, sir.
    Signed,
    Brother Maynard.

Maybe you are looking for

  • HT1338 Update Safari on iPad/ best way not to crash? - eMail Overload

    My Safari often crashes, even though I only have maybe 3 to 4 tabs open. (This is even after a factory reset to new restore as new with only 2 apps installed. So no memory is being used on anything.) And I'm wondering do I need to update it separatel

  • ISync will not start with error "Could not retrieve .Mac config"

    iSync fails when run from .Mac System Preferences with message "There was a problem with the sync operation Could not retrieve .Mac configuration." However, Backup and iDisk access are successfull. Dual G5 Etc.   Mac OS X (10.4.7)  

  • Firefox crashes after Carbon Copy Cloner

    I powered on the computer, and did not open anything other than Carbon Copy Cloner. I then copied one entire hard drive to another. Everything works, such as Thunderbird and Opera Next, except for Firefox! As it's my primary browser I have spent an h

  • Steaming content on 6630.

    I have a 6630 and cannot stream content, it just says "max number of connections in use". Gives the same message with a Vodafone SIM and with a Three SIM. Is there something I need to do on the phone?

  • Problem getting an LDAPContext after authenticating via Kerberos

    Hi, I am trying to create a Java program that can query an Active Directory server using the currenlty logged in Windows user's credentials to authenticate via LDAP. I am getting the following error in my output when trying to create the LdapContext