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

    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));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sort = new JButton("Sort");

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

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

    panel1.add(nameText);
    panel2.add(sortedText);
    panel3.add(sort);

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



    sortText(nameText.getText());

    sort.addActionListener(new SortButtonListener());

    pack();
    setVisible(true);
    }

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

    private class SortButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if(e.getActionCommand().equals("sort"))
    {
    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
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    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 - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

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