#(owolib) Trying to save text from textArea component to a variable

11 messages · Page 1 of 1 (latest)

edgy bough
#

As said from the title, I'm trying to get text that you put in a textArea component, and save it to a variable. But, it's giving me a NullPointerException error when I open the screen. I'm using the owolib library.

Here's my code so far:

package org.pyrotonic.simplenotes.client.screen;

import io.wispforest.owo.ui.base.BaseOwoScreen;
import io.wispforest.owo.ui.component.Components;
import io.wispforest.owo.ui.component.TextAreaComponent;
import io.wispforest.owo.ui.container.Containers;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.core.Insets;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;

public class TestScreen extends BaseOwoScreen<FlowLayout> {
    @Override
    protected @NotNull OwoUIAdapter<FlowLayout> createAdapter() {
        return OwoUIAdapter.create(this, Containers::verticalFlow);
    }

    @Override
    protected void build(FlowLayout rootComponent) {
        String textContent = rootComponent.childById(TextAreaComponent.class, "text-test").getText();
        rootComponent.surface(Surface.VANILLA_TRANSLUCENT);
        rootComponent
                .surface(Surface.VANILLA_TRANSLUCENT)
                .horizontalAlignment(HorizontalAlignment.CENTER)
                .verticalAlignment(VerticalAlignment.CENTER);
        rootComponent.child(
                Containers.verticalFlow(Sizing.content(), Sizing.content()))
                .child(Components.textArea(Sizing.fixed(10), Sizing.fixed(10), "Text"))
                .child(Components.button(Text.literal("Print Text"), buttonComponent -> {System.out.println(textContent);}))
                .padding(Insets.of(10))
                .surface(Surface.DARK_PANEL)
                .verticalAlignment(VerticalAlignment.CENTER)
                .horizontalAlignment(HorizontalAlignment.CENTER)
                .id("text-test");

    }
}

Here's a log of me trying to open the screen: https://mclo.gs/ISAbNqu

blazing creek
#

you're trying to access text-test before it's added

#

String textContent = rootComponent.childById(TextAreaComponent.class, "text-test").getText();
how would this work when you're adding the component after this?

edgy bough
#

your right that is a problem, but I did that because I also made a button that prints the content of textContent, and I needed to put it before that. I just tried getting rid of the variable and instead directly printing the content of the textArea and it gave me the same error
heres what i changed:

        rootComponent.surface(Surface.VANILLA_TRANSLUCENT);
        rootComponent
                .surface(Surface.VANILLA_TRANSLUCENT)
                .horizontalAlignment(HorizontalAlignment.CENTER)
                .verticalAlignment(VerticalAlignment.CENTER);
        rootComponent.child(
                Containers.verticalFlow(Sizing.content(), Sizing.content()))
                .child(Components.textArea(Sizing.fixed(10), Sizing.fixed(10), "Text"))
                .child(Components.button(Text.literal("Print Text"), buttonComponent -> {System.out.println(rootComponent.childById(TextAreaComponent.class, "text-test").getText());}))
                .padding(Insets.of(10))
                .surface(Surface.DARK_PANEL)
                .verticalAlignment(VerticalAlignment.CENTER)
                .horizontalAlignment(HorizontalAlignment.CENTER)
                .id("text-test");
blazing creek
#

create a TextArea component and have a reference to it

#

then use that reference instead of searching for it in the hierarchy

#

you can set the same reference as the child

edgy bough
#

Ok, here's what I did:

    @Override
    protected void build(FlowLayout rootComponent) {
        rootComponent.surface(Surface.VANILLA_TRANSLUCENT);
        rootComponent
                .surface(Surface.VANILLA_TRANSLUCENT)
                .horizontalAlignment(HorizontalAlignment.CENTER)
                .verticalAlignment(VerticalAlignment.CENTER);
        TextArea textArea = (TextArea) rootComponent
                .child(Components.textArea(Sizing.fixed(100), Sizing.fixed(100), "Text"))
                .verticalAlignment(VerticalAlignment.CENTER)
                .horizontalAlignment(HorizontalAlignment.CENTER)
                .id("text-test");
        rootComponent.child(
                        Containers.verticalFlow(Sizing.content(), Sizing.content()))
                .child(Components.button(Text.literal("Print Text"), buttonComponent -> {System.out.println(textArea.getText());}))
                .padding(Insets.of(10))
                .surface(Surface.DARK_PANEL)
                .verticalAlignment(VerticalAlignment.CENTER)
                .horizontalAlignment(HorizontalAlignment.CENTER)
                .id("print-button");
    }

I made a new component and made a reference to it, and I had to cast it to TextArea. But, when I launch the game, I get a ClassCastException error.

blazing creek
#

i have not worked with owo, i'm saying this only based on what i've seen

#

TextArea textArea = Components.textArea(Sizing.fixed(100), Sizing.fixed(100), "Text");

#

then pass this