Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.util.Properties;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class Main extends JFrame {
    static String NOTHING_SELECTED = "<nothing selected>";
    JScrollPane treeScroll;
    JTree tree;
    DefaultTreeModel treeModel;
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("GIT commands");
    JLabel descriptionLabel;

    Properties properties = new Properties();

    public Main() {
        setLayout(new BorderLayout());
        treeModel = new DefaultTreeModel(root);
        tree = new JTree(treeModel);
        treeScroll = new JScrollPane(tree);
        add(treeScroll, BorderLayout.WEST);

        properties.setProperty("foo1", "bar1");
        properties.setProperty("foo2", "bar2");
        properties.setProperty("foo3", "bar3");
        properties.setProperty("foo4", "bar4");

        Set<Object> keySet = properties.keySet();
        for (Object key : keySet) {
            root.add(new DefaultMutableTreeNode(key));
        }

        tree.expandPath(new TreePath(root));
        descriptionLabel = new JLabel(NOTHING_SELECTED);
        add(descriptionLabel, BorderLayout.CENTER);

        tree.addTreeSelectionListener(e -> {
            DefaultMutableTreeNode selection = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (selection != null) {
                String key = (String) selection.getUserObject();
                String command = properties.getProperty(key);
                descriptionLabel.setText(command);
            } else {
                descriptionLabel.setText(NOTHING_SELECTED);
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        new Main().setVisible(true);
    }
}