mercredi 6 janvier 2021

Aligning checkBoxes in different HBoxes

I created a scene that has multiple check boxes. It looks okay, but I don't like how the check boxes look disorganized. I would rather have it so the all of them line up in a column. Similar to this, but I want all the columns line up instead of the first one. I figured this was happening because the check box's label were all different lengths, so I decided to make two methods. One making all weapon check box's label 11 characters (since that is the longest string) and the other padding the check box's label to 12 characters. However, it didn't seem to make a difference.

Here is the code that causes the last image:

package sample;


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application
{
    //weapon checkBoxes
    private static CheckBox broadswordCheckBox = new CheckBox(padWeaponString("Broadsword"));
    private static CheckBox caberCheckBox = new CheckBox(padWeaponString("Caber"));
    private static CheckBox nastyKnifeCheckBox = new CheckBox(padWeaponString("Nasty Knife"));
    private static CheckBox longbowCheckBox = new CheckBox(padWeaponString("Longbow"));
    private static CheckBox magicOrbCheckBox = new CheckBox(padWeaponString("Magic Orb"));
    private static CheckBox grimoireCheckBox = new CheckBox(padWeaponString("Grimoire"));

    //HBoxes to hold the weapon checkBoxes
    private static HBox weapon1HBox = setObjectHBox(broadswordCheckBox, caberCheckBox, nastyKnifeCheckBox);
    private static HBox weapon2HBox = setObjectHBox(longbowCheckBox, magicOrbCheckBox, grimoireCheckBox);

    //item checkBoxes
    private static CheckBox balloonCheckBox = new CheckBox(padItemString("Balloon"));
    private static CheckBox batteryCheckBox = new CheckBox(padItemString("Battery"));
    private static CheckBox bellowsCheckBox = new CheckBox(padItemString("Bellows"));
    private static CheckBox cheatCodeCheckBox = new CheckBox(padItemString("Cheat Code"));
    private static CheckBox crystalBallCheckBox = new CheckBox(padItemString("Crystal Ball"));
    private static CheckBox featherCheckBox = new CheckBox(padItemString("Feather"));
    private static CheckBox hardDriveCheckBox = new CheckBox(padItemString("Hard Drive"));
    private static CheckBox lampCheckBox = new CheckBox(padItemString("Lamp"));
    private static CheckBox moonstoneCheckBox = new CheckBox(padItemString("Moonstone"));
    private static CheckBox potionCheckBox = new CheckBox(padItemString("Potion"));
    private static CheckBox smallDogCheckBox = new CheckBox(padItemString("Small Dog"));
    private static CheckBox stepladderCheckBox = new CheckBox(padItemString("Stepladder"));
    private static CheckBox sunstoneCheckBox = new CheckBox(padItemString("Sunstone"));
    private static CheckBox symbolCheckBox = new CheckBox(padItemString("Symbol"));
    private static CheckBox ticketCheckBox = new CheckBox(padItemString("Ticket"));
    private static CheckBox trophyCheckBox = new CheckBox(padItemString("Trophy"));

    //HBoxes to hold the item checkBoxes
    private static HBox item1HBox = setObjectHBox(balloonCheckBox, batteryCheckBox, bellowsCheckBox);
    private static HBox item2HBox = setObjectHBox(cheatCodeCheckBox, crystalBallCheckBox, featherCheckBox);
    private static HBox item3HBox = setObjectHBox(hardDriveCheckBox, lampCheckBox, moonstoneCheckBox);
    private static HBox item4HBox = setObjectHBox(potionCheckBox, smallDogCheckBox, stepladderCheckBox);
    private static HBox item5HBox = setObjectHBox(sunstoneCheckBox, symbolCheckBox, ticketCheckBox);

    @Override
    public void start(Stage primaryStage)
    {
        VBox vBox = new VBox(new Label("Weapons:"), weapon1HBox, weapon2HBox, new Label("Items:"), item1HBox,
                              item2HBox, item3HBox, item4HBox, item5HBox, trophyCheckBox);
        vBox.setSpacing(10);
        vBox.setAlignment(Pos.CENTER);
        vBox.setPadding(new Insets(10));

        primaryStage.setScene(new Scene(vBox));
        primaryStage.show();
    }

    //a method to add 3 checkBoxes to an HBox and returns it
    private static HBox setObjectHBox(CheckBox checkBox1, CheckBox checkBox2, CheckBox checkBox3) {
        HBox hBox = new HBox(checkBox1, checkBox2, checkBox3);
        hBox.setAlignment(Pos.CENTER);
        hBox.setSpacing(10);

        return hBox;
    }

    private static String padWeaponString(String weaponString)
    {
        while (weaponString.length() < 11)
        {
            weaponString += " ";
        }

        return weaponString;
    }

    //a method that will pad a string to the length of 12 with spaces
    private static String padItemString(String itemString)
    {
        while (itemString.length() < 12)
        {
            itemString += " ";
        }


        return itemString;
    }



    public static void main(String[] args) {
        launch(args);
    }
}




Aucun commentaire:

Enregistrer un commentaire