Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8214977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:38:15+00:00 2026-06-07T11:38:15+00:00

I am using FTP4j and I want to populate a JTree with the root

  • 0

I am using FTP4j and I want to populate a JTree with the root directory of an FTP server. I’ve tried using FTP4j’s currentDirectory() method, but that only returns a “/” which isn’t useful. I’ve also tried passing the ftp:// url to a method that initializes the JTree, which doesn’t work either. This is my first Swing program so I’m kind of stumped on where to go. Here is the code:

package net.emptybox.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import net.miginfocom.swing.MigLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JSplitPane;
import javax.swing.JSeparator;
import javax.swing.JTree;
import javax.swing.JTextArea;
import java.awt.Component;
import java.io.File;

import net.emptybox.ui.FTP;

import javax.swing.Box;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;



public class GUI {

    static JFrame frame;
    static private JSplitPane splitPane;
    static private JLabel lblServer;
    static private JTextField serverField;
    static private JLabel lblPort;
    static private JTextField portField;
    static private JLabel lblUsername;
    static private JTextField usernameField;
    static private JLabel lblPassword;
    static private JTextField passwordField;
    static private JButton connectButton;
    static private JSeparator separator;
    static private JTextArea detailArea;
    static private JButton downloadButton;
    static private JButton uploadButton;
    static private Component horizontalGlue;
    static private JTextField fileField;
    static private JButton goButton;
    static private Component horizontalGlue_1;

    static FileSystemModel fileSystemModel;
    static JLabel consoleLabel;
    private static Component verticalGlue;
    private static JScrollPane scrollPane;
    static JTree fileTree;

    /**
     * Launch the application.
     */

    /**
     * Create the application.
     */
    public GUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    public static void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 648, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new MigLayout("", "[grow]", "[][][][grow][]"));

        lblServer = new JLabel("Server:");
        frame.getContentPane().add(lblServer, "flowx,cell 0 0");

        consoleLabel = new JLabel("");
        frame.getContentPane().add(consoleLabel, "flowx,cell 0 1");

        separator = new JSeparator();
        frame.getContentPane().add(separator, "cell 0 2");

        splitPane = new JSplitPane();
        splitPane.setOneTouchExpandable(true);
        splitPane.setContinuousLayout(true);
        frame.getContentPane().add(splitPane, "cell 0 3,grow");

        detailArea = new JTextArea();
        detailArea.setEditable(false);
        splitPane.setRightComponent(detailArea);

        scrollPane = new JScrollPane();
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        splitPane.setLeftComponent(scrollPane);

        serverField = new JTextField();
        frame.getContentPane().add(serverField, "cell 0 0,growx");

        lblPort = new JLabel("Port:");
        frame.getContentPane().add(lblPort, "cell 0 0");

        portField = new JTextField();
        frame.getContentPane().add(portField, "cell 0 0,growx");

        lblUsername = new JLabel("Username:");
        frame.getContentPane().add(lblUsername, "cell 0 0");

        usernameField = new JTextField();
        frame.getContentPane().add(usernameField, "cell 0 0,growx");

        lblPassword = new JLabel("Password:");
        frame.getContentPane().add(lblPassword, "cell 0 0");

        passwordField = new JTextField();
        frame.getContentPane().add(passwordField, "cell 0 0,growx");


        connectButton = new JButton("Connect");
        frame.getContentPane().add(connectButton, "cell 0 0");

        if (serverField.getText() == null || usernameField.getText() == null || passwordField.getText() == null) {
            connectButton.disable();
        } else {
            connectButton.enable();
        }

        if (portField.getText() == null) {
            portField.setText("21");
        }

        connectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                FTP.connect(serverField.getText(), portField.getText(), usernameField.getText(), passwordField.getText());
            }
        });

        downloadButton = new JButton("Download");
        frame.getContentPane().add(downloadButton, "flowx,cell 0 4");
        downloadButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });

        uploadButton = new JButton("Upload");
        frame.getContentPane().add(uploadButton, "cell 0 4");
        uploadButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });

        horizontalGlue_1 = Box.createHorizontalGlue();
        frame.getContentPane().add(horizontalGlue_1, "cell 0 4,growx");

        fileField = new JTextField();
        frame.getContentPane().add(fileField, "cell 0 4");
        fileField.setColumns(200);

        goButton = new JButton("Go");
        goButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        frame.getContentPane().add(goButton, "cell 0 4");

        horizontalGlue = Box.createHorizontalGlue();
        frame.getContentPane().add(horizontalGlue, "cell 0 4,alignx leading");

        verticalGlue = Box.createVerticalGlue();
        frame.getContentPane().add(verticalGlue, "cell 0 1");
    }

    private String getFileDetails(File file) {
        if (file == null)
          return "";
        StringBuffer buffer = new StringBuffer();
        buffer.append("Name: " + file.getName() + "\n");
        buffer.append("Path: " + file.getPath() + "\n");
        buffer.append("Size: " + file.length() + "\n");
        return buffer.toString();
      }

    public static void populateTree(String directory) {
        fileSystemModel = new FileSystemModel(new File(directory));

        fileTree = new JTree(fileSystemModel);
        scrollPane.setViewportView(fileTree);
    }
}

Populate tree is called by another class when a connection is successfully established with the server and the user has logged in.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:38:17+00:00Added an answer on June 7, 2026 at 11:38 am

    You’re going to want to create your own TreeModel.

    Once the user has connected successfully, you’ll need to query the site for more information, there are no models available in the default implementation that will do what you want, you’re going have to work for it 😉

    Let’s start with the FTP side.

    String current = ftpSite.getCurrentDirectory(); // Just in case you want to come back
    ftpSite.changeDirectort("/"); // Move to the root directory
    FPTFile[] fileList = ftpSite.list(); // Get the list of files.
    

    Now, how you go about this comes down to you. But basically, I would construct a MutableTreeNode as the root node (for the JTree).

    From there you can add new (MutableTreeNode) nodes to the root as you need.

    You could check out http://www.jroller.com/Thierry/entry/swing_lazy_loading_in_a for some more ideas on how to achieve all this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a monitoring application for a FTP server using FTP4J
Using emacs on Ubuntu 11.10. I want to connect to a SQL Server database
Using Yii, I want to delete all the rows that are not from today.
im using ftp4j to connect and download a file via ftp The following works
Using Server.Transfer to show a page that informs the user that the web site
Using tornado, I want to create a bit of middleware magic that ensures that
Using a populated Table Type as the source for a TSQL-Merge. I want to
Using SQL Server 2008 R2 we are looking for a way to select the
I am using eclipse. When I add an external jar (import it.sauronsoftware.ftp4j.*;) it seems
The following code executes a function that retrieves a file via ftp and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.