Show Files In Jlist In Java Swing From Specific Path In NetBeans

If you want to make project related with files and want to implement your project with files’ list procedure, you can use jlist. In java, jlist play a very important role for exploring the list of files, database data, dynamic data and static data. Here SKOTechLearn will describe how can we Show Files in jList  in Java Swing from specific path in NetBeans?

First Learn How to Show or Add Data or Items in jList in Java Swing?


So, what will we do for it? There is following steps to Learn it.

Step1:
 Drag jList from “Palette” and find “Swing Contol” and Select “List” control and put it on your Java Swing form and change it’s variable name as “MyFiles_Lst”.

Step2:
 After that you can add jButton with change variable nameBrowseBtn” and Text “Browse..” . And add another jButton with variable nameShowFileBtn” and Text “Show Files”. And add jLabel with variable nameMyPath_lbl”. And Last and one more jLabel with change variable name “MyTotalFiles_Lbl”.
Swing Controls Variable Name Use
jList MyFiles_Lst To show files from specific selected location
jButton BrowseBtn To browse path for and set specific location
jButton ShowFileBtn This will show the files in jList
jLabel MyPath_lbl Get path which was selected from “BrowseBtn”.
jLabel MyTotalFiles_Lbl Get the total number of items from “MyFiles_Lst” control.
Now the following image shows these controls with arrow indication.
Swing Controls with Variable Name in java
After adjusting controls according to desire just as mention above image. Now come to the coding point, what we should do for showing files in jList from specific path in java? The answer is described in below steps.

Example to Show Files in jList in Java


Step3:
 First we import some classes that helps for proceeding above process. The Imported class listed as following:
 import java.awt.event.KeyEvent;
 import java.io.File;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import javax.swing.DefaultListModel;
 import javax.swing.JFileChooser;

Also Learn Types of Messages using jOptionPane in Java Swing.


Step4:
 Write code for browsing files’ path from ‘ActionPerformed’ event of “BrowseBtn” .

private void BrowseBtnActionPerformed(java.awt.event.ActionEvent evt) { 
        JFileChooser myfileChooser = new JFileChooser();
        // Use JFileChooser for Open Folder Dialog Box.
        if (Files.isDirectory(Paths.get(MyPath_lbl.getText()))) {
            myfileChooser.setCurrentDirectory(new File(MyPath_lbl.getText()));
        }
        int findresult = myfileChooser.showOpenDialog(this);
        if (findresult == myfileChooser.APPROVE_OPTION) {
            // If We Select any file and click on "Open" button. The specified path will be show in "MyPath_lbl" jLabel
            MyPath_lbl.setText(myfileChooser.getCurrentDirectory().getAbsolutePath());
        }
    } 

When you click on “BrowseBtn”, a Dailog box “Open” will appear. Now, select then path and select any one of listed file.

Note: In “Open” dialog box, you have to select at least one file for defining the path. Then Click on “Open” Button.

Secret to Design jRadioButton in Java Swing with Stylish Look.


Step5:
 Now SKOTechLearn will write code for listing all files in jList. So, we will choose “ShowFileBtn” jButton and go to its “ActionPerformed” event and will write following code.
private void ShowFileBtnActionPerformed(java.awt.event.ActionEvent evt) {
        String mypath = MyPath_lbl.getText();
        MyFiles_Lst.removeAll();
        String myfiles = null;
        File myfolder = new File(mypath);
        File[] MylistOfFiles = myfolder.listFiles();
        // 'MylistOfFiles' stores all files' list on array
        DefaultListModel MyJlistModel = new DefaultListModel();
        for (int i = 0; i < MylistOfFiles.length; i++) {
            //Loop 'MylistOfFiles' to get the files in jlist
            if (MylistOfFiles[i].isFile()) {
                myfiles = MylistOfFiles[i].getName();
                MyJlistModel.addElement(myfiles);
                    // addElemet will add file's name in jList
                          }
        }
        MyFiles_Lst.setModel(MyJlistModel);
        // "MyFiles_Lst.getModel().getSize()" - indicate the total number of files in jList
        MyTotalFiles_Lbl.setText(String.valueOf(MyFiles_Lst.getModel().getSize()));
    }

Now This Action Performed will show the Files in jList.

SKOTechLearn suggest you to please proceed step by step, if you want to get proper result on your project in swing java.

Learn How to Change Look or Design jLabel in Java Swing with Example.


Now we will understand by the image indication.
Process to show file in jList in java
Process to show file in jList
As you can see, first we click on “Browse..” button. Then, a “Open” dialog will appear, just input or select your files’ path and then select any file from it.

And press “Open” button. After that the file’s path as string will show in “Files’ Path” jLabel.

Go to “Show Files” button and press it. This process will show the files in jList. And the total number of items will show in “Total Files:” jLabel.

Hidden Tips to Open jFrame from Another Jframe's Controls in Java


There is the easiest way to guide you how you can Show Files in details of jList in java in swing java from Specific path in Netbeans with SKOTechLearn.

0 comments: