How to copy from a JTextArea?












0














How would one go about creating a JTextArea in which you can copy the text that is set?



Simple, Cntl-C or right-click copy is fine.



Code is for a tile game I am working on and it prints information to the TextArea. Its works great, except I cannot copy any of the text.



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


public class TileHelper{

static JFrame frame;
static JButton getTile, end, start;
static JTextField field;
static JTextArea area;
static JScrollPane scroller;
static Container pane;
static JCheckBox manual;
static JComboBox numList;
static Insets insets;
static String newTile;
static boolean startLoop, firstTime = true;
static int numTiles;
public Tile previous, current;
static final String intString = {"4","5","6","7","8","9","10"};


public void buildGUI() {

//build our test gui & components
frame = new JFrame("TileHelper");
frame.setSize(680, 240);

manual = new JCheckBox("Get Manually");
getTile = new JButton("Get Tile");
end = new JButton("End");
start = new JButton("Start");
numList = new JComboBox(intString);
numList.setSelectedIndex(0);
field = new JTextField(10);
field.setText("Enter Name");
area = new JTextArea(670, 175);
area.setEditable(true);
area.setText("Press Start below after selection.");

//set font
area.setFont(new Font("Serif", Font.PLAIN, 16));

scroller = new JScrollPane(area);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

pane = frame.getContentPane();
pane.setLayout(null);
pane.add(manual);
pane.add(getTile);
pane.add(end);
pane.add(field);
pane.add(start);
pane.add(scroller);
pane.add(numList);

insets = pane.getInsets();

scroller.setBounds(insets.left + 5, insets.top + 5, 670, 175);
start.setBounds(insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
getTile.setBounds(insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
end.setBounds(insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, end.getPreferredSize().width, end.getPreferredSize().height);
numList.setBounds(insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, numList.getPreferredSize().width + 6, numList.getPreferredSize().height);
field.setBounds(insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, field.getPreferredSize().width, field.getPreferredSize().height);
manual.setBounds(insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width, manual.getPreferredSize().height);

getTile.addActionListener(new getListener());
end.addActionListener(new endListener());
start.addActionListener((new startListener()));
frame.setResizable(false);
frame.setVisible(true);
}


public static class startListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (!manual.isSelected()) {
startLoop = true;
}

if (field.getText().equals("Enter Name")) {
area.setText("You must name your array");
} else area.setText("Tile " + field.getText() ");
}
}

public static class endListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
area.append(" };");
area.selectAll();
area.copy();
}
}

public static class getListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
if (firstTime) {
newTile = String.format("new Tile" );
numTiles++;
firstTime = false;
} else {
if (++numTiles % 3 == 0){
newTile = String.format(",nt new Tile" );
} else newTile = String.format(", new Tile" );
}
area.append(newTile);
}
}









share|improve this question
























  • Ctrl+C should work right out of the box. Right click copy is another story. Are you saying Ctrl+C is not working for you?
    – sethu
    Jun 14 '13 at 3:39










  • Its not and I have no idea why.
    – sherrellbc
    Jun 14 '13 at 3:56






  • 1




    Are you capturing key press events somewhere in your application? If so you may have overridden the default behavior.
    – Seth Hays
    Jun 14 '13 at 4:08










  • I will look into that. Thank you for the suggestion.
    – sherrellbc
    Jun 14 '13 at 4:16










  • Maybe this sounds stupid..but had to ask.. are you using a Mac? Then it would be Command+C.
    – sethu
    Jun 14 '13 at 4:45
















0














How would one go about creating a JTextArea in which you can copy the text that is set?



Simple, Cntl-C or right-click copy is fine.



Code is for a tile game I am working on and it prints information to the TextArea. Its works great, except I cannot copy any of the text.



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


public class TileHelper{

static JFrame frame;
static JButton getTile, end, start;
static JTextField field;
static JTextArea area;
static JScrollPane scroller;
static Container pane;
static JCheckBox manual;
static JComboBox numList;
static Insets insets;
static String newTile;
static boolean startLoop, firstTime = true;
static int numTiles;
public Tile previous, current;
static final String intString = {"4","5","6","7","8","9","10"};


public void buildGUI() {

//build our test gui & components
frame = new JFrame("TileHelper");
frame.setSize(680, 240);

manual = new JCheckBox("Get Manually");
getTile = new JButton("Get Tile");
end = new JButton("End");
start = new JButton("Start");
numList = new JComboBox(intString);
numList.setSelectedIndex(0);
field = new JTextField(10);
field.setText("Enter Name");
area = new JTextArea(670, 175);
area.setEditable(true);
area.setText("Press Start below after selection.");

//set font
area.setFont(new Font("Serif", Font.PLAIN, 16));

scroller = new JScrollPane(area);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

pane = frame.getContentPane();
pane.setLayout(null);
pane.add(manual);
pane.add(getTile);
pane.add(end);
pane.add(field);
pane.add(start);
pane.add(scroller);
pane.add(numList);

insets = pane.getInsets();

scroller.setBounds(insets.left + 5, insets.top + 5, 670, 175);
start.setBounds(insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
getTile.setBounds(insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
end.setBounds(insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, end.getPreferredSize().width, end.getPreferredSize().height);
numList.setBounds(insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, numList.getPreferredSize().width + 6, numList.getPreferredSize().height);
field.setBounds(insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, field.getPreferredSize().width, field.getPreferredSize().height);
manual.setBounds(insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width, manual.getPreferredSize().height);

getTile.addActionListener(new getListener());
end.addActionListener(new endListener());
start.addActionListener((new startListener()));
frame.setResizable(false);
frame.setVisible(true);
}


public static class startListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (!manual.isSelected()) {
startLoop = true;
}

if (field.getText().equals("Enter Name")) {
area.setText("You must name your array");
} else area.setText("Tile " + field.getText() ");
}
}

public static class endListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
area.append(" };");
area.selectAll();
area.copy();
}
}

public static class getListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
if (firstTime) {
newTile = String.format("new Tile" );
numTiles++;
firstTime = false;
} else {
if (++numTiles % 3 == 0){
newTile = String.format(",nt new Tile" );
} else newTile = String.format(", new Tile" );
}
area.append(newTile);
}
}









share|improve this question
























  • Ctrl+C should work right out of the box. Right click copy is another story. Are you saying Ctrl+C is not working for you?
    – sethu
    Jun 14 '13 at 3:39










  • Its not and I have no idea why.
    – sherrellbc
    Jun 14 '13 at 3:56






  • 1




    Are you capturing key press events somewhere in your application? If so you may have overridden the default behavior.
    – Seth Hays
    Jun 14 '13 at 4:08










  • I will look into that. Thank you for the suggestion.
    – sherrellbc
    Jun 14 '13 at 4:16










  • Maybe this sounds stupid..but had to ask.. are you using a Mac? Then it would be Command+C.
    – sethu
    Jun 14 '13 at 4:45














0












0








0







How would one go about creating a JTextArea in which you can copy the text that is set?



Simple, Cntl-C or right-click copy is fine.



Code is for a tile game I am working on and it prints information to the TextArea. Its works great, except I cannot copy any of the text.



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


public class TileHelper{

static JFrame frame;
static JButton getTile, end, start;
static JTextField field;
static JTextArea area;
static JScrollPane scroller;
static Container pane;
static JCheckBox manual;
static JComboBox numList;
static Insets insets;
static String newTile;
static boolean startLoop, firstTime = true;
static int numTiles;
public Tile previous, current;
static final String intString = {"4","5","6","7","8","9","10"};


public void buildGUI() {

//build our test gui & components
frame = new JFrame("TileHelper");
frame.setSize(680, 240);

manual = new JCheckBox("Get Manually");
getTile = new JButton("Get Tile");
end = new JButton("End");
start = new JButton("Start");
numList = new JComboBox(intString);
numList.setSelectedIndex(0);
field = new JTextField(10);
field.setText("Enter Name");
area = new JTextArea(670, 175);
area.setEditable(true);
area.setText("Press Start below after selection.");

//set font
area.setFont(new Font("Serif", Font.PLAIN, 16));

scroller = new JScrollPane(area);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

pane = frame.getContentPane();
pane.setLayout(null);
pane.add(manual);
pane.add(getTile);
pane.add(end);
pane.add(field);
pane.add(start);
pane.add(scroller);
pane.add(numList);

insets = pane.getInsets();

scroller.setBounds(insets.left + 5, insets.top + 5, 670, 175);
start.setBounds(insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
getTile.setBounds(insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
end.setBounds(insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, end.getPreferredSize().width, end.getPreferredSize().height);
numList.setBounds(insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, numList.getPreferredSize().width + 6, numList.getPreferredSize().height);
field.setBounds(insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, field.getPreferredSize().width, field.getPreferredSize().height);
manual.setBounds(insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width, manual.getPreferredSize().height);

getTile.addActionListener(new getListener());
end.addActionListener(new endListener());
start.addActionListener((new startListener()));
frame.setResizable(false);
frame.setVisible(true);
}


public static class startListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (!manual.isSelected()) {
startLoop = true;
}

if (field.getText().equals("Enter Name")) {
area.setText("You must name your array");
} else area.setText("Tile " + field.getText() ");
}
}

public static class endListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
area.append(" };");
area.selectAll();
area.copy();
}
}

public static class getListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
if (firstTime) {
newTile = String.format("new Tile" );
numTiles++;
firstTime = false;
} else {
if (++numTiles % 3 == 0){
newTile = String.format(",nt new Tile" );
} else newTile = String.format(", new Tile" );
}
area.append(newTile);
}
}









share|improve this question















How would one go about creating a JTextArea in which you can copy the text that is set?



Simple, Cntl-C or right-click copy is fine.



Code is for a tile game I am working on and it prints information to the TextArea. Its works great, except I cannot copy any of the text.



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


public class TileHelper{

static JFrame frame;
static JButton getTile, end, start;
static JTextField field;
static JTextArea area;
static JScrollPane scroller;
static Container pane;
static JCheckBox manual;
static JComboBox numList;
static Insets insets;
static String newTile;
static boolean startLoop, firstTime = true;
static int numTiles;
public Tile previous, current;
static final String intString = {"4","5","6","7","8","9","10"};


public void buildGUI() {

//build our test gui & components
frame = new JFrame("TileHelper");
frame.setSize(680, 240);

manual = new JCheckBox("Get Manually");
getTile = new JButton("Get Tile");
end = new JButton("End");
start = new JButton("Start");
numList = new JComboBox(intString);
numList.setSelectedIndex(0);
field = new JTextField(10);
field.setText("Enter Name");
area = new JTextArea(670, 175);
area.setEditable(true);
area.setText("Press Start below after selection.");

//set font
area.setFont(new Font("Serif", Font.PLAIN, 16));

scroller = new JScrollPane(area);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

pane = frame.getContentPane();
pane.setLayout(null);
pane.add(manual);
pane.add(getTile);
pane.add(end);
pane.add(field);
pane.add(start);
pane.add(scroller);
pane.add(numList);

insets = pane.getInsets();

scroller.setBounds(insets.left + 5, insets.top + 5, 670, 175);
start.setBounds(insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
getTile.setBounds(insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width, getTile.getPreferredSize().height);
end.setBounds(insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5, end.getPreferredSize().width, end.getPreferredSize().height);
numList.setBounds(insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, numList.getPreferredSize().width + 6, numList.getPreferredSize().height);
field.setBounds(insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6, field.getPreferredSize().width, field.getPreferredSize().height);
manual.setBounds(insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width, manual.getPreferredSize().height);

getTile.addActionListener(new getListener());
end.addActionListener(new endListener());
start.addActionListener((new startListener()));
frame.setResizable(false);
frame.setVisible(true);
}


public static class startListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (!manual.isSelected()) {
startLoop = true;
}

if (field.getText().equals("Enter Name")) {
area.setText("You must name your array");
} else area.setText("Tile " + field.getText() ");
}
}

public static class endListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
area.append(" };");
area.selectAll();
area.copy();
}
}

public static class getListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
if (firstTime) {
newTile = String.format("new Tile" );
numTiles++;
firstTime = false;
} else {
if (++numTiles % 3 == 0){
newTile = String.format(",nt new Tile" );
} else newTile = String.format(", new Tile" );
}
area.append(newTile);
}
}






java swing copy set jtextarea






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 27 '13 at 9:40









mKorbel

104k14106261




104k14106261










asked Jun 14 '13 at 3:36









sherrellbc

1,80162552




1,80162552












  • Ctrl+C should work right out of the box. Right click copy is another story. Are you saying Ctrl+C is not working for you?
    – sethu
    Jun 14 '13 at 3:39










  • Its not and I have no idea why.
    – sherrellbc
    Jun 14 '13 at 3:56






  • 1




    Are you capturing key press events somewhere in your application? If so you may have overridden the default behavior.
    – Seth Hays
    Jun 14 '13 at 4:08










  • I will look into that. Thank you for the suggestion.
    – sherrellbc
    Jun 14 '13 at 4:16










  • Maybe this sounds stupid..but had to ask.. are you using a Mac? Then it would be Command+C.
    – sethu
    Jun 14 '13 at 4:45


















  • Ctrl+C should work right out of the box. Right click copy is another story. Are you saying Ctrl+C is not working for you?
    – sethu
    Jun 14 '13 at 3:39










  • Its not and I have no idea why.
    – sherrellbc
    Jun 14 '13 at 3:56






  • 1




    Are you capturing key press events somewhere in your application? If so you may have overridden the default behavior.
    – Seth Hays
    Jun 14 '13 at 4:08










  • I will look into that. Thank you for the suggestion.
    – sherrellbc
    Jun 14 '13 at 4:16










  • Maybe this sounds stupid..but had to ask.. are you using a Mac? Then it would be Command+C.
    – sethu
    Jun 14 '13 at 4:45
















Ctrl+C should work right out of the box. Right click copy is another story. Are you saying Ctrl+C is not working for you?
– sethu
Jun 14 '13 at 3:39




Ctrl+C should work right out of the box. Right click copy is another story. Are you saying Ctrl+C is not working for you?
– sethu
Jun 14 '13 at 3:39












Its not and I have no idea why.
– sherrellbc
Jun 14 '13 at 3:56




Its not and I have no idea why.
– sherrellbc
Jun 14 '13 at 3:56




1




1




Are you capturing key press events somewhere in your application? If so you may have overridden the default behavior.
– Seth Hays
Jun 14 '13 at 4:08




Are you capturing key press events somewhere in your application? If so you may have overridden the default behavior.
– Seth Hays
Jun 14 '13 at 4:08












I will look into that. Thank you for the suggestion.
– sherrellbc
Jun 14 '13 at 4:16




I will look into that. Thank you for the suggestion.
– sherrellbc
Jun 14 '13 at 4:16












Maybe this sounds stupid..but had to ask.. are you using a Mac? Then it would be Command+C.
– sethu
Jun 14 '13 at 4:45




Maybe this sounds stupid..but had to ask.. are you using a Mac? Then it would be Command+C.
– sethu
Jun 14 '13 at 4:45












4 Answers
4






active

oldest

votes


















1














JTextArea has a default behavior where you could do ctrl + c to copy the text to the clip board.



Below is the code that I just copied from your above question and I ran it. Ctrl + C still works fine.



import java.awt.Container;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class TileHelper
{

static JFrame frame;
static JButton getTile, end, start;
static JTextField field;
static JTextArea area;
static JScrollPane scroller;
static Container pane;
static JCheckBox manual;
static JComboBox numList;
static Insets insets;
static String newTile;
static boolean startLoop, firstTime = true;
static int numTiles;
static final String intString = { "4", "5", "6", "7", "8", "9", "10" };

public void buildGUI()
{

// build our test gui & components
frame = new JFrame( "TileHelper" );
frame.setSize( 680, 240 );

manual = new JCheckBox( "Get Manually" );
getTile = new JButton( "Get Tile" );
end = new JButton( "End" );
start = new JButton( "Start" );
numList = new JComboBox( intString );
numList.setSelectedIndex( 0 );
field = new JTextField( 10 );
field.setText( "Enter Name" );
area = new JTextArea( 670, 175 );
area.setEditable( true );
area.setText( "Press Start below after selection." );

// set font
area.setFont( new Font( "Serif", Font.PLAIN, 16 ) );

scroller = new JScrollPane( area );
scroller.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
scroller.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

pane = frame.getContentPane();
pane.setLayout( null );
pane.add( manual );
pane.add( getTile );
pane.add( end );
pane.add( field );
pane.add( start );
pane.add( scroller );
pane.add( numList );

insets = pane.getInsets();

scroller.setBounds( insets.left + 5, insets.top + 5, 670, 175 );
start.setBounds( insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width,
getTile.getPreferredSize().height );
getTile.setBounds( insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
getTile.getPreferredSize().width, getTile.getPreferredSize().height );
end.setBounds( insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
end.getPreferredSize().width, end.getPreferredSize().height );
numList.setBounds( insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
numList.getPreferredSize().width + 6, numList.getPreferredSize().height );
field.setBounds( insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
field.getPreferredSize().width, field.getPreferredSize().height );
manual.setBounds( insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width,
manual.getPreferredSize().height );

getTile.addActionListener( new getListener() );
end.addActionListener( new endListener() );
start.addActionListener( ( new startListener() ) );
frame.setResizable( false );
frame.setVisible( true );
}

public static class startListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (!manual.isSelected())
{
startLoop = true;
}

if (field.getText().equals("Enter Name"))
{
area.setText("You must name your array");
}
else
{
area.setText( "Tile" + field.getText() );
}
}
}

public static class endListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
area.append( " };" );
area.selectAll();
area.copy();
}
}

public static class getListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
if( firstTime )
{
newTile = String.format( "new Tile" );
numTiles++;
firstTime = false;
}
else
{
if( ++numTiles % 3 == 0 )
{
newTile = String.format( ",nt new Tile" );
}
else
newTile = String.format( ", new Tile" );
}
area.append( newTile );
}
}

public static void main( String args )
{
TileHelper helper = new TileHelper();
helper.buildGUI();
}
}





share|improve this answer























  • Thats what I have read, yet it does not work when I run this.
    – sherrellbc
    Jun 14 '13 at 3:58










  • As said by @sethu, if you are on Mac, you'll have to go for a different command lists.apple.com/archives/java-dev/2008/Aug/msg00053.html
    – Raghav
    Jun 14 '13 at 5:32



















1














Or else you could use something like the one in the below code.



Keymap km = area.getKeymap();
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK);
km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());





share|improve this answer





























    0














    Try:



    String str;



    str =TTextarea.getText();



    See this interesting link
    http://java-program-sample.blogspot.com/2011/08/copy-selected-text-from-jtextarea-to.html!






    share|improve this answer





























      0














      This worked for me (same as Raghav's answer, but it maps copy to Command-C instead of Control-C, which is what most Mac users expect).



      String osName = System.getProperties().getProperty("os.name");
      if (osName.startsWith("Mac OS X")) {
      Keymap km = area.getKeymap();
      KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C,
      InputEvent.META_DOWN_MASK);
      km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());
      }





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17100691%2fhow-to-copy-from-a-jtextarea%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        JTextArea has a default behavior where you could do ctrl + c to copy the text to the clip board.



        Below is the code that I just copied from your above question and I ran it. Ctrl + C still works fine.



        import java.awt.Container;
        import java.awt.Font;
        import java.awt.Insets;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        import javax.swing.JButton;
        import javax.swing.JCheckBox;
        import javax.swing.JComboBox;
        import javax.swing.JFrame;
        import javax.swing.JScrollPane;
        import javax.swing.JTextArea;
        import javax.swing.JTextField;
        import javax.swing.ScrollPaneConstants;

        public class TileHelper
        {

        static JFrame frame;
        static JButton getTile, end, start;
        static JTextField field;
        static JTextArea area;
        static JScrollPane scroller;
        static Container pane;
        static JCheckBox manual;
        static JComboBox numList;
        static Insets insets;
        static String newTile;
        static boolean startLoop, firstTime = true;
        static int numTiles;
        static final String intString = { "4", "5", "6", "7", "8", "9", "10" };

        public void buildGUI()
        {

        // build our test gui & components
        frame = new JFrame( "TileHelper" );
        frame.setSize( 680, 240 );

        manual = new JCheckBox( "Get Manually" );
        getTile = new JButton( "Get Tile" );
        end = new JButton( "End" );
        start = new JButton( "Start" );
        numList = new JComboBox( intString );
        numList.setSelectedIndex( 0 );
        field = new JTextField( 10 );
        field.setText( "Enter Name" );
        area = new JTextArea( 670, 175 );
        area.setEditable( true );
        area.setText( "Press Start below after selection." );

        // set font
        area.setFont( new Font( "Serif", Font.PLAIN, 16 ) );

        scroller = new JScrollPane( area );
        scroller.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
        scroller.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

        pane = frame.getContentPane();
        pane.setLayout( null );
        pane.add( manual );
        pane.add( getTile );
        pane.add( end );
        pane.add( field );
        pane.add( start );
        pane.add( scroller );
        pane.add( numList );

        insets = pane.getInsets();

        scroller.setBounds( insets.left + 5, insets.top + 5, 670, 175 );
        start.setBounds( insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width,
        getTile.getPreferredSize().height );
        getTile.setBounds( insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
        getTile.getPreferredSize().width, getTile.getPreferredSize().height );
        end.setBounds( insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
        end.getPreferredSize().width, end.getPreferredSize().height );
        numList.setBounds( insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
        numList.getPreferredSize().width + 6, numList.getPreferredSize().height );
        field.setBounds( insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
        field.getPreferredSize().width, field.getPreferredSize().height );
        manual.setBounds( insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width,
        manual.getPreferredSize().height );

        getTile.addActionListener( new getListener() );
        end.addActionListener( new endListener() );
        start.addActionListener( ( new startListener() ) );
        frame.setResizable( false );
        frame.setVisible( true );
        }

        public static class startListener implements ActionListener
        {
        public void actionPerformed(ActionEvent e)
        {
        if (!manual.isSelected())
        {
        startLoop = true;
        }

        if (field.getText().equals("Enter Name"))
        {
        area.setText("You must name your array");
        }
        else
        {
        area.setText( "Tile" + field.getText() );
        }
        }
        }

        public static class endListener implements ActionListener
        {
        public void actionPerformed( ActionEvent event )
        {
        area.append( " };" );
        area.selectAll();
        area.copy();
        }
        }

        public static class getListener implements ActionListener
        {
        public void actionPerformed( ActionEvent event )
        {
        if( firstTime )
        {
        newTile = String.format( "new Tile" );
        numTiles++;
        firstTime = false;
        }
        else
        {
        if( ++numTiles % 3 == 0 )
        {
        newTile = String.format( ",nt new Tile" );
        }
        else
        newTile = String.format( ", new Tile" );
        }
        area.append( newTile );
        }
        }

        public static void main( String args )
        {
        TileHelper helper = new TileHelper();
        helper.buildGUI();
        }
        }





        share|improve this answer























        • Thats what I have read, yet it does not work when I run this.
          – sherrellbc
          Jun 14 '13 at 3:58










        • As said by @sethu, if you are on Mac, you'll have to go for a different command lists.apple.com/archives/java-dev/2008/Aug/msg00053.html
          – Raghav
          Jun 14 '13 at 5:32
















        1














        JTextArea has a default behavior where you could do ctrl + c to copy the text to the clip board.



        Below is the code that I just copied from your above question and I ran it. Ctrl + C still works fine.



        import java.awt.Container;
        import java.awt.Font;
        import java.awt.Insets;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        import javax.swing.JButton;
        import javax.swing.JCheckBox;
        import javax.swing.JComboBox;
        import javax.swing.JFrame;
        import javax.swing.JScrollPane;
        import javax.swing.JTextArea;
        import javax.swing.JTextField;
        import javax.swing.ScrollPaneConstants;

        public class TileHelper
        {

        static JFrame frame;
        static JButton getTile, end, start;
        static JTextField field;
        static JTextArea area;
        static JScrollPane scroller;
        static Container pane;
        static JCheckBox manual;
        static JComboBox numList;
        static Insets insets;
        static String newTile;
        static boolean startLoop, firstTime = true;
        static int numTiles;
        static final String intString = { "4", "5", "6", "7", "8", "9", "10" };

        public void buildGUI()
        {

        // build our test gui & components
        frame = new JFrame( "TileHelper" );
        frame.setSize( 680, 240 );

        manual = new JCheckBox( "Get Manually" );
        getTile = new JButton( "Get Tile" );
        end = new JButton( "End" );
        start = new JButton( "Start" );
        numList = new JComboBox( intString );
        numList.setSelectedIndex( 0 );
        field = new JTextField( 10 );
        field.setText( "Enter Name" );
        area = new JTextArea( 670, 175 );
        area.setEditable( true );
        area.setText( "Press Start below after selection." );

        // set font
        area.setFont( new Font( "Serif", Font.PLAIN, 16 ) );

        scroller = new JScrollPane( area );
        scroller.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
        scroller.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

        pane = frame.getContentPane();
        pane.setLayout( null );
        pane.add( manual );
        pane.add( getTile );
        pane.add( end );
        pane.add( field );
        pane.add( start );
        pane.add( scroller );
        pane.add( numList );

        insets = pane.getInsets();

        scroller.setBounds( insets.left + 5, insets.top + 5, 670, 175 );
        start.setBounds( insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width,
        getTile.getPreferredSize().height );
        getTile.setBounds( insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
        getTile.getPreferredSize().width, getTile.getPreferredSize().height );
        end.setBounds( insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
        end.getPreferredSize().width, end.getPreferredSize().height );
        numList.setBounds( insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
        numList.getPreferredSize().width + 6, numList.getPreferredSize().height );
        field.setBounds( insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
        field.getPreferredSize().width, field.getPreferredSize().height );
        manual.setBounds( insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width,
        manual.getPreferredSize().height );

        getTile.addActionListener( new getListener() );
        end.addActionListener( new endListener() );
        start.addActionListener( ( new startListener() ) );
        frame.setResizable( false );
        frame.setVisible( true );
        }

        public static class startListener implements ActionListener
        {
        public void actionPerformed(ActionEvent e)
        {
        if (!manual.isSelected())
        {
        startLoop = true;
        }

        if (field.getText().equals("Enter Name"))
        {
        area.setText("You must name your array");
        }
        else
        {
        area.setText( "Tile" + field.getText() );
        }
        }
        }

        public static class endListener implements ActionListener
        {
        public void actionPerformed( ActionEvent event )
        {
        area.append( " };" );
        area.selectAll();
        area.copy();
        }
        }

        public static class getListener implements ActionListener
        {
        public void actionPerformed( ActionEvent event )
        {
        if( firstTime )
        {
        newTile = String.format( "new Tile" );
        numTiles++;
        firstTime = false;
        }
        else
        {
        if( ++numTiles % 3 == 0 )
        {
        newTile = String.format( ",nt new Tile" );
        }
        else
        newTile = String.format( ", new Tile" );
        }
        area.append( newTile );
        }
        }

        public static void main( String args )
        {
        TileHelper helper = new TileHelper();
        helper.buildGUI();
        }
        }





        share|improve this answer























        • Thats what I have read, yet it does not work when I run this.
          – sherrellbc
          Jun 14 '13 at 3:58










        • As said by @sethu, if you are on Mac, you'll have to go for a different command lists.apple.com/archives/java-dev/2008/Aug/msg00053.html
          – Raghav
          Jun 14 '13 at 5:32














        1












        1








        1






        JTextArea has a default behavior where you could do ctrl + c to copy the text to the clip board.



        Below is the code that I just copied from your above question and I ran it. Ctrl + C still works fine.



        import java.awt.Container;
        import java.awt.Font;
        import java.awt.Insets;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        import javax.swing.JButton;
        import javax.swing.JCheckBox;
        import javax.swing.JComboBox;
        import javax.swing.JFrame;
        import javax.swing.JScrollPane;
        import javax.swing.JTextArea;
        import javax.swing.JTextField;
        import javax.swing.ScrollPaneConstants;

        public class TileHelper
        {

        static JFrame frame;
        static JButton getTile, end, start;
        static JTextField field;
        static JTextArea area;
        static JScrollPane scroller;
        static Container pane;
        static JCheckBox manual;
        static JComboBox numList;
        static Insets insets;
        static String newTile;
        static boolean startLoop, firstTime = true;
        static int numTiles;
        static final String intString = { "4", "5", "6", "7", "8", "9", "10" };

        public void buildGUI()
        {

        // build our test gui & components
        frame = new JFrame( "TileHelper" );
        frame.setSize( 680, 240 );

        manual = new JCheckBox( "Get Manually" );
        getTile = new JButton( "Get Tile" );
        end = new JButton( "End" );
        start = new JButton( "Start" );
        numList = new JComboBox( intString );
        numList.setSelectedIndex( 0 );
        field = new JTextField( 10 );
        field.setText( "Enter Name" );
        area = new JTextArea( 670, 175 );
        area.setEditable( true );
        area.setText( "Press Start below after selection." );

        // set font
        area.setFont( new Font( "Serif", Font.PLAIN, 16 ) );

        scroller = new JScrollPane( area );
        scroller.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
        scroller.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

        pane = frame.getContentPane();
        pane.setLayout( null );
        pane.add( manual );
        pane.add( getTile );
        pane.add( end );
        pane.add( field );
        pane.add( start );
        pane.add( scroller );
        pane.add( numList );

        insets = pane.getInsets();

        scroller.setBounds( insets.left + 5, insets.top + 5, 670, 175 );
        start.setBounds( insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width,
        getTile.getPreferredSize().height );
        getTile.setBounds( insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
        getTile.getPreferredSize().width, getTile.getPreferredSize().height );
        end.setBounds( insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
        end.getPreferredSize().width, end.getPreferredSize().height );
        numList.setBounds( insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
        numList.getPreferredSize().width + 6, numList.getPreferredSize().height );
        field.setBounds( insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
        field.getPreferredSize().width, field.getPreferredSize().height );
        manual.setBounds( insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width,
        manual.getPreferredSize().height );

        getTile.addActionListener( new getListener() );
        end.addActionListener( new endListener() );
        start.addActionListener( ( new startListener() ) );
        frame.setResizable( false );
        frame.setVisible( true );
        }

        public static class startListener implements ActionListener
        {
        public void actionPerformed(ActionEvent e)
        {
        if (!manual.isSelected())
        {
        startLoop = true;
        }

        if (field.getText().equals("Enter Name"))
        {
        area.setText("You must name your array");
        }
        else
        {
        area.setText( "Tile" + field.getText() );
        }
        }
        }

        public static class endListener implements ActionListener
        {
        public void actionPerformed( ActionEvent event )
        {
        area.append( " };" );
        area.selectAll();
        area.copy();
        }
        }

        public static class getListener implements ActionListener
        {
        public void actionPerformed( ActionEvent event )
        {
        if( firstTime )
        {
        newTile = String.format( "new Tile" );
        numTiles++;
        firstTime = false;
        }
        else
        {
        if( ++numTiles % 3 == 0 )
        {
        newTile = String.format( ",nt new Tile" );
        }
        else
        newTile = String.format( ", new Tile" );
        }
        area.append( newTile );
        }
        }

        public static void main( String args )
        {
        TileHelper helper = new TileHelper();
        helper.buildGUI();
        }
        }





        share|improve this answer














        JTextArea has a default behavior where you could do ctrl + c to copy the text to the clip board.



        Below is the code that I just copied from your above question and I ran it. Ctrl + C still works fine.



        import java.awt.Container;
        import java.awt.Font;
        import java.awt.Insets;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        import javax.swing.JButton;
        import javax.swing.JCheckBox;
        import javax.swing.JComboBox;
        import javax.swing.JFrame;
        import javax.swing.JScrollPane;
        import javax.swing.JTextArea;
        import javax.swing.JTextField;
        import javax.swing.ScrollPaneConstants;

        public class TileHelper
        {

        static JFrame frame;
        static JButton getTile, end, start;
        static JTextField field;
        static JTextArea area;
        static JScrollPane scroller;
        static Container pane;
        static JCheckBox manual;
        static JComboBox numList;
        static Insets insets;
        static String newTile;
        static boolean startLoop, firstTime = true;
        static int numTiles;
        static final String intString = { "4", "5", "6", "7", "8", "9", "10" };

        public void buildGUI()
        {

        // build our test gui & components
        frame = new JFrame( "TileHelper" );
        frame.setSize( 680, 240 );

        manual = new JCheckBox( "Get Manually" );
        getTile = new JButton( "Get Tile" );
        end = new JButton( "End" );
        start = new JButton( "Start" );
        numList = new JComboBox( intString );
        numList.setSelectedIndex( 0 );
        field = new JTextField( 10 );
        field.setText( "Enter Name" );
        area = new JTextArea( 670, 175 );
        area.setEditable( true );
        area.setText( "Press Start below after selection." );

        // set font
        area.setFont( new Font( "Serif", Font.PLAIN, 16 ) );

        scroller = new JScrollPane( area );
        scroller.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
        scroller.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

        pane = frame.getContentPane();
        pane.setLayout( null );
        pane.add( manual );
        pane.add( getTile );
        pane.add( end );
        pane.add( field );
        pane.add( start );
        pane.add( scroller );
        pane.add( numList );

        insets = pane.getInsets();

        scroller.setBounds( insets.left + 5, insets.top + 5, 670, 175 );
        start.setBounds( insets.left + 5, scroller.getY() + scroller.getHeight() + 5, getTile.getPreferredSize().width,
        getTile.getPreferredSize().height );
        getTile.setBounds( insets.left + start.getX() + start.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
        getTile.getPreferredSize().width, getTile.getPreferredSize().height );
        end.setBounds( insets.left + getTile.getX() + getTile.getWidth() + 5, scroller.getY() + scroller.getHeight() + 5,
        end.getPreferredSize().width, end.getPreferredSize().height );
        numList.setBounds( insets.left + end.getX() + end.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
        numList.getPreferredSize().width + 6, numList.getPreferredSize().height );
        field.setBounds( insets.left + numList.getX() + numList.getWidth() + 5, scroller.getY() + scroller.getHeight() + 6,
        field.getPreferredSize().width, field.getPreferredSize().height );
        manual.setBounds( insets.left + 550, scroller.getY() + scroller.getHeight() + 6, manual.getPreferredSize().width,
        manual.getPreferredSize().height );

        getTile.addActionListener( new getListener() );
        end.addActionListener( new endListener() );
        start.addActionListener( ( new startListener() ) );
        frame.setResizable( false );
        frame.setVisible( true );
        }

        public static class startListener implements ActionListener
        {
        public void actionPerformed(ActionEvent e)
        {
        if (!manual.isSelected())
        {
        startLoop = true;
        }

        if (field.getText().equals("Enter Name"))
        {
        area.setText("You must name your array");
        }
        else
        {
        area.setText( "Tile" + field.getText() );
        }
        }
        }

        public static class endListener implements ActionListener
        {
        public void actionPerformed( ActionEvent event )
        {
        area.append( " };" );
        area.selectAll();
        area.copy();
        }
        }

        public static class getListener implements ActionListener
        {
        public void actionPerformed( ActionEvent event )
        {
        if( firstTime )
        {
        newTile = String.format( "new Tile" );
        numTiles++;
        firstTime = false;
        }
        else
        {
        if( ++numTiles % 3 == 0 )
        {
        newTile = String.format( ",nt new Tile" );
        }
        else
        newTile = String.format( ", new Tile" );
        }
        area.append( newTile );
        }
        }

        public static void main( String args )
        {
        TileHelper helper = new TileHelper();
        helper.buildGUI();
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jun 14 '13 at 5:28

























        answered Jun 14 '13 at 3:47









        Raghav

        2,50321325




        2,50321325












        • Thats what I have read, yet it does not work when I run this.
          – sherrellbc
          Jun 14 '13 at 3:58










        • As said by @sethu, if you are on Mac, you'll have to go for a different command lists.apple.com/archives/java-dev/2008/Aug/msg00053.html
          – Raghav
          Jun 14 '13 at 5:32


















        • Thats what I have read, yet it does not work when I run this.
          – sherrellbc
          Jun 14 '13 at 3:58










        • As said by @sethu, if you are on Mac, you'll have to go for a different command lists.apple.com/archives/java-dev/2008/Aug/msg00053.html
          – Raghav
          Jun 14 '13 at 5:32
















        Thats what I have read, yet it does not work when I run this.
        – sherrellbc
        Jun 14 '13 at 3:58




        Thats what I have read, yet it does not work when I run this.
        – sherrellbc
        Jun 14 '13 at 3:58












        As said by @sethu, if you are on Mac, you'll have to go for a different command lists.apple.com/archives/java-dev/2008/Aug/msg00053.html
        – Raghav
        Jun 14 '13 at 5:32




        As said by @sethu, if you are on Mac, you'll have to go for a different command lists.apple.com/archives/java-dev/2008/Aug/msg00053.html
        – Raghav
        Jun 14 '13 at 5:32













        1














        Or else you could use something like the one in the below code.



        Keymap km = area.getKeymap();
        KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK);
        km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());





        share|improve this answer


























          1














          Or else you could use something like the one in the below code.



          Keymap km = area.getKeymap();
          KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK);
          km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());





          share|improve this answer
























            1












            1








            1






            Or else you could use something like the one in the below code.



            Keymap km = area.getKeymap();
            KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK);
            km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());





            share|improve this answer












            Or else you could use something like the one in the below code.



            Keymap km = area.getKeymap();
            KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK);
            km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 14 '13 at 6:34









            Raghav

            2,50321325




            2,50321325























                0














                Try:



                String str;



                str =TTextarea.getText();



                See this interesting link
                http://java-program-sample.blogspot.com/2011/08/copy-selected-text-from-jtextarea-to.html!






                share|improve this answer


























                  0














                  Try:



                  String str;



                  str =TTextarea.getText();



                  See this interesting link
                  http://java-program-sample.blogspot.com/2011/08/copy-selected-text-from-jtextarea-to.html!






                  share|improve this answer
























                    0












                    0








                    0






                    Try:



                    String str;



                    str =TTextarea.getText();



                    See this interesting link
                    http://java-program-sample.blogspot.com/2011/08/copy-selected-text-from-jtextarea-to.html!






                    share|improve this answer












                    Try:



                    String str;



                    str =TTextarea.getText();



                    See this interesting link
                    http://java-program-sample.blogspot.com/2011/08/copy-selected-text-from-jtextarea-to.html!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 13 '13 at 7:48









                    Ms_Joe

                    1002616




                    1002616























                        0














                        This worked for me (same as Raghav's answer, but it maps copy to Command-C instead of Control-C, which is what most Mac users expect).



                        String osName = System.getProperties().getProperty("os.name");
                        if (osName.startsWith("Mac OS X")) {
                        Keymap km = area.getKeymap();
                        KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C,
                        InputEvent.META_DOWN_MASK);
                        km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());
                        }





                        share|improve this answer


























                          0














                          This worked for me (same as Raghav's answer, but it maps copy to Command-C instead of Control-C, which is what most Mac users expect).



                          String osName = System.getProperties().getProperty("os.name");
                          if (osName.startsWith("Mac OS X")) {
                          Keymap km = area.getKeymap();
                          KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C,
                          InputEvent.META_DOWN_MASK);
                          km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());
                          }





                          share|improve this answer
























                            0












                            0








                            0






                            This worked for me (same as Raghav's answer, but it maps copy to Command-C instead of Control-C, which is what most Mac users expect).



                            String osName = System.getProperties().getProperty("os.name");
                            if (osName.startsWith("Mac OS X")) {
                            Keymap km = area.getKeymap();
                            KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C,
                            InputEvent.META_DOWN_MASK);
                            km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());
                            }





                            share|improve this answer












                            This worked for me (same as Raghav's answer, but it maps copy to Command-C instead of Control-C, which is what most Mac users expect).



                            String osName = System.getProperties().getProperty("os.name");
                            if (osName.startsWith("Mac OS X")) {
                            Keymap km = area.getKeymap();
                            KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C,
                            InputEvent.META_DOWN_MASK);
                            km.addActionForKeyStroke(ks, TransferHandler.getCopyAction());
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 27 '18 at 21:55









                            Carl Grundstrom

                            11




                            11






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17100691%2fhow-to-copy-from-a-jtextarea%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas