Results 1 to 2 of 2
- 11-29-2011, 04:34 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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();
}
}
- 11-29-2011, 04:53 PM #2
Re: Displaying text in textarea with a button
Check out the JTextArea API for useful functions.
Java Platform SE 6How 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
-
how to display text with \n,\r in jsp from textarea?
By tejz in forum New To JavaReplies: 0Last Post: 02-25-2010, 11:30 AM -
Appending text to TextArea
By deepthought015 in forum AWT / SwingReplies: 3Last Post: 05-01-2009, 02:42 PM -
Displaying a selected image with a JFileChooser into a textarea
By Jonte79 in forum AWT / SwingReplies: 2Last Post: 04-24-2009, 08:10 AM -
Displaying a file slected with a JFileChooser into a textarea
By happysal in forum AWT / SwingReplies: 1Last Post: 11-13-2008, 07:43 AM -
Displaying text in a JTextField after pressing a button
By RLRExtra in forum New To JavaReplies: 5Last Post: 01-17-2008, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks