Tuesday, October 28, 2014

Java SpringLayout example


        // ==================================================================
        // Config layout for runPanel
        // ==================================================================
        //               ----------------------------> X
        //              | (0,0)
        //              |
        //              |
        //              \/ Y


        // 1. Add components into runPanel
        runPanel.add(iatCheckBoxPanel);
        runPanel.add(runButton);
        runPanel.add(parButton);

        SpringLayout runPanelLayout = new SpringLayout();
        runPanel.setLayout(runPanelLayout);

        int spacer = 5;
        int xOrig = 110;
        int xWidth = 150;
        int yHeight = 30;

        // 2. Use constraint to position each component within panel
        //  All these positions starting from (0,) are RELATIVE to this panel LOCALLY.
        SpringLayout.Constraints iatCheckBoxPanelCons = runPanelLayout
                .getConstraints(iatCheckBoxPanel);
        iatCheckBoxPanelCons.setX(Spring.constant(0));
        iatCheckBoxPanelCons.setY(Spring.constant(0));
        iatCheckBoxPanelCons.setWidth(Spring.constant(100));
        iatCheckBoxPanelCons.setHeight(Spring.constant(100));

        SpringLayout.Constraints runButtonCons = runPanelLayout
                .getConstraints(runButton);
        runButtonCons.setX(Spring.constant(xOrig));
        runButtonCons.setY(Spring.constant(70));
        runButtonCons.setWidth(Spring.constant(xWidth));
        runButtonCons.setHeight(Spring.constant(yHeight));

        SpringLayout.Constraints parButtonCons = runPanelLayout
                .getConstraints(parButton);
        parButtonCons.setX(Spring.constant(xOrig + xWidth + spacer));
        parButtonCons.setY(Spring.constant(70));
        parButtonCons.setWidth(Spring.constant(xWidth));
        parButtonCons.setHeight(Spring.constant(yHeight));
        // ==================================================================
        // End of runPanel
        // ==================================================================

No comments:

Post a Comment