Implementing file explorer GUI like one of VS Code. In java.
by xg3571 from LinuxQuestions.org on (#5STN1)
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
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