Displays all files under the specified directory in the list box (JList) component,
Following
Importjava.awt.BorderLayout;ImportJava.io.File;ImportJavax.swing.JComboBox;ImportJavax.swing.JFrame;Importjavax.swing.JList;ImportJavax.swing.JScrollPane; Public classKuang { Public Static voidListdrectory (File dir)throwsillegalaccessexception {JFrame frm=NewJFrame ("Drop down combo box"); Frm.setdefaultcloseoperation (Jframe.exit_on_close); Frm.setlayout (NewBorderLayout ()); String[] Likes={"All Formats", ". jpg", ". WPs", ". Doc", ". ppt", ". xls"}; JComboBox JB1=NewJComboBox (likes); Frm.add (JB1, Borderlayout.north); Frm.setbounds (550,190,260,360); Frm.setvisible (true); //determine if the file (directory) is being passed in if(!dir.exists ()) { Throw NewIllegalaccessexception ("directory" + dir + "does not exist"); } //determine if the pass in is a directory if(!dir.isdirectory ()) { Throw NewIllegalaccessexception (dir + "not directory"); } //use the list () method in the file class to list all the file (directory) names in the current directorystring[] FileNames =dir.list (); JList L1=NewJList (fileNames); JScrollPane SP=NewJScrollPane (L1);//Put the jlist in the scroll bar and put the scroll bar in the Layout centerFrm.add (sp,borderlayout.center);//For (String s:filenames) {//System.out.println (dir + "\ \" +s);//output The full file directory name// }// //lists all files (folders) under the specified directory (including its subdirectories)//file[] fs = Dir.listfiles ();//For (File file:fs) {//if (file.isdirectory ()) {// //Recursive//listdrectory (file);//}else {//System.out.println (dir + "\ \" +file); //string[] fileNames = Dir.list ();//JList JL =new JList (fileNames);//Jl.setforeground (New Color (140,171,226));//Jl.setbackground (New Color (0,0,0));//Jl.setselectionbackground (New Color (87,49,134));//Jl.setselectionforeground (New Color (140,171,226));//JScrollPane sp=new JScrollPane (JL);//Frm.add (sp,borderlayout.center); } /** * @paramargs*/ Public Static voidMain (String[]args) {//TODO auto-generated Method Stub Try{kuang.listdrectory (NewFile ("C:\\users\\think\\desktop\\1")); } Catch(illegalaccessexception e) {//Todo:handle ExceptionE.printstacktrace (); } }}
Week five Java Study notes (v)