Realizing the copy and paste features between Swing JTables and Excel [转] Many business applications today are developed by Java, and this will be more developed with Java. Swing JTAble components in many applications are used to display data in a similar spreadsheet. If the business application can import data into Microsoft Excel and export data from Microsoft Excel, it will make it easy for users to use the powerful features of the unnecessary spreadsheet program. This Java tip will help you understand the system clipboard and make your JTable to interact with Excel and interoperability. You will see that this useful function is added to achieve this useful function by adding another one-line code in the current application. To achieve this goal, just copy the file ExceLadapter.java given here, and make sure your application can find an ExceLadapter.class file; after finishing, JTable can talk to Excel. ! We will show you only through this line of code, how to achieve the copy and paste (Ctrl V) and paste (Ctrl V) and paste (Ctrl V) and paste (Ctrl V). An example application using ExceLadapter is also provided. The code below is an adapter code (called ExceLadapter.java): import java.awt. *; Import java.awt.event. *; Import javax.swing. *; Import java.awt.dataratfer. *; Import java.util. *; / ** * Exceladapter implements copy paste * clipboard function in jtables. The clipboard data format used in the adapter * is compatible with the clipboard format used in Excel. This provides interoperability between JTables and Excel * supported. * / Public class ExcelAdapter implements ActionListener {private String rowstring, value; private Clipboard system; private StringSelection stsel; private JTable jTable1; / ** * Excel adapter consists JTable, copy and paste it implements * * function on JTable, and act as Clipboard monitor. * / Public ExcelAdapter (JTable myJTable) {jTable1 = myJTable; KeyStroke copy = KeyStroke.getKeyStroke (KeyEvent.VK_C, ActionEvent.CTRL_MASK, false); // determine the copy button // user can modify other key combination to achieve Copy function. Keystroke Paste = Keystroke.getKeystroke (KeyEvent.vk_v, ActionEvent.ctrl_mask, false); // Determines that the paste button can modify it // to implement the replication function of other button combinations.
jTable1.registerKeyboardAction (this, "Copy", copy, JComponent.WHEN_FOCUSED); jTable1.registerKeyboardAction (this, "Paste", paste, JComponent.WHEN_FOCUSED); system = Toolkit.getDefaultToolkit () getSystemClipboard ();.} / ** * This adapter runs a common reading method for the chart. * / Public jtable getjtable () {return jtable1;} public void setjtable (jtable jtable1) {this.jtable1 = jtable1;} / *** Activate this method on our listening to this implementation. * Here, it listens to replication and paste ActionCommands. * Contains selection of non-adjacent cells caused to select invalid, * and then copy the actions cannot be performed. * Paste method is to align the first element of the upper left corner of the selected content to * JTABLE.
* / public void actionperformed (ActionEvent E) {if (E.GetActionCommand (). Compareto ("Copy") == 0) {StringBuffer SBF = New StringBuffer (); // Check to make sure we only select cell / / neighboring blocks 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))) {JOPANE.SHOWMESSAGEDIALOG (NULL, "Invalid Copy Selection", "Invalid Copy Selection", JOPANE.ERROR_MESSAGE); RETURN;} for (INT i = 0; I (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 ) {rowerTring = st1.nextToken (); stringtokenizer st2 = new stringtokenizer (Rowstring, "/ t"); for (int J = 0; st2.hasmoreToKens (); J ) {Value = (string) st2.nextToken (); if (Startrow i import java.awt *;. import javax.swing *;. public class Frame1 extends Frame {BorderLayout borderLayout1 = new BorderLayout (); JTable jTable1; Object [] [] data = new Object [4] [4]; Object header [ ] = {"Jan", "Feb", "Mar", "APR"}; public static void main (string args []) {frame1 myframe = new frame1 (); myframe.setsize (New Dimension (250, 250)); Myframe.setVisible (TRUE);} public frame1 () {Super (); try {jbinit ();} catch (exception e) {E.PrintStackTrace ();}} private void jbinit () throws exception {for (int I = 0; i <4; i ) for (int J = 0; j <4; j ) Data [i] [j] = new integer (i * 10 j); system.out.println ("Header length = " Header [1]); jtable1 = new jtable (data, header); jtable1.setcellselectionenable; this.SetTitle (" Excel Lent Jtable "); jtable1.setback; this.setLayout (BorderLayout1 ); This.setsize (New Dimension (400, 300)); this.setBackground (color.white); this.add (jtable1, borderla Yout.center); // This is the line of adding replication and paste! ExceLadapter myad = new exceladapter (jtable1);}} The clipboard format of the clipboard format Excel is very simple. It uses a tab to divide the elements on one line and take a line with a wrap. Thus, when you copy a set of continuous and / or adjacent cells, Excel only marks the spreadsheet data into a long string, and each cell value is separated by tabs in the string. . What should I do if the selected cell is not adjacent? Very simple: Excel does not let you copy the selected content to the clipboard. This behavior is imitated by the adapter described herein. If the selected cell is not adjacent, you will not copy data. In Excel, a dialog will pop up to tell us that it is not allowed to copy; this behavior is again imitated by the adapter. Code Simple Interpretation To use this feature, you need to download the ExceLadapter.java file, compile them, add the last line in the sample application to a location in your code to activate the adapter on the JTABLE. In the adapter, the activation button of the replication and paste function has been registered. Thereafter, the ActionPerformed method will be called whenever you typing the activated button.