Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to clear the textArea?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to clear the textArea?

    Hi everyone,

    I started working in a GUI, and I did this. But, I don't know how to add functionality to the "Clear" button, so when it is clicked to clear all the content of the text are. Also the "Reset" button to clear all of the areas.

    Thanks in advance

    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
     
    public class FindIt implements ActionListener
    {
     
    	public FindIt()
        {
            JFrame FindIt = new JFrame("Word Search");//frame is created
            FindIt.setLayout(new BorderLayout());//layout is set to Border Layout
            FindIt.setSize(500, 300);//dimensions of frame are set
     
     
     
            JLabel label = new JLabel("Empty or Copy & Paste Text");//Label for the panel
            JButton clear = new JButton("Clear");//Clear button
     
     
            clear.setMnemonic('C');//set c as the mnemonic for clear
     
            JTextArea area = new JTextArea(30,40);//text area is set
     
            area.setLineWrap(true);
          area.setWrapStyleWord(true);
     
            JTextArea search = new JTextArea(2,10);//search area is set
            search.setVisible(true);
            search.setEditable(true);//make the text editable
            JButton search1 = new JButton("Search");//search button
            search1.setMnemonic('S');//set s as the mnemonic for search
     
            JLabel found2 = new JLabel("# Found");
            JTextArea found = new JTextArea(2,5);//found search area
            found.setVisible(true);
            JButton reset = new JButton("Reset");//reset button
            reset.setMnemonic('R');
            JButton exit = new JButton("Exit");//exit button
            exit.setMnemonic('x');
     
            JPanel jpNorth = new JPanel();// panel for the top
            JPanel jpCenter = new JPanel();//center panel for text box
            JPanel jpSouth = new JPanel();//panel for the bottom
     
            jpNorth.add(label);// place label in top of the panel
            jpNorth.add(clear);// button in top of the panel
     
            jpCenter.add(area);// text box in center panel
     
            jpSouth.add(search);//place text box in bottom panel
            jpSouth.add(search1);//place search button
            jpSouth.add(found2);//label for found
            jpSouth.add(found);//place found text box in bottom panel
            jpSouth.add(reset);//place reset button in bottom panel
            jpSouth.add(exit);//place exit button in bottom panel
     
            FindIt.add(jpNorth, BorderLayout.NORTH);//places top panel in north position
            FindIt.add(jpCenter, BorderLayout.CENTER);//places center panel in center position
            FindIt.add(jpSouth, BorderLayout.SOUTH);//places bottom panel in bottom postion
     
            JMenuBar mbar = new JMenuBar();//create menu bar
            FindIt.setJMenuBar(mbar);
            JMenu file = new JMenu("File");//creates a file button on menu bar
            JMenuItem resetAll = new JMenuItem("ResetAll");//create reset all option under file
            resetAll.setMnemonic('A');
            JMenuItem exit1 = new JMenuItem("Exit");//create exit option under file
            exit1.setMnemonic('x');
            file.add(resetAll);//add reset all to file option on menu bar
            file.add(exit1);//add exit to file option on menu bar
            mbar.add(file);//adds file option to menu bar
     
            exit.addActionListener(this);//adds action listener to the exit button
            exit1.addActionListener(this);//adds action listener to the exit menu item
            clear.addActionListener(this);
     
     
     
     
            FindIt.setVisible(true);//allows the frame to be visible
     
            Searcher searchIT = new Searcher(search1, area, search, found);//sets listner for search1 JButton
     
            FindIt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//so program will close when x is clicked    
     
        }
     
     
     
        public void actionPerformed(ActionEvent e)//sets an action to be performed for the exit options
        {
            if (e.getActionCommand().equals("Exit"))
            {
                System.exit(0);
            }
     
     
     
     
            else if (e.getActionCommand().equals("x"))
            {
                System.exit(0);
            }
     
     
     
        }
        public static void main(String[] args)
        {
            FindIt word = new FindIt();
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to clear the textArea?

    Read the API doc for the class and see if there are any methods for changing the contents of a text area.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to clear the textArea?

    I already read it, and I've tried many things, I couldn't find any solution yet . Anyway, thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to clear the textArea?

    What methods did you see that change the contents of the textarea? Using a class's methods are the only way to change an objects contents.

    You should also have to look at the methods for any classes that the text area class extends to see if there is a method there.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: How to clear the textArea?

    Have a look here. Reading that should help you.

  6. #6
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to clear the textArea?

    This is realy easy, the only thing you should do is setting the text to a string that contains nothing.
    So you should do is : textField.setText(""); in the actionlistener of the button.
    Then the field wil be set to nothing.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: How to clear the textArea?

    Quote Originally Posted by The_pizzaguy View Post
    This is realy easy, the only thing you should do is setting the text to a string that contains nothing.
    So you should do is : textField.setText(""); in the actionlistener of the button.
    Then the field wil be set to nothing.
    Except it's not a text field, it's a text area.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to clear the textArea?

    Did you read the API for the textarea and for the classes it extends?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Help! ComboBox, CheckBox, RadioButton and TextArea/TextField
    By Kinshen09 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 17th, 2012, 07:32 PM
  2. [SOLVED] TextArea Not Setting size?
    By Java Programmer in forum AWT / Java Swing
    Replies: 4
    Last Post: January 22nd, 2012, 11:52 AM
  3. Sorting data from a file into textarea
    By paperkut in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 26th, 2011, 07:38 AM
  4. Adding a frame/textarea to an application
    By zincc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2011, 04:43 PM
  5. TextArea and Array Problem
    By slippery_one in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 28th, 2010, 07:11 AM