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 3692358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:15:11+00:00 2026-05-19T04:15:11+00:00

i have jtable filed with data. i want to create java code for Action

  • 0

i have jtable filed with data. i want to create java code for Action of the Jbutton. My requirement is when i click the button, then copy all the content of jtable to clip board. how can i do that.

    String[] columnNames={"DATE","Steet"};
    String[][] cells=new String[ar.size()][2];
    for(int i=0;i<ar.size();i++){
        cells[i][0]=((PRIvariable)ar.get(i)).incDate;
        cells[i][1]=((PRIvariable)ar.get(i)).selectedSteer;
    }
    table = new JTable(cells,columnNames);
    table.setVisible(true);
    table.setSize(400, 400);
    js=new JScrollPane();
    js.setViewportView(table);
    js.setBounds(10, 230,500, 215);
    js.setVisible(true);
    add(js,java.awt.BorderLayout.CENTER);
  • in this code ar is my arraylist.
  • how can i write the code witch can copy the content of this Jtable.
  • 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-19T04:15:12+00:00Added an answer on May 19, 2026 at 4:15 am

    When I needed to do this in the past I started with the code here:
    http://www.javaworld.com/javatips/jw-javatip77.html

    And modified to create an action for a button that would copy the data and the column headings from a table to the clipboard.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.StringSelection;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.util.StringTokenizer;
    
    
    
    
    /**
     * ExcelAdapter enables Copy-Paste Clipboard functionality on JTables. The clipboard data format used by the adapter is
     * compatible with the clipboard format used by Excel. This provides for clipboard interoperability between enabled
     * JTables and Excel.
     */
    public class ExcelAdapter implements ActionListener {
    
    
        private String rowstring, value;
        private Clipboard clipboard;
        private StringSelection stsel;
        private JTable jTable1;
    
    
        /**
         * The Excel Adapter is constructed with a JTable on which it enables Copy-Paste and acts as a Clipboard listener.
         */
        public ExcelAdapter(JTable myJTable) {
    
            jTable1 = myJTable;
            final KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK, false);
            // Identifying the copy KeyStroke user can modify this
            // to copy on some other Key combination.
            final KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK, false);
            // Identifying the Paste KeyStroke user can modify this
            //to copy on some other Key combination.
            jTable1.registerKeyboardAction(this, "Copy", copy, JComponent.WHEN_FOCUSED);
            jTable1.registerKeyboardAction(this, "Paste", paste, JComponent.WHEN_FOCUSED);
            clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        }
    
    
        /**
         * Public Accessor methods for the Table on which this adapter acts.
         */
        public JTable getJTable() {
    
            return jTable1;
        }
    
    
        public void setJTable(JTable jTable1) {
    
            this.jTable1 = jTable1;
        }
    
    
        /**
         * This method is activated on the Keystrokes we are listening to in this implementation. Here it listens for Copy
         * and Paste ActionCommands. Selections comprising non-adjacent cells result in invalid selection and then copy
         * action cannot be performed. Paste is done by aligning the upper left corner of the selection with the 1st element
         * in the current selection of the JTable.
         */
        @Override
        public void actionPerformed(ActionEvent e) {
    
            final String actionCommand = e.getActionCommand();
    
            if (actionCommand.equals("Copy")) {
    
                StringBuilder sbf = new StringBuilder();
                // Check to ensure we have selected only a contiguous block of cells.
                final int numcols = jTable1.getSelectedColumnCount();
                final int numrows = jTable1.getSelectedRowCount();
                final int[] rowsselected = jTable1.getSelectedRows();
                final int[] colsselected = jTable1.getSelectedColumns();
    
                if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0] &&
                        numrows == rowsselected.length) &&
                        (numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0] &&
                                numcols == colsselected.length))) {
                    JOptionPane.showMessageDialog(null, "Invalid Copy Selection",
                                                  "Invalid Copy Selection",
                                                  JOptionPane.ERROR_MESSAGE);
                    return;
                }
                for (int i = 0; i < numrows; i++) {
                    for (int j = 0; j < numcols; j++) {
                        sbf.append(jTable1.getValueAt(rowsselected[i], colsselected[j]));
                        if (j < numcols - 1) {
                            sbf.append('\t');
                        }
                    }
                    sbf.append('\n');
                }
                stsel = new StringSelection(sbf.toString());
                clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                clipboard.setContents(stsel, stsel);
    
            } else if (actionCommand.equals("Paste")) {
    
                System.out.println("Trying to Paste");
                final int startRow = (jTable1.getSelectedRows())[0];
                final int startCol = (jTable1.getSelectedColumns())[0];
                try {
                    final String trString = (String) (clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor));
                    System.out.println("String is:" + trString);
                    final StringTokenizer st1 = new StringTokenizer(trString, "\n");
                    for (int i = 0; st1.hasMoreTokens(); i++) {
                        rowstring = st1.nextToken();
                        StringTokenizer st2 = new StringTokenizer(rowstring, "\t");
                        for (int j = 0; st2.hasMoreTokens(); j++) {
                            value = (String) st2.nextToken();
                            if (startRow + i < jTable1.getRowCount() &&
                                    startCol + j < jTable1.getColumnCount()) {
                                jTable1.setValueAt(value, startRow + i, startCol + j);
                            }
                            System.out.println("Putting " + value + "at row = " + startRow + i + " column = " + startCol + j);
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
    
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is quite basic. I have a JTable showing cached data from a
I have a JTable filled with data of a table of my database (so
I have a JTable which is loaded from a data-structure using table model.The data-structure
I have a snippet to create a 'Like' button for our news site: <iframe
We have a Java Swing application which contains components like JTable, JCombobox, JTextArea and
I have a cell editor that contains a little button and then a textfield
I have a JTable with a custom TableModel called DataTableModel . I initialized the
I have a custom cell renderer set in JTable and it works but instead
Have you guys had any experiences (positive or negative) by placing your source code/solution
Hi I have created a Jtable and can get it to show on my

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.