Java language implementation of simple FTP software --> Implementation of FTP software local Windows (V)

Source: Internet
Author: User
Tags ftp connection

1. First, let's take a look at the layout of the local window.

2. Check the code framework implemented in the local window.

2. Implementation Code of the local window localpanel. Java

Package COM. oyp. FTP. panel. local; import Java. AWT. color; import Java. AWT. desktop; import Java. AWT. dimension; import Java. AWT. event. itemevent; import Java. io. file; import Java. io. ioexception; import Java. util. date; import Java. util. using list; import Java. util. queue; import Java. util. logging. level; import Java. util. logging. logger; import javax. swing. actionmap; import javax. swing. defaultcomboboxmodel; import javax. Swing. jbutton; import javax. swing. joptionpane; import javax. swing. swingutilities; import javax. swing. table. defaulttablemodel; import javax. swing. table. tablemodel; import javax. swing. table. tablerowsorter; import javax. swing. table. tablestringconverter; import COM. oyp. FTP. ftpclientframe; import COM. oyp. FTP. panel. ftptablecellranderer; import COM. oyp. FTP. panel. FTP. tableconverter; import COM. oyp. FTP. utils. diskfil E; public class localpanel extends javax. swing. jpanel {queue <object []> queue = new queue list <object []> (); Private uploadthread = NULL; private desktop = NULL; private javax. swing. jbutton createfolderbutton; private javax. swing. jbutton delbutton; private javax. swing. jscrollpane scrollpane; private javax. swing. jtoolbar. separator jseparator1; private javax. swing. jtoolbar toolbar; private Javax. swing. jcombobox localdiskcombobox; javax. swing. jtable localdisktable; javax. swing. jlabel localselfilepathlabel; private javax. swing. jbutton renamebutton; private javax. swing. jbutton uploadbutton; private tablerowsorter <tablemodel> sorter; ftpclientframe frame = NULL; Public localpanel () {initcomponents ();} public localpanel (ftpclientframe client_frame) {frame = client_frame; If (desktop. isdesktopsup Ported () {desktop = desktop. getdesktop () ;}initcomponents () ;}/ *** interface layout and Initialization Method */private void initcomponents () {actionmap = getactionmap (); actionmap. put ("delaction", new delfileaction (this, "delete", null); actionmap. put ("renameaction", new rennameaction (this, "RENAME", null); actionmap. put ("createfolderaction", new createfolderaction (this, "new folder", null); actionmap. put ("uploadaction", new upload Action (this, "Upload", null); actionmap. put ("refreshaction", new refreshaction (this, "refresh", null); Java. AWT. gridbagconstraints; toolbar = new javax. swing. jtoolbar (); delbutton = new javax. swing. jbutton (); renamebutton = new javax. swing. jbutton (); createfolderbutton = new javax. swing. jbutton (); uploadbutton = new javax. swing. jbutton (); jseparator1 = new javax. swing. jtoolbar. separator (); loca Ldiskcombobox = new javax. swing. jcombobox (); localdiskcombobox. setpreferredsize (new dimension (100, 25); scrollpane = new javax. swing. jscrollpane (); localdisktable = new javax. swing. jtable (); localdisktable. setdragenabled (true); localselfilepathlabel = new javax. swing. jlabel ();/*** Add a title to the existing border so that it has the specified position, default font, and text color (determined by the current appearance ). * Titledborder. Center: place the title text in the center of the border line. * Titledborder. above_top: place the title above the top line of the border. */Setborder (javax. swing. borderfactory. createtitledborder (null, "local", javax. swing. border. titledborder. center, javax. swing. border. titledborder. above_top); setlayout (New Java. AWT. gridbaglayout (); toolbar. setrolover (true); toolbar. setfloatable (false); delbutton. settext ("delete"); delbutton. setfocusable (false); delbutton. setaction (actionmap. get ("delaction"); toolbar. add (delbutton); renamebutton. settext ("RENAME"); Re Namebutton. setfocusable (false); renamebutton. setaction (actionmap. get ("renameaction"); toolbar. add (renamebutton); createfolderbutton. settext ("new folder"); createfolderbutton. setfocusable (false); createfolderbutton. setaction (actionmap. get ("createfolderaction"); toolbar. add (createfolderbutton); uploadbutton. settext ("Upload"); uploadbutton. setfocusable (false); uploadbutton. setaction (actionmap. get ("uploadaction"); too Lbar. add (uploadbutton); jbutton refreshbutton = new jbutton (); refreshbutton. settext ("refresh"); refreshbutton. setfocusable (false); refreshbutton. setaction (actionmap. get ("refreshaction"); toolbar. add (refreshbutton); toolbar. add (jseparator1); // file. listroots (): List available file system root. Localdiskcombobox. setmodel (New defaultcomboboxmodel (file. listroots (); localdiskcombobox. additemlistener (New Java. AWT. event. itemlistener () {public void itemstatechanged (Java. AWT. event. itemevent EVT) {localdiskcomboboxitemstatechanged (EVT) ;}}); toolbar. add (localdiskcombobox); gridbagconstraints = new Java. AWT. gridbagconstraints (); gridbagconstraints. gridx = 0; gridbagconstraints. gridy = 1; gridbagconstr Aints. fill = Java. AWT. gridbagconstraints. horizontal; gridbagconstraints. weightx = 1.0; add (toolbar, gridbagconstraints); localdisktable. setmodel (New localtablemodel (); localdisktable. setshowhorizontallines (false); localdisktable. setshowverticallines (false); localdisktable. gettableheader (). setreorderingallowed (false); localdisktable. addmouselistener (New Java. AWT. event. mouseadapter () {public void mousecli Cked (Java. AWT. event. mouseevent EVT) {localdisktablemouseclicked (EVT) ;}}); scrollpane. setviewportview (localdisktable); scrollpane. getviewport (). setbackground (color. white); // sets the Renderer localdisktable for rendering the local resource and FTP resource table components. getcolumnmodel (). getcolumn (0 ). setcellrenderer (ftptablecellranderer. getcellranderer (); // an implementation of rowsorter, which uses tablemodel to provide sorting and filtering operations. Sorter = new tablerowsorter <tablemodel> (localdisktable. GetModel (); tablestringconverter converter = new tableconverter (); // you can specify an object that converts a value from a model to a string. Sorter. setstringconverter (converter); // you can specify rowsorter. Rowsorter is used to sort and filter jtables. Localdisktable. setrowsorter (sorter); sorter. togglesortorder (0); gridbagconstraints = new Java. AWT. gridbagconstraints (); gridbagconstraints. gridx = 0; gridbagconstraints. gridy = 2; gridbagconstraints. fill = Java. AWT. gridbagconstraints. both; gridbagconstraints. weightx= 1.0; gridbagconstraints. weighty = 1.0; add (scrollpane, gridbagconstraints); localselfilepathlabel. setborder (javax. swing. borderfactory. create Etchedborder (); gridbagconstraints = new Java. AWT. gridbagconstraints (); gridbagconstraints. gridx = 0; gridbagconstraints. gridy = 3; gridbagconstraints. fill = Java. AWT. gridbagconstraints. horizontal; add (localselfilepathlabel, gridbagconstraints);}/*** option of the local disk drop-down selection box changes the event processing method, called by the event listener */private void localdiskcomboboxitemstatechanged (Java. AWT. event. itemevent EVT) {If (EVT. getstatechange () = itemevent. sele CTED) {object item = EVT. getitem (); // obtain the option if (item instanceof file) for the selected drop-down list {// If this option is a file class instance object file seldisk = (File) item; // convert this option to a file class // call the listlocalfiles () method to display the list of Disk Files specified by the file class listlocalfiles (seldisk );}}} /*** Method for refreshing the specified folder */void refreshfolder (File file) {listlocalfiles (File );} /*** Method for refreshing the current local folder */Public void refreshcurrentfolder () {final file = getcurrentfolder (); // get the runnable runna of the current folder Ble = new runnable () {// create a new thread public void run () {listlocalfiles (File); // reload the list of the current folder to the table }}; // cause the runnable run method to be called on the designated thread of eventqueue. Swingutilities. invokelater (runnable); // call this thread object in the event thread}/*** get the current folder */Public file getcurrentfolder () {// use the path label to create the current folder object file = new file (localselfilepathlabel. gettext (); // If a folder is selected for the table, or the selected file has a real parent folder if (localdisktable. getselectedrow ()> 1 & file. getparentfile ()! = NULL) file = file. getparentfile (); // get the return file of the parent folder; // return the folder object}/*** click the local disk file table and double-click the event handling method */private void localdisktablemouseclicked (Java. AWT. event. mouseevent EVT) {int selectedrow = localdisktable. getselectedrow (); // get the row number of the selected table if (selectedrow <0) return; // get the value of the first field of the selected row in the table object value = localdisktable. getvalueat (selectedrow, 0); If (value instanceof diskfile) {// if the value is diskfile's instance object diskfi Le selfile = (diskfile) value; // sets the local file path localselfilepathlabel in the status bar. settext (selfile. getabsolutepath (); If (EVT. getclickcount ()> = 2) {// if you double-click the mouse if (selfile. isdirectory () {// and select the folder listlocalfiles (selfile); // display the folder's content list} else if (desktop! = NULL) {// if it is not a folder try {desktop. open (selfile); // associate the local program to open the file} catch (ioexception ex) {logger. getlogger (ftpclientframe. class. getname ()). log (level. severe, null, ex) ;}}} else {// If the selected table content is not an instance of the diskfile class, // you can check whether the selected table content is .. option if (EVT. getclickcount ()> = 2 & value. equals (".. ") {// create the temporary file tempfile = new file (localselfilepathlabel. gettext (); // display the list of upper-level directories of the selected file listlocalfiles (tempfile. getparentfi Le () ;}}/ *** Method for reading local files to a table */private void listlocalfiles (File seldisk) {If (seldisk = NULL | seldisk. isfile () {return;} localselfilepathlabel. settext (seldisk. getabsolutepath (); file [] listfiles = seldisk. listfiles (); // obtain the disk file list // obtain the table data model defaulttablemodel model = (defaulttablemodel) localdisktable. getModel (); Model. setrowcount (0); // clear the model content model. addrow (new object [] {". "," <dir> "," "}); // create Create. option Model. addrow (new object [] {"... "," <dir> "," "}); // create .. option if (listfiles = NULL) {joptionpane. showmessagedialog (this, "this disk cannot be accessed"); return;} // traverse the content of the disk root folder and add it to the table for (File file: listfiles) {file diskfile = new diskfile (File); // create a file object String Length = file. length () + "B"; // get the file size if (file. length ()> 1000*1000*1000) {// calculates the file g unit length = file. length ()/1000000000 + "G";} If (file. length ()> 1000*1000) {// calculates the file M unit length = file. length ()/1000000 + "M";} If (file. length () & gt; 1000) {length = file. length ()/1000 + "K"; // calculates the K Unit of the file} If (file. isdirectory () {// display the folder flag length = "<dir>" ;}// obtain the last modification date of the file string modifdate = new date (file. lastmodified ()). tolocalestring (); If (! File. canread () {length = "unknown"; modifdate = "unknown" ;}// add the information of a single file to the data model of the table. addrow (new object [] {diskfile, length, modifdate});} localdisktable. clearselection (); // cancel the table selection}/*** Method for stopping the File Upload thread */Public void stopuploadthread () {If (uploadthread! = NULL) uploadthread. stopthread ();} public javax. Swing. jcombobox getlocaldiskcombobox () {return localdiskcombobox;}/*** sets the FTP connection and starts the upload queue thread method. */Public void setftpclient (string server, int port, string userstr, string passstr) {If (uploadthread! = NULL) uploadthread. stopthread (); uploadthread = new uploadthread (this, server, port, userstr, passstr); uploadthread. start () ;}public queue <object []> getqueue () {return queue ;}}

========================================================== ========================================================== ============================

Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng/article/details/9673081

========================================================== ========================================================== ==============================

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.