Tuesday, 25 March 2014

Dockable Frame in Java Swing

Make a Dockable Frame in Java Swing.

---------------------------------------------------------------------------------------------

import com.vlsolutions.swing.docking.*;
import javax.swing.*;
import java.awt.*;
/*
 * @author Prabhat
 */
public class DockableFrame extends JFrame {
// our docking desktop
    DockingDesktop desk = new DockingDesktop();

    public DockableFrame() {
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        getContentPane().add(desk);
        setTitle("Chcek Dock Frame");
// first, create 4 dockable buttons (inner class below)
        DockableButton button1 = new DockableButton("Dockable 1");
        DockableButton button2 = new DockableButton("Dockable 2");
        DockableButton button3 = new DockableButton("Dockable 3");
        DockableButton button4 = new DockableButton("Dockable 4");
// first button at center
        desk.addDockable(button1);
// second and third button splitted vertically on the left of center
// first parameter is the "reference", second is the added dockable
        desk.split(button1, button2, DockingConstants.SPLIT_LEFT);
        desk.split(button2, button3, DockingConstants.SPLIT_BOTTOM);
// add the fourth button as a tab on the 3rd :
        desk.createTab(button3, button4, 1);
        SwingUtilities.invokeLater(
                new Runnable() {

                    public void run() {
                        setSize(800, 600);
                        validate();
                        setVisible(true); // realize on the event dispatch thread
                    }
                });
    }

    public static void main(String[] args) {
        new DockableFrame();
    }
// our basic dockable object
    class DockableButton extends JButton implements Dockable {

        DockKey key;

        public DockableButton(String label) {
            super(label);
            this.key = new DockKey(label); // most simple dockkey constructor
            setPreferredSize(new Dimension(200, 200));
        }

        public DockKey getDockKey() {
            return key;
        }

        public Component getComponent() {
            return this;
        }
    }
}
------------------------------------------------------------------------------------------

Note : You need vldocking.jar file to execute this program.


No comments:

Post a Comment