how to select all the text in a JTextArea?

any1 know how to select all the text in a textarea? just like when we press ctrl+a? i use JTextAreaName.selectAll();doesnt work helt..
[162 byte] By [w32sysfiea] at [2007-11-27 3:28:04]
# 1

> doesnt work

the way you are doing it doesn't work.

the textArea requires focus for the selection highlight to be visible.

if you are (e.g.) clicking a button for your selectAll(), the button gets the focus.

click the button, then tab into the textArea (note: tab, not click into).

after tabbing, if you now see the selection highlight, add this line

JTextAreaName.requestFocusInWindow();//<-

JTextAreaName.selectAll();

Michael_Dunna at 2007-7-12 8:30:52 > top of Java-index,Desktop,Core GUI APIs...
# 2
still doesnt work checked all spellings.. no error but still cant selectall() ;the component that call this function is from a menuitem
w32sysfiea at 2007-7-12 8:30:52 > top of Java-index,Desktop,Core GUI APIs...
# 3
post a sample program, so we can see how you've put it togetherjust a frame with a textArea and how you are calling selectAll()
Michael_Dunna at 2007-7-12 8:30:52 > top of Java-index,Desktop,Core GUI APIs...
# 4

public class Myprogram extends JFrame

{

public Classname()

{

JMenuJMenuName= new JMenu("JMenuName");

JMenuItem JMenuItemName=new JMenuItem("Select all");

JMenuName.add(JMenuItemName);

JMenuItemName.addActionListener(new SelectAllAction());

class SelectAllAction implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

TArea.requestFocusInWindow();

TArea.selectAll();

}

}

}

}

note that i didnt include the menubar declaration here ,

the main problem is thatt after i click the JMenuItemName which call the SelectAllAction class, nothing happeens

everything include the TArea has been added to the container and i can see it on screen, i have only 1 textarea

Message was edited by:

w32sysfie

w32sysfiea at 2007-7-12 8:30:52 > top of Java-index,Desktop,Core GUI APIs...
# 5

worked OK for me

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Myprogram extends JFrame

{

JTextArea TArea = new JTextArea(10,10);

public Myprogram()

{

JMenu JMenuName= new JMenu("JMenuName");

JMenuItem JMenuItemName=new JMenuItem("Select all");

JMenuName.add(JMenuItemName);

JMenuItemName.addActionListener(new SelectAllAction());

getContentPane().add(new JScrollPane(TArea));

JMenuBar mb = new JMenuBar();

mb.add(JMenuName);

setJMenuBar(mb);

pack();

setLocationRelativeTo(null);

setVisible(true);

}

class SelectAllAction implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

TArea.requestFocusInWindow();

TArea.selectAll();

}

}

public static void main(String[] args){new Myprogram();}

}

Michael_Dunna at 2007-7-12 8:30:52 > top of Java-index,Desktop,Core GUI APIs...