Results 1 to 2 of 2
  1. #1
    brendan89 is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Displaying text in textarea with a button

    Hi, I am new to Java and I am trying to figure out how to do this: "Create two text areas - one that will hold user input of names and another that will display the names sorted after having pressed a button." The part I'm stuck at is getting the text to display in the text area on the right. What am I doing wrong?

    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ClassFrame extends JFrame
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JTextArea nameText;
    private JTextArea sortedText;
    private JButton sort;
    private String list = "";
    private String[] nameArray;
    public ClassFrame()
    setTitle("Class Frame");
    setLayout(new BorderLayout(2,2));
    sort = new JButton("Sort");

    nameText = new JTextArea(20,20);
    sortedText = new JTextArea(20,20);

    panel1 = new JPanel();
    panel2 = new JPanel();
    panel3 = new JPanel();


    add(panel1, BorderLayout.WEST);
    add(panel2, BorderLayout.EAST);
    add(panel3, BorderLayout.SOUTH);


    sort.addActionListener(new SortButtonListener());


    public void sortText(String names)
    String delim = "\n";
    nameArray = names.split(delim);

    private class SortButtonListener implements ActionListener
    public void actionPerformed(ActionEvent e)
    for(int index = 0; index<nameArray.length; index++)
    sortedText.append(nameArray[index] + "\n");


    public static void main(String[] args)
    ClassFrame show = new ClassFrame();

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default Re: Displaying text in textarea with a button

    Check out the JTextArea API for useful functions.

    Java Platform SE 6
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 0
    Last Post: 02-25-2010, 11:30 AM
  2. Appending text to TextArea
    By deepthought015 in forum AWT / Swing
    Replies: 3
    Last Post: 05-01-2009, 02:42 PM
  3. Replies: 2
    Last Post: 04-24-2009, 08:10 AM
  4. Replies: 1
    Last Post: 11-13-2008, 07:43 AM
  5. Replies: 5
    Last Post: 01-17-2008, 09:01 PM

Posting Permissions

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