Why is GUI-creation code so messy?

/mr []ing w/
i am new to writing GUI code, so some tips would be helpful. but geez, looking at this entire thing, i feel like it could be done a lot better; yet, i don’t know how. it looks like its just supposed to be messy.

Moved to Off-Topic, as this isn’t related to Minecraft.

5 Likes

Shouldn’t be. Use OO to fix your mess :). These prepare methods could be classes.

as per description of the gist

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

yay, time for extensive research in jfx documentation

if you have any reference links for examples or guides on this, would be nice :c

untested:

public class MenuBarItem extends MenuBar{
   public MenuBarItem(){
       // Head menu item
        Menu fileMenu = MenuBuilder.create().text("File").build();
        
        MenuItem fileMenuNew = MenuItemBuilder.create().text("New").build();
        MenuItem fileMenuOpen = MenuItemBuilder.create().text("Open").build();
        // separator
        SeparatorMenuItem sep1 = new SeparatorMenuItem();
        // -- submenu: import
        Menu fileMenuSubImport = MenuBuilder.create().text("Import from..").build();
        MenuItem fileMenuSubImportFile = MenuItemBuilder.create().text("Permissions file").build();
        MenuItem fileMenuSubImportPlugins = MenuItemBuilder.create().text("Sponge Plugin directory").build();
        
        // -- submenu: export
        Menu fileMenuSubExport = MenuBuilder.create().text("Export to..").build();
        MenuItem fileMenuSubExportFile = MenuItemBuilder.create().text("Permissions file").build();
        MenuItem fileMenuSubExportDefinitions = MenuItemBuilder.create().text("Node definitions").build();
        // separator
        SeparatorMenuItem sep2 = new SeparatorMenuItem();
        // menu items: saving
        MenuItem fileMenuSaveAs = MenuItemBuilder.create().text("Save as..").build();
        MenuItem fileMenuSave = MenuItemBuilder.create().text("Save").build();
        // separator
        SeparatorMenuItem sep3 = new SeparatorMenuItem();
        // menu items: exit
        MenuItem fileMenuExit = MenuItemBuilder.create().text("Exit").build();
        
        //TODO Menu item functions
        
        fileMenuSubImport.getItems().addAll(fileMenuSubImportFile, fileMenuSubImportPlugins);
        fileMenuSubExport.getItems().addAll(fileMenuSubExportFile, fileMenuSubExportDefinitions);
        fileMenu.getItems().addAll(fileMenuNew, fileMenuOpen,
            sep1,
            fileMenuSubImport, fileMenuSubExport,
            sep2,
            fileMenuSaveAs, fileMenuSave,
            sep3,
            fileMenuExit);
        
        getMenus().add(fileMenu);
   }
}

Than you can do:

root.getChildren().add(new MenuBarItem());

was able to do that with the tabs and made that portion a lot cleaner
feel like making a builder-type thing for the menus, but i was pointed at JavaFX Scene Builder (ty, ryantheleach) to try out and it looks promising

gonna try it out first and scrap the current code and come back to it later
as great as that tool is i still wanna try understanding it line by line

The reason why GUI code is so complex, is that it’s graphical by it’s very nature.

You end up having to write code that describes a scene, that isn’t an easy barrier for most people.

1 Like

Do you expect coding graphics to be easy? Imagine trying to do this in C++ without the .NET framework. Going from code -> visuals is one of the hardest things in programming. You are lucky enough to have the quite powerful javax library helping you out. Learn it. Live it. Love it. and use OOP to clean up your code and graphics programming will become second nature. I actually have a few template classes setup for layouts I normally use, maybe you could do the same?