Realize copy and paste features between Swing JTables and Excel

xiaoxiao2021-03-06  110

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

转载请注明原文地址:https://www.9cbs.com/read-103326.html

New Post(0)