Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseWheelListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class Main extends Box {
    JPanel glassPane = new JPanel() {
        public void paintComponent(Graphics g) {
            g.setColor(new Color(0, 0, 0, 80));
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.white);
            g.setFont(g.getFont().deriveFont(18f).deriveFont(Font.BOLD));
            g.drawString("Processing...", getWidth() - 100, getHeight() - 10);
        }
    };

    public Main() {
        super(BoxLayout.Y_AXIS);
        glassPane.setOpaque(false);
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        for (int i = 0; i < 14000; i++) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root" + i);
            node.add(new DefaultMutableTreeNode("Child" + i));
            root.add(node);
        }

        final JTree tree = new JTree(root);
        tree.setRootVisible(false);
        final JScrollPane pane = new JScrollPane(tree);
        add(pane);

        JButton button = new JButton("Expand");
        button.addActionListener(e -> {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    TreePath[] paths = tree.getSelectionPaths();
                    if (paths == null || paths.length == 0) {
                        glassPane.setVisible(false);
                        return;
                    }
                    tree.setSelectionPath(paths[0]);

                    for (int i = 0; i < paths.length; i++) {
                        tree.expandPath(paths[i]);
                    }
                    glassPane.setVisible(false);
                }
            });
            getRootPane().setGlassPane(glassPane);
            glassPane.setVisible(true);
            t.start();
        });
        add(button);
        glassPane.addMouseWheelListener(e -> {
            for (MouseWheelListener mwl : pane.getMouseWheelListeners()) {
                mwl.mouseWheelMoved(e);
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Main());
        frame.pack();
        frame.setVisible(true);
    }

}