LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2021, 11:31 AM   #1
xg3571
LQ Newbie
 
Registered: Dec 2021
Posts: 12

Rep: Reputation: Disabled
Implementing file explorer GUI like one of VS Code. In java.


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class BoxLayoutTest extends Frame {
    BoxLayoutTest() {
        JPanel explorer = new JPanel();
        
        explorer.setLayout(new BoxLayout(explorer, BoxLayout.Y_AXIS));

        // Label showing current path
        JLabel currentPath = new JLabel("root/java");
        currentPath.setAlignmentX( LEFT_ALIGNMENT );

        // add a folder button
        JButton addFolder = new JButton("add folder");
        addFolder.setSize(10, 5);
        addFolder.setAlignmentX(RIGHT_ALIGNMENT);

        // add a file button
        JButton addFile = new JButton("add file");
        addFile.setSize(10, 5);
        addFile.setAlignmentX(RIGHT_ALIGNMENT);

        // Labels of subdirectories
        ArrayList childrens = new ArrayList();
        childrens.add(new JLabel("java"));
        childrens.add(new JLabel("cpp"));
        childrens.add(new JLabel("python"));

        explorer.add(currentPath);
        explorer.add(addFolder);
        explorer.add(addFile);

        Object[] labels = childrens.toArray();

        for (Object l : labels) {
            JLabel label = (JLabel)l;
            label.setAlignmentX( LEFT_ALIGNMENT );
            explorer.add(label);
        }

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        add("West", explorer);
        setSize(700, 700);
        setVisible(true);
    }

    public static void main(String[] args) {
        BoxLayoutTest win = new BoxLayoutTest();
    }
}
On the top, showing current path left aligned and
following are buttons off "add folder" and "add file" right aligned
Below are labels sub directories left aligned

But when I run the code, layout become mess. none of code are run as I want like attached picture

I'll be thankful of advice
Attached Thumbnails
Click image for larger version

Name:	explorer.png
Views:	15
Size:	17.9 KB
ID:	37812   Click image for larger version

Name:	wrong_layout.png
Views:	11
Size:	8.5 KB
ID:	37813  
 
Old 12-08-2021, 12:30 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

The image showing what you're trying to replicate is sometimes called a TreeView and must involve a tree/nested structure in the data, but the code you show only has a one-dimensional array.

Also, you don't need to create this yourself - you can use existing JTree component:

https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JTree.html

https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html

 
1 members found this post helpful.
Old 12-08-2021, 06:04 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Before you charge off to "implement something" in [[Java]] ... first do a very thorough review of the (vast ...) contributed software library that is already out there. It is extremely likely these days that what you are looking for has already been very thoroughly done. Such that, if it doesn't take you all the way there, it'll get you more than ninety percent. Every programming system has one.

"Actum Ne Agas: Do Not Do A Thing Already Done."
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Implementing edge computing, DevOps like car racing, and more industry trends LXer Syndicated Linux News 0 08-27-2019 02:33 AM
Implementing a secure file sharing (NFS like) server ciphyre Linux - Server 3 11-14-2008 03:18 PM
Merge many files in to one big file. like 20 file merge in one big file Jmor Linux - Newbie 2 10-29-2008 09:41 PM
explorer-like file manager zoomzoom Linux - Software 8 06-13-2004 05:38 PM
Needing the source code implementing WFQ rickie Linux - Networking 1 07-29-2002 10:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration