2011-01-12 18 views
7

पर कॉपी करने के लिए कैसे करें मेरे पास jtable डेटा के साथ दायर किया गया है। मैं जब्बटन की कार्रवाई के लिए जावा कोड बनाना चाहता हूं। मेरी आवश्यकता तब होती है जब मैं बटन पर क्लिक करता हूं, तो प्रतिलिपि क्लिप बोर्ड पर जेटीई की सभी सामग्री कॉपी करें। मैं उसे कैसे कर सकता हूँ।jtable की सामग्री को क्लिपबोर्ड

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); 
    इस कोड में
  • ar मेरी ArrayList है।
  • मैं कोड चुड़ैल कैसे लिख सकता हूं इस जेटेबल की सामग्री की प्रतिलिपि बना सकता हूं।

उत्तर

6

जब मैं अतीत में ऐसा करने की जरूरत है मैं कोड यहाँ के साथ शुरू किया: http://www.javaworld.com/javatips/jw-javatip77.html

और एक बटन है कि एक तालिका से डेटा और स्तंभ शीर्षकों की नक़ल की के लिए एक कार्य बनाने के लिए संशोधित क्लिपबोर्ड पर।

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.datatransfer.*; 
import java.util.*; 
/** 
* 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 system; 
    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; 
     KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK,false); 
     // Identifying the copy KeyStroke user can modify this 
     // to copy on some other Key combination. 
     KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.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); 
     system = 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. 
    */ 
public void actionPerformed(ActionEvent e) 
    { 
     if (e.getActionCommand().compareTo("Copy")==0) 
     { 
     StringBuffer sbf=new StringBuffer(); 
     // Check to ensure we have selected only a contiguous block of 
     // cells 
     int numcols=jTable1.getSelectedColumnCount(); 
     int numrows=jTable1.getSelectedRowCount(); 
     int[] rowsselected=jTable1.getSelectedRows(); 
     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()); 
     system = Toolkit.getDefaultToolkit().getSystemClipboard(); 
     system.setContents(stsel,stsel); 
     } 
     if (e.getActionCommand().compareTo("Paste")==0) 
     { 
      System.out.println("Trying to Paste"); 
      int startRow=(jTable1.getSelectedRows())[0]; 
      int startCol=(jTable1.getSelectedColumns())[0]; 
      try 
      { 
      String trstring= (String)(system.getContents(this).getTransferData(DataFlavor.stringFlavor)); 
      System.out.println("String is:"+trstring); 
      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();} 
     } 
    } 
} 
8

जेटीबल पहले से ही एक प्रतिलिपि कार्रवाई का समर्थन करता है। Action Map Action दिखाता है कि आप JButton के साथ आसानी से इस क्रिया का उपयोग कैसे कर सकते हैं ताकि आपको कोड को फिर से लिखना न पड़े।

1

यहां जावावॉल्ड (1 999 से) एक्सेल एडाप्टर का नवीनीकृत संस्करण है। Link

उपयोग के लिए:

jTable1.addKeyListener(new ClipboardKeyAdapter(jTable1)); 

ClipboardKeyAdapter.java

 public class ClipboardKeyAdapter extends KeyAdapter { 

     private static final String LINE_BREAK = "\r"; 
     private static final String CELL_BREAK = "\t"; 
     private static final Clipboard CLIPBOARD = Toolkit.getDefaultToolkit().getSystemClipboard(); 

     private final JTable table; 

     public ClipboardKeyAdapter(JTable table) { 
       this.table = table; 
     } 

     @Override 
     public void keyReleased(KeyEvent event) { 
       if (event.isControlDown()) { 
         if (event.getKeyCode()==KeyEvent.VK_C) { // Copy       
           cancelEditing(); 
           copyToClipboard(false); 
         } else if (event.getKeyCode()==KeyEvent.VK_X) { // Cut 
           cancelEditing(); 
           copyToClipboard(true); 
         } else if (event.getKeyCode()==KeyEvent.VK_V) { // Paste 
           cancelEditing(); 
           pasteFromClipboard();   
         } 
       } 
     } 

     private void copyToClipboard(boolean isCut) { 
       int numCols=table.getSelectedColumnCount(); 
       int numRows=table.getSelectedRowCount(); 
       int[] rowsSelected=table.getSelectedRows(); 
       int[] colsSelected=table.getSelectedColumns(); 
       if (numRows!=rowsSelected[rowsSelected.length-1]-rowsSelected[0]+1 || numRows!=rowsSelected.length || 
           numCols!=colsSelected[colsSelected.length-1]-colsSelected[0]+1 || numCols!=colsSelected.length) { 

         JOptionPane.showMessageDialog(null, "Invalid Copy Selection", "Invalid Copy Selection", JOptionPane.ERROR_MESSAGE); 
         return; 
       } 

       StringBuffer excelStr=new StringBuffer(); 
       for (int i=0; i<numRows; i++) { 
         for (int j=0; j<numCols; j++) { 
           excelStr.append(escape(table.getValueAt(rowsSelected[i], colsSelected[j]))); 
           if (isCut) { 
             table.setValueAt(null, rowsSelected[i], colsSelected[j]); 
           } 
           if (j<numCols-1) { 
             excelStr.append(CELL_BREAK); 
           } 
         } 
         excelStr.append(LINE_BREAK); 
       } 

       StringSelection sel = new StringSelection(excelStr.toString()); 
       CLIPBOARD.setContents(sel, sel); 
     } 

     private void pasteFromClipboard() { 
       int startRow=table.getSelectedRows()[0]; 
       int startCol=table.getSelectedColumns()[0]; 

       String pasteString = ""; 
       try { 
         pasteString = (String)(CLIPBOARD.getContents(this).getTransferData(DataFlavor.stringFlavor)); 
       } catch (Exception e) { 
         JOptionPane.showMessageDialog(null, "Invalid Paste Type", "Invalid Paste Type", JOptionPane.ERROR_MESSAGE); 
         return; 
       } 

       String[] lines = pasteString.split(LINE_BREAK); 

       for (int i=0 ; i<lines.length; i++) { 
         String[] cells = lines[i].split(CELL_BREAK); 
         for (int j=0 ; j<cells.length; j++) { 
           if (table.getRowCount()>startRow+i && table.getColumnCount()>startCol+j) { 
             table.setValueAt(cells[j], startRow+i, startCol+j); 
           } 
         } 
       } 
     } 

     private void cancelEditing() { 
       if (table.getCellEditor() != null) { 
         table.getCellEditor().cancelCellEditing(); 
      } 
     } 

     private String escape(Object cell) { 
       return cell.toString().replace(LINE_BREAK, " ").replace(CELL_BREAK, " "); 
     } 
}