java swing 把控件转化为BufferedImage

简介:

Java swing 把控件映射为BufferedImage

如何把java swing的可视控件 转化为BufferedImage 呢?

直接上代码:

Java代码   收藏代码
  1. /*** 
  2.          * convert JTextArea to image 
  3.          * @param ta 
  4.          * @param destFile 
  5.          * @param format 
  6.          */  
  7.         public static BufferedImage genericImage(JComponent ta,File destFile,String format){//TODO 如何提高分辨率  
  8.             BufferedImage img = new BufferedImage(ta.getWidth(), ta.getHeight(), BufferedImage.TYPE_INT_RGB);  
  9.             Graphics2D g2d = img.createGraphics();  
  10.             ta.printAll(g2d);  
  11.             g2d.dispose();  
  12.             if(!ValueWidget.isNullOrEmpty(destFile)){  
  13.                 try {  
  14.                     ImageIO.write(img, format/*"jpg"*/, destFile);  
  15.                 } catch (IOException ex) {  
  16.                     ex.printStackTrace();  
  17.                 }  
  18.             }  
  19.               
  20.             return img;  
  21.         }  

 

如何把BufferedImage 复制到剪切板:

Java代码   收藏代码
  1. /*** 
  2.      * 复制图片到剪切板 
  3.      * @param image 
  4.      */  
  5.     public static void setClipboardImage(Container frame, final Image image) {  
  6.         Transferable trans = new Transferable() {  
  7.             @Override  
  8.             public Object getTransferData(DataFlavor flavor)  
  9.                     throws UnsupportedFlavorException, IOException {  
  10.                 if (isDataFlavorSupported(flavor)) {  
  11.                     return image;  
  12.                 }  
  13.                 throw new UnsupportedFlavorException(flavor);  
  14.             }  
  15.   
  16.             @Override  
  17.             public DataFlavor[] getTransferDataFlavors() {  
  18.                 return new DataFlavor[] { DataFlavor.imageFlavor };  
  19.             }  
  20.   
  21.             @Override  
  22.             public boolean isDataFlavorSupported(DataFlavor flavor) {  
  23.                 return DataFlavor.imageFlavor.equals(flavor);  
  24.             }  
  25.         };  
  26.           
  27.         frame.getToolkit().getSystemClipboard().setContents(trans, null);  
  28.     }  

 

 

 

调用:

Java代码   收藏代码
  1. BufferedImage img =ImageHWUtil.genericImage(ta, null"jpg"/*picFormat*/);  
  2.                     if(ValueWidget.isNullOrEmpty(img)){  
  3.                         return;  
  4.                     }  
  5.                     ComponentUtil.setClipboardImage(ta.getParent(),img);  
  6.                     ToastMessage toastMessage = new ToastMessage("复制图片到剪切板",3000);  
  7.                     toastMessage.setVisible(true);  

 

下面是详细工具类:

Java代码   收藏代码
  1. package com.swing.component;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Container;  
  5. import java.awt.Frame;  
  6. import java.awt.Image;  
  7. import java.awt.datatransfer.DataFlavor;  
  8. import java.awt.datatransfer.Transferable;  
  9. import java.awt.datatransfer.UnsupportedFlavorException;  
  10. import java.awt.event.ActionEvent;  
  11. import java.awt.event.ActionListener;  
  12. import java.awt.event.ItemEvent;  
  13. import java.awt.event.ItemListener;  
  14. import java.awt.event.MouseAdapter;  
  15. import java.awt.event.MouseEvent;  
  16. import java.awt.image.BufferedImage;  
  17. import java.io.File;  
  18. import java.io.FileOutputStream;  
  19. import java.io.IOException;  
  20. import java.io.InputStream;  
  21. import java.util.ArrayList;  
  22. import java.util.List;  
  23.   
  24. import javax.swing.JButton;  
  25. import javax.swing.JCheckBox;  
  26. import javax.swing.JComboBox;  
  27. import javax.swing.JProgressBar;  
  28. import javax.swing.JTextArea;  
  29. import javax.swing.JTextField;  
  30. import javax.swing.JTextPane;  
  31. import javax.swing.event.DocumentEvent;  
  32. import javax.swing.event.DocumentListener;  
  33. import javax.swing.text.AttributeSet;  
  34. import javax.swing.text.BadLocationException;  
  35. import javax.swing.text.Document;  
  36. import javax.swing.text.JTextComponent;  
  37.   
  38. import com.common.dict.Constant2;  
  39. import com.common.util.SystemHWUtil;  
  40. import com.common.util.TypeUtil;  
  41. import com.common.util.WindowUtil;  
  42. import com.io.hw.file.util.FileUtils;  
  43. import com.string.widget.util.ValueWidget;  
  44. import com.swing.menu.MenuUtil2;  
  45. import com.swing.messagebox.GUIUtil23;  
  46. import com.time.util.TimeHWUtil;  
  47.   
  48. public final class ComponentUtil {  
  49.     /*** 
  50.      * 为了防止JTextField 重复增加 DocumentListener 
  51.      */  
  52.     public static List<JTextField> tfs = new ArrayList<JTextField>();  
  53.   
  54.     /*** 
  55.      * 获取指定页面 复选框被选中的个数 
  56.      *  
  57.      * @param checkBoxes 
  58.      * @param startIndex 
  59.      *            :currentPage * size_per_page 
  60.      * @param count 
  61.      * @return 
  62.      */  
  63.     public static int getSelSum(List checkBoxes, int startIndex, int count) {  
  64.         if (checkBoxes == null || checkBoxes.size() == 0) {  
  65.             return 0;  
  66.         } else {  
  67.             JCheckBox[] chkArr = getCurrentPageChkbox(checkBoxes, startIndex,  
  68.                     count);  
  69.             if (chkArr == null) {  
  70.                 return 0;  
  71.             }  
  72.             int tmp = 0;  
  73.             for (int i = 0; i < chkArr.length; i++) {  
  74.                 JCheckBox array_element = chkArr[i];  
  75.                 tmp += TypeUtil.bool2int(array_element.isSelected());  
  76.             }  
  77.             return tmp;  
  78.         }  
  79.     }  
  80.   
  81.     /*** 
  82.      *  
  83.      * @param checkBoxes 
  84.      * @param startIndex 
  85.      *            :0,5,10,15 ;if =-1,indicate no paging 
  86.      * @param count 
  87.      *            :size of per page 
  88.      */  
  89.     public static void setSelect(List checkBoxes, int startIndex, int count) {  
  90.         JCheckBox[] chkArr = getCurrentPageChkbox(checkBoxes, startIndex, count);  
  91.         if (ValueWidget.isNullOrEmpty(chkArr)) {  
  92.             return;  
  93.         }  
  94.         for (int i = 0; i < chkArr.length; i++) {  
  95.             JCheckBox chk = chkArr[i];  
  96.             chk.setSelected(true);  
  97. //          System.out.println(2);  
  98.         }  
  99.     }  
  100.   
  101.     /*** 
  102.      *  
  103.      * @param checkBoxes 
  104.      * @param startIndex 
  105.      *            :0,5,10,15 ;if =-1,indicate no paging and omit count 
  106.      * @param count 
  107.      *            :omit when startIndex=-1 
  108.      * @return 
  109.      */  
  110.     public static JCheckBox[] getCurrentPageChkbox(List checkBoxes,  
  111.             int startIndex, int count) {  
  112.         if (checkBoxes == null || checkBoxes.size() == 0) {  
  113.             return null;  
  114.         } else {  
  115.             int endIndex = startIndex + count;  
  116.             int sum_chk = checkBoxes.size();  
  117.             if (/*startIndex == -1*/startIndex <0) {  
  118.                 startIndex = 0;  
  119.             } else {  
  120.                 if (sum_chk < endIndex) {  
  121.                     endIndex = sum_chk;  
  122.                 }  
  123.             }  
  124.             JCheckBox[] chkArr = new JCheckBox[endIndex - startIndex];  
  125.             int index3 = 0;  
  126.             for (int i = startIndex; i < endIndex&i<sum_chk; i++) {  
  127.                 JCheckBox chk = (JCheckBox) checkBoxes.get(i);  
  128.                 chkArr[index3] = chk;  
  129.                 index3++;  
  130.             }  
  131.             return chkArr;  
  132.         }  
  133.     }  
  134.   
  135.     public static int getCurrentPageChkboxSum(List checkBoxes, int startIndex,  
  136.             int count) {  
  137.         if (checkBoxes == null || checkBoxes.size() == 0) {  
  138.             return 0;  
  139.         } else {  
  140.             JCheckBox[] chkArr = getCurrentPageChkbox(checkBoxes, startIndex,  
  141.                     count);  
  142.             return chkArr.length;  
  143.         }  
  144.     }  
  145.   
  146.     /*** 
  147.      * 当文本框中输入\ 时,自动扫描该目录下是否只有一个文件,若只有一个文件,设置文本框的值为该文件的绝对路径 
  148.      *  
  149.      * @param sourceTF 
  150.      * @param e 
  151.      * @throws BadLocationException 
  152.      */  
  153.     public static void assistantTF(final JTextField sourceTF, DocumentEvent e)  
  154.             throws BadLocationException {  
  155.         int changeLength = e.getLength();  
  156.         if (changeLength == 1) {// 表示一次性增加的字符个数是1  
  157.             final Document doc = e.getDocument();  
  158.             final String input = doc.getText(e.getDocument().getLength() - 1,  
  159.                     changeLength);  
  160.             String filepath = null;  
  161.             File[] files = null;  
  162.             final String sourceFileStr = sourceTF.getText();  
  163.             if (input.endsWith(SystemHWUtil.SEPARATOR)) {// 输入的必须是一个字符,必须是\  
  164.                 files = FileUtils.getFilesByPathAndSuffix(sourceFileStr, "");  
  165.   
  166.             } else {  
  167.   
  168.                 String fatherFolder = SystemHWUtil.getParentDir(sourceFileStr);  
  169.                 if (!ValueWidget.isNullOrEmpty(fatherFolder)) {  
  170.                     files = FileUtils.getFilesByPathAndPrefix(fatherFolder,  
  171.                             SystemHWUtil.getFileSimpleName(sourceFileStr));  
  172.                 }  
  173.   
  174.             }  
  175.             if (!ValueWidget.isNullOrEmpty(files)) {  
  176.                 if (files.length == 1) {  
  177.                     filepath = files[0].getAbsolutePath();  
  178.                     // System.out.println(filepath);  
  179.                 }  
  180.             }  
  181.             if (!ValueWidget.isNullOrEmpty(filepath)) {  
  182.                 // System.out.println("input:" + filepath);  
  183.                 // 临时变量  
  184.                 final String filepath_tmp = filepath;  
  185.                 new Thread(new Runnable() {  
  186.                     @Override  
  187.                     public void run() {  
  188.                         // int length2=sourceFileStr.length();  
  189.                         // try {  
  190.                         // doc.insertString(length2-1, "abc", null);  
  191.                         // } catch (BadLocationException e4) {  
  192.                         // e4.printStackTrace();  
  193.                         // }  
  194.                         sourceTF.setText(filepath_tmp);// 应该放在线程中,不然会报异常:java.lang.IllegalStateException:  
  195.                                                         // Attempt to mutate in  
  196.                                                         // notification,参考:http://stackoverflow.com/questions/2788779/java-lang-illegalstateexception-while-using-document-listener-in-textarea-java  
  197.                         sourceTF.setCaretPosition(filepath_tmp.length());  
  198.                         // sourceTF.updateUI();  
  199.                     }  
  200.                 }).start();  
  201.             }  
  202.         }  
  203.     }  
  204.   
  205.     public static void assistantTF(final JTextField sourceTF) {  
  206.         boolean isContains = tfs.contains(sourceTF);  
  207.         if (isContains) {  
  208.             throw new RuntimeException(  
  209.                     "This JTextField has added the DocumentListener.");  
  210.         }  
  211.         final Document doc = sourceTF.getDocument();  
  212.         DocumentListener docLis = new DocumentListener() {  
  213.   
  214.             @Override  
  215.             public void removeUpdate(DocumentEvent e) {  
  216.   
  217.             }  
  218.   
  219.             @Override  
  220.             public void insertUpdate(DocumentEvent e) {  
  221.                 // System.out.println("insert");  
  222.                 try {  
  223.                     ComponentUtil.assistantTF(sourceTF, e);// Assist path  
  224.                                                             // complement  
  225.                 } catch (BadLocationException e2) {  
  226.                     e2.printStackTrace();  
  227.                 }  
  228.   
  229.             }  
  230.   
  231.             @Override  
  232.             public void changedUpdate(DocumentEvent e) {  
  233.                 // System.out.println("change");  
  234.             }  
  235.         };  
  236.         doc.addDocumentListener(docLis);  
  237.         tfs.add(sourceTF);  
  238.     }  
  239.   
  240.     /*** 
  241.      * Get a copy button. 
  242.      *  
  243.      * @param tp 
  244.      * @return 
  245.      */  
  246.     public static JButton getCopyBtn(final JTextPane tp) {  
  247.         JButton copyBtn = new JButton(MenuUtil2.ACTION_STR_COPY);  
  248.         copyBtn.addActionListener(new ActionListener() {  
  249.             @Override  
  250.             public void actionPerformed(ActionEvent e) {  
  251.                 if(!ValueWidget.isNullOrEmpty(tp)){  
  252.                 String input = tp.getText();  
  253.                 if (!ValueWidget.isNullOrEmpty(input)) {  
  254.                     WindowUtil.setSysClipboardText(input);  
  255.                 }  
  256.             }}  
  257.         });  
  258.         return copyBtn;  
  259.     }  
  260.   
  261.     /*** 
  262.      * Get a copy button. 
  263.      *  
  264.      * @param tf 
  265.      * @return 
  266.      *//* 
  267.     public static JButton getCopyBtn(final JTextField tf) { 
  268.         JButton copyBtn = new JButton(MenuUtil2.ACTION_STR_COPY); 
  269.         copyBtn.addActionListener(new ActionListener() { 
  270.             @Override 
  271.             public void actionPerformed(ActionEvent e) { 
  272.                 if(!ValueWidget.isNullOrEmpty(tf)){ 
  273.                 String input = tf.getText(); 
  274.                 if (!ValueWidget.isNullOrEmpty(input)) { 
  275.                     WindowUtil.setSysClipboardText(input); 
  276.                 } 
  277.             }} 
  278.         }); 
  279.         return copyBtn; 
  280.     }*/  
  281.   
  282.     /*** 
  283.      * Get a copy button. 
  284.      *  
  285.      * @param tf 
  286.      * @return 
  287.      */  
  288.     public static JButton getCopyBtn(final JTextComponent ta) {  
  289.         JButton copyBtn = new JButton(MenuUtil2.ACTION_STR_COPY);  
  290.         copyBtn.addActionListener(new ActionListener() {  
  291.             @Override  
  292.             public void actionPerformed(ActionEvent e) {  
  293.                 if(!ValueWidget.isNullOrEmpty(ta)){  
  294.                     String input = ta.getText();  
  295.                     if (!ValueWidget.isNullOrEmpty(input)) {  
  296.                         WindowUtil.setSysClipboardText(input);  
  297.                     }  
  298.                 }  
  299.             }  
  300.         });  
  301.         return copyBtn;  
  302.     }  
  303.   
  304.     /*** 
  305.      * Get a paste button. 
  306.      *  
  307.      * @param ta 
  308.      * @return 
  309.      */  
  310.     public static JButton getPasteBtn(final JTextComponent ta) {  
  311.         JButton copyBtn = new JButton(MenuUtil2.ACTION_STR_PASTE);  
  312.         copyBtn.addActionListener(new ActionListener() {  
  313.             @Override  
  314.             public void actionPerformed(ActionEvent e) {  
  315.                 String input = WindowUtil.getSysClipboardText();  
  316.                 if (!ValueWidget.isNullOrEmpty(input)) {  
  317.                     ta.setText(input);  
  318.                 }  
  319.             }  
  320.         });  
  321.         return copyBtn;  
  322.     }  
  323.   
  324.     /*** 
  325.      * Get a paste button. 
  326.      *  
  327.      * @param tf 
  328.      * @return 
  329.      */  
  330.     /*public static JButton getPasteBtn(final JTextField tf) { 
  331.         JButton copyBtn = new JButton(MenuUtil2.ACTION_STR_PASTE); 
  332.         copyBtn.addActionListener(new ActionListener() { 
  333.             @Override 
  334.             public void actionPerformed(ActionEvent e) { 
  335.                 String input = WindowUtil.getSysClipboardText(); 
  336.                 if (!ValueWidget.isNullOrEmpty(input)) { 
  337.                     tf.setText(input); 
  338.                 } 
  339.             } 
  340.         }); 
  341.         return copyBtn; 
  342.     }*/  
  343.   
  344.     /*** 
  345.      * Get a paste button. 
  346.      *  
  347.      * @param tp 
  348.      * @return 
  349.      */  
  350.     public static JButton getPasteBtn(final JTextPane tp) {  
  351.         if(ValueWidget.isNullOrEmpty(tp)){  
  352.             return null;  
  353.         }  
  354.         JButton copyBtn = new JButton(MenuUtil2.ACTION_STR_PASTE);  
  355.         copyBtn.addActionListener(new ActionListener() {  
  356.             @Override  
  357.             public void actionPerformed(ActionEvent e) {  
  358.                 String input = WindowUtil.getSysClipboardText();  
  359.                 if (!ValueWidget.isNullOrEmpty(input)) {  
  360.                     tp.setText(input);  
  361.                 }  
  362.             }  
  363.         });  
  364.         return copyBtn;  
  365.     }  
  366.     /*** 
  367.      * 默认显示时间 
  368.      * @param resultTextArea 
  369.      * @param result 
  370.      * @param isAddDivide 
  371.      */  
  372.     public static void appendResult(JTextArea resultTextArea, String result,  
  373.             boolean isAddDivide){  
  374.         appendResult(resultTextArea, result, isAddDivide, true);  
  375.     }  
  376.     /*** 
  377.      *  
  378.      * @param resultTextArea 
  379.      * @param result 
  380.      * @param isAddDivide 
  381.      * @param prependTime : 是否显示时间 
  382.      */  
  383.     public static void appendResult(JTextArea resultTextArea, String result,  
  384.             boolean isAddDivide,boolean prependTime)   
  385.     {  
  386.         if(ValueWidget.isNullOrEmpty(resultTextArea)){  
  387.             return;  
  388.         }  
  389.         appendResult(resultTextArea, result, isAddDivide, false,prependTime);  
  390.     }  
  391.     /*** 
  392.      * 向结果文本框追加内容,不会删除原来的内容。 
  393.      *  
  394.      * @param result 
  395.      * @param CRLFbefore : 表示是否在前面添加一个回车换行 
  396.      * @param isAddDivide 
  397.      * @param prependTime : 是否显示时间 
  398.      */  
  399.     public static void appendResult(JTextArea resultTextArea, String result,  
  400.             boolean isAddDivide,boolean CRLFbefore,boolean prependTime) {  
  401.         if(ValueWidget.isNullOrEmpty(resultTextArea)){  
  402.             return;  
  403.         }  
  404.         if (result == null) {  
  405.             result = "";  
  406.         } else {  
  407.             result = result + SystemHWUtil.CRLF;  
  408.             if(prependTime){  
  409.                 String currentTime=TimeHWUtil.getCurrentMiniuteSecondZH();  
  410.                 result = currentTime+"\t|  "+result;  
  411.             }  
  412.         }  
  413.         if(CRLFbefore){  
  414.             result=SystemHWUtil.CRLF+result;  
  415.         }  
  416.         Document doc = resultTextArea.getDocument();  
  417.         int length = doc.getLength();  
  418.         try {  
  419.             doc.insertString(length, result, null);  
  420.             length = length + result.length();  
  421.             if (isAddDivide) {  
  422.                 doc.insertString(length, SystemHWUtil.DIVIDING_LINE  
  423.                         + SystemHWUtil.CRLF, null);  
  424.             }  
  425.         } catch (BadLocationException e) {  
  426.             e.printStackTrace();  
  427.             GUIUtil23.warningDialog(e.getMessage());  
  428.         }  
  429.   
  430.     }  
  431.     /*** 
  432.      * 向结果文本框追加内容,不会删除原来的内容。 
  433.      *  
  434.      * @param result 
  435.      * @param isAddDivide 
  436.      */  
  437.     public static void appendResult(JTextPane resultTextArea, String result,  
  438.             boolean isAddDivide) {  
  439.         if(ValueWidget.isNullOrEmpty(resultTextArea)){  
  440.             return;  
  441.         }  
  442.         appendResult(resultTextArea, result, null, isAddDivide);  
  443.     }  
  444.     /*** 
  445.      * 向结果文本框追加内容,不会删除原来的内容。 
  446.      *  
  447.      * @param result 
  448.      * @param isAddDivide 
  449.      */  
  450.     public static void appendResult(JTextPane resultTextPane, String result, AttributeSet set,  
  451.             boolean isAddDivide) {  
  452.         if(ValueWidget.isNullOrEmpty(resultTextPane)){  
  453.             return;  
  454.         }  
  455.         if (result == null) {  
  456.             result = "";  
  457.         } else {  
  458.             result = result + SystemHWUtil.CRLF;  
  459.         }  
  460.         Document doc = resultTextPane.getDocument();resultTextPane.getText();  
  461.         int length = doc.getLength();  
  462. //      if(length>7){  
  463. //          length=length-17;  
  464. //      }  
  465.         try {  
  466.             doc.insertString(length, result, set);  
  467.             length = length + result.length();  
  468.             if (isAddDivide) {  
  469.                 doc.insertString(length, SystemHWUtil.DIVIDING_LINE  
  470.                         + SystemHWUtil.CRLF, null);  
  471.             }  
  472.         } catch (BadLocationException e) {  
  473.             e.printStackTrace();  
  474.             GUIUtil23.warningDialog(e.getMessage());  
  475.         }  
  476.   
  477.     }  
  478.   
  479.     /*** 
  480.      * 在实际使用过程中,应该放在线程中 
  481.      *  
  482.      * @param in 
  483.      * @param sourceSize 
  484.      *            : 输入流的长度,即要读取的字节个数 
  485.      * @param targfile 
  486.      */  
  487.     public static void progress(JProgressBar copyProgressBar, InputStream in,  
  488.             long sourceSize, File targfile) {  
  489.         FileOutputStream target = null;  
  490.         try {  
  491.             int bytesArrLeng = 0;  
  492.             if (sourceSize < SystemHWUtil.BUFF_SIZE_1024) {//  
  493.                 bytesArrLeng = (int) sourceSize;  
  494.             } else {  
  495.                 long shangOne = sourceSize / SystemHWUtil.NUMBER_100;  
  496.                 if (shangOne == 0) {// sourceSize<100  
  497.                     shangOne = shangOne + 1;  
  498.                 }  
  499.   
  500.                 if (shangOne <= SystemHWUtil.BUFF_SIZE_1024) {  
  501.                     bytesArrLeng = (int) shangOne;  
  502.                 } else {  
  503.                     long shangTwo = shangOne / SystemHWUtil.BUFF_SIZE_1024;  
  504.                     if (shangOne % SystemHWUtil.BUFF_SIZE_1024 != 0) {  
  505.                         shangTwo = shangTwo + 1L;  
  506.                     }  
  507.                     bytesArrLeng = (int) (shangOne / shangTwo);  
  508.                 }  
  509.             }  
  510.             System.out.println("字节数组的长度是:" + bytesArrLeng);  
  511.             target = new FileOutputStream(targfile);  
  512.             byte[] buffer = new byte[bytesArrLeng];  
  513.             int byteNum;  
  514.             long hasReadByte = 0L;// 已经读取的字节个数  
  515.             float result;  
  516.             int progressSize = 0;  
  517.             while ((byteNum = in.read(buffer)) != SystemHWUtil.NEGATIVE_ONE) {  
  518.                 target.write(buffer, 0, byteNum);  
  519.                 hasReadByte = hasReadByte + byteNum;  
  520.                 result = (float) ((double) hasReadByte / sourceSize);  
  521.                 progressSize = Math.round(result * SystemHWUtil.NUMBER_100);  
  522.                 updateProgress(copyProgressBar, progressSize);  
  523.             }  
  524.             if (progressSize < SystemHWUtil.NUMBER_100) {  
  525.                 progressSize = SystemHWUtil.NUMBER_100;  
  526.                 updateProgress(copyProgressBar, progressSize);  
  527.             }  
  528.             copyProgressBar.setForeground(Color.blue);  
  529.   
  530.         } catch (Exception e) {  
  531.             e.printStackTrace();  
  532.             GUIUtil23.errorDialog(e);  
  533.             // copyFileBtn.setEnabled(true);  
  534.         } finally {  
  535.             if (in != null) {  
  536.                 try {  
  537.                     in.close();  
  538.                 } catch (IOException e) {  
  539.                     e.printStackTrace();  
  540.                     GUIUtil23.errorDialog(e);  
  541.                 }  
  542.                 in = null;  
  543.             }  
  544.             if (target != null) {  
  545.                 try {  
  546.                     target.close();  
  547.                 } catch (IOException e) {  
  548.                     e.printStackTrace();  
  549.                     GUIUtil23.errorDialog(e);  
  550.                 }  
  551.                 target = null;  
  552.             }  
  553.         }  
  554.     }  
  555.   
  556.     /*** 
  557.      * 更新进度条上得进度数字 
  558.      *  
  559.      * @param copyProgressBar 
  560.      * @param progressSize 
  561.      */  
  562.     private static void updateProgress(JProgressBar copyProgressBar,  
  563.             int progressSize) {  
  564.         copyProgressBar.setString(progressSize + "%");  
  565.         copyProgressBar.setValue(progressSize);  
  566.     }  
  567.     /*** 
  568.      * 复制图片到剪切板 
  569.      * @param image 
  570.      */  
  571.     public static void setClipboardImage(Container frame, final Image image) {  
  572.         Transferable trans = new Transferable() {  
  573.             @Override  
  574.             public Object getTransferData(DataFlavor flavor)  
  575.                     throws UnsupportedFlavorException, IOException {  
  576.                 if (isDataFlavorSupported(flavor)) {  
  577.                     return image;  
  578.                 }  
  579.                 throw new UnsupportedFlavorException(flavor);  
  580.             }  
  581.   
  582.             @Override  
  583.             public DataFlavor[] getTransferDataFlavors() {  
  584.                 return new DataFlavor[] { DataFlavor.imageFlavor };  
  585.             }  
  586.   
  587.             @Override  
  588.             public boolean isDataFlavorSupported(DataFlavor flavor) {  
  589.                 return DataFlavor.imageFlavor.equals(flavor);  
  590.             }  
  591.         };  
  592.           
  593.         frame.getToolkit().getSystemClipboard().setContents(trans, null);  
  594.     }  
  595.     public static BufferedImage getClipboardImage(Frame frame) {  
  596.         // java.lang.ClassCastException: sun.awt.datatransfer.TransferableProxy cannot be cast to sun.awt.datatransfer.ClipboardTransferable  
  597.         Transferable trans=frame.getToolkit().getSystemClipboard().getContents(null);  
  598.         BufferedImage image=null;  
  599. //      if(trans instanceof ClipboardTransferable){  
  600. //      ClipboardTransferable clipboardTrans =(ClipboardTransferable)trans;  
  601.           
  602.         try {  
  603.             if (null != trans && trans.isDataFlavorSupported(DataFlavor.imageFlavor)) {     
  604.             Object obj22=trans.getTransferData(DataFlavor.imageFlavor);  
  605.             if(!ValueWidget.isNullOrEmpty(obj22)){  
  606.                 if(obj22 instanceof BufferedImage){  
  607.                 image=(BufferedImage)obj22;  
  608.                 }  
  609.             }  
  610.             }  
  611.         } catch (UnsupportedFlavorException e1) {  
  612.             e1.printStackTrace();  
  613.             GUIUtil23.errorDialog(e1);  
  614.         } catch (IOException e1) {  
  615.             e1.printStackTrace();  
  616.             GUIUtil23.errorDialog(e1);  
  617.         }  
  618. //      }  
  619.           
  620.           
  621.         /*try { 
  622.             Map map=(Map)ReflectHWUtils.getObjectValue(clipboardTrans, "flavorsToData"); 
  623.             Object val=null; 
  624.             for(Object obj:map.keySet()){ 
  625.                 val=map.get(obj); 
  626.                 break; 
  627.             } 
  628.             byte[] data=(byte[])ReflectHWUtils.getObjectValue(val, "data"); 
  629.              
  630.             return data;//(BufferedImage)trans.getTransferData(trans.getTransferDataFlavors()[0]); 
  631.         } catch (SecurityException e) { 
  632.             e.printStackTrace(); 
  633.         } catch (IllegalArgumentException e) { 
  634.             e.printStackTrace(); 
  635.         } catch (NoSuchFieldException e) { 
  636.             e.printStackTrace(); 
  637.         } catch (IllegalAccessException e) { 
  638.             e.printStackTrace(); 
  639.         }*/  
  640.         return image;  
  641.     }  
  642.     /*** 
  643.      *  
  644.      * @param comb 
  645.      * @param urls 
  646.      */  
  647.     public static void fillComboBox(JComboBox<String> comb,String urls[]){  
  648.         if(comb==null||ValueWidget.isNullOrEmpty(urls)){  
  649.             return;  
  650.         }  
  651.         for(int i=0;i<urls.length;i++){  
  652.             comb.addItem(urls[i]);  
  653.         }  
  654.     }  
  655.     /*** 
  656.      *  
  657.      * @param comb : 下拉框 
  658.      * @param picUrls : 以;;分割的字符串 
  659.      */  
  660.     public static void fillComboBox(JComboBox<String> comb,String picUrls){  
  661.         if(!ValueWidget.isNullOrEmpty(picUrls)){//为空判断  
  662.             String urls[]=picUrls.split(Constant2.SHAREDPICDIVISION);  
  663.             ComponentUtil.fillComboBox(comb, SystemHWUtil.unique(urls));  
  664.         }  
  665.     }  
  666.     public static JComboBox<String> comboBoxSelectedHandle(JComboBox<String> comboBox,final JTextField ipTextField){  
  667.         if(ValueWidget.isNullOrEmpty(comboBox)){  
  668.             comboBox = new JComboBox<String>();  
  669.         }  
  670.         comboBox.addMouseListener(new MouseAdapter() {  
  671.             @Override  
  672.             public void mouseClicked(MouseEvent e) {  
  673.                 JComboBox<String> target=(JComboBox<String>)e.getSource();  
  674.                 String  selectedPort=(String)target.getSelectedItem();  
  675.                 if(!ValueWidget.isNullOrEmpty(selectedPort)){  
  676.                     ipTextField.setText(selectedPort);  
  677.                 }  
  678. //              System.out.println(e.getSource());  
  679.             }  
  680.         });  
  681.         comboBox.addItemListener(new ItemListener() {  
  682.             public void itemStateChanged(ItemEvent e) {  
  683.                 JComboBox<String> target=(JComboBox<String>)e.getSource();  
  684.                 String  selectedPort=(String)target.getSelectedItem();  
  685.                 if(!ValueWidget.isNullOrEmpty(selectedPort)){  
  686.                     ipTextField.setText(selectedPort);  
  687.                 }  
  688.             }  
  689.         });  
  690.         return comboBox;  
  691.     }  
  692.       
  693.     /*** 
  694.      *  
  695.      * @param tc 
  696.      */  
  697.     public static void trim(JTextComponent tc){  
  698.         String text=tc.getText();  
  699.         if(text!=null && text.length()>0){  
  700.             tc.setText(text.trim());  
  701.         }  
  702.     }  
  703. }  

 

 

 

相关文章
|
6天前
|
存储 Java 关系型数据库
农产品管理系统【GUI/Swing+MySQL】(Java课设)
农产品管理系统【GUI/Swing+MySQL】(Java课设)
20 1
|
6天前
|
存储 Java 关系型数据库
个人成绩信息管理系统【GUI/Swing+MySQL】(Java课设)
个人成绩信息管理系统【GUI/Swing+MySQL】(Java课设)
22 0
|
6天前
|
存储 Java 关系型数据库
酒店管理系统【GUI/Swing+MySQL】(Java课设)
酒店管理系统【GUI/Swing+MySQL】(Java课设)
25 1
|
6天前
|
存储 Java 关系型数据库
社区医院管理服务系统【GUI/Swing+MySQL】(Java课设)
社区医院管理服务系统【GUI/Swing+MySQL】(Java课设)
31 1
|
6天前
|
存储 Java 关系型数据库
仓库管理系统【GUI/Swing+MySQL】(Java课设)
仓库管理系统【GUI/Swing+MySQL】(Java课设)
22 0
|
6天前
|
存储 Java 关系型数据库
游乐场管理系统【GUI/Swing+MySQL】(Java课设)
游乐场管理系统【GUI/Swing+MySQL】(Java课设)
19 0
|
6天前
|
存储 Java 关系型数据库
影碟出租管理系统【GUI/Swing+MySQL】(Java课设)
影碟出租管理系统【GUI/Swing+MySQL】(Java课设)
19 0
|
6天前
|
存储 Java 关系型数据库
实验室设备管理系统【GUI/Swing+MySQL】(Java课设)
实验室设备管理系统【GUI/Swing+MySQL】(Java课设)
21 0
|
6天前
|
存储 Java 关系型数据库
请销假管理系统【GUI/Swing+MySQL】(Java课设)
请销假管理系统【GUI/Swing+MySQL】(Java课设)
20 0
|
6天前
|
Java 关系型数据库 MySQL
基于swing的java物业管理系统
基于swing的java物业管理系统
23 5