Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7077981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:29:25+00:00 2026-05-28T06:29:25+00:00

By default, when you try to copy from a JTable , the toString method

  • 0

By default, when you try to copy from a JTable, the toString method of the value(s) being copied are sent to the clipboard. How can I change this behavior for one class of objects?

Let’s say I have a table with two columns for simplicity’s sake. The first column has Booleans in it and the second column has Strings. Currently when you copy a Boolean, you get either true or false. How could I change this behavior to place an arbitrary string on the clipboard (say t for true and f for false) without changing the copy behavior of String?

Here’s a SSCCE where you can copy / paste from a JTable.

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;

public class ChangeCopyBehavior {
    private static class TestModel extends AbstractTableModel {
        private static final long serialVersionUID = -774558262249729206L;

        @Override
        public int getRowCount() {
            return 4;
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Class<?> getColumnClass(int col) {
            return col == 0 ? Boolean.class : String.class;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 1) {
                return "String";
            } else {
                return rowIndex % 2 == 0;
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JTable table = new JTable(new TestModel());
                table.setCellSelectionEnabled(true);

                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                panel.add(new JScrollPane(table), BorderLayout.CENTER);
                panel.add(new JTextArea("Paste stuff here"), BorderLayout.SOUTH);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                frame.setContentPane(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:29:26+00:00Added an answer on May 28, 2026 at 6:29 am

    Create a custom TransferHandler to export the data in whatever format you want.

    Here is a link to the old ExtendedDnDDemo from the Swing tutorial that show an example of a custom TableTransferHandler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to change the default collation of a SQL Server instance using
I'm trying to copy the docklayout from one page and try to recreate it
For some reason, whenever I try to copy over a database from my assets
I have this html page I copied from here . There is a javascript
Notice how the default domain for stackoverflow is http://stackoverflow.com and if you try to
When i try to run a maven plugin, i found that the default classpath
I try to copy and localize a zendframework project seems every thing worked good
I am trying to copy a MyIsam dataBase from my a remote server to
I'm importing data from a CSV file that comes from excel, but i can't
I have taken a copy of a database home with me so I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.