[SOLVED] How to change font of a nifty button (Java, not XML)

Hello again, I’m new to JME and I have the the problem that i can’t change the font of a button in a nifty gui. It works with a text, e.g.:

text(new TextBuilder() {{
    text("1234");
    font("Fonts/SomeFont.fnt");
}});

But it’s not working with this:

control(new ButtonBuilder("CloseButton", "Close") {{
    font("Fonts/SomeFont.fnt");
}});

What am I doing wrong?
And by the way, where can I find a documentation that helps me finding errors like this (the nifty-gui manual didn’t help me with this problem)?

PS: I thank you all for spending your time to help guys like me!!

Nifty 1.3.2 manual (which I assume you are referring to) is just fine. Nifty just needs some practice, and having quick links to the source doesn’t hurt. Mainly to figure out hierarchies on control definitions etc.

You could try to circumvent this by using styles?

I tried it with a style like this:

new StyleBuilder() {{
    id("myStyle");
    font("Fonts/SomeFont.fnt");
}}.build(nifty);

but again, to set the attribute with

style("myStyle");

works with a text, but not with a button.

Look here:

The builder does nothing with the Font parameter you give. You need to explicitly call setFont on the control.

Also, you often need to check the hierarchy (for using styles):

Write the style like this

new StyleBuilder() {{
    id("myStyle#text");
    font("Fonts/SomeFont.fnt");
}}.build(nifty);

And set the button style to myStyle (without #text).

1 Like

I don’t understand why, but it works !!
Thank you very much!!

When i try the same with a TextFieldBuilder it’s not working. This works:

control(new TextFieldBuilder("txtLaenge","1035"){{
}});

But this does not

control(new TextFieldBuilder("txtLaenge","1035"){{
    style("myStyle");
}});

I get the error that says “missing childLayout Attribute”. When i change it to

control(new TextFieldBuilder("txtLaenge","1035"){{
    style("myStyle");
    childLayoutCenter();
}});

i still get the same error !?!?
I’m new to Java and JME (self-taught) and must admit that i don’t really understand the documentation mentioned above.

It works because of the hierarchy:

1 Like

This might not work since you are wiping out some important bits from the original style (the childLayouts).

See the original style:

But are you really using different font in every control you are creating? Set up a base font so that you don’t need to change this for every control.

1 Like

If you want to write a complete new style I advise you to copy the nifty black style XMLs and edit them. They have the right ids and you can just customize their appearance. If you don’t you can just take a look at the control definition xml to pickup the control children ids and use them in the selector.

All i want to do ist to use a different font for all Elements.
I found a “fonts.xml” in "C:\Program Files\jmonkeyplatform\jmonkeyplatform\libs\nifty-style-black-1.4.3.jar with this content

<?xml version="1.0" encoding="UTF-8"?>
<nifty-styles xmlns="http://nifty-gui.lessvoid.com/nifty-gui">

    <style id="button-font">
        <attributes font="aurulent-sans-16.fnt"/>
    </style>

    <style id="tab-font">
        <attributes font="aurulent-sans-16.fnt"/>
    </style>

    <style id="base-font">
        <attributes font="aurulent-sans-16.fnt"/>
    </style>
</nifty-styles>

Would it work to change the fonts here ?
If yes, how can i do this? I don’t know the workflow to change content in a jar-file .

1 Like

All i want to do ist to use a different font for all Elements.
Can you tell me how to set up a base font?

Yes, try to write a style file and load it from nifty loadStyleFile method.

<?xml version="1.0" encoding="UTF-8"?>
<nifty-styles xmlns="http://nifty-gui.lessvoid.com/nifty-gui">
    <style id="base-font">
        <attributes font="font.fnt"/>
    </style>
</nifty-styles>

or write it in your screen XML file.

<nifty xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://nifty-gui.lessvoid.com/nifty-gui" xsi:schemaLocation="https://raw.githubusercontent.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd https://raw.githubusercontent.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd">
    <style id="base-font">
        <attributes font="font.fnt"/>
    </style>
    <screen>
    <!--code code code....-->
    </screen>
</nifty>

You should write the code that overrides the base-font style after loading the nifty black style.

3 Likes

That’s it !!! Thank you very much !!!

2 Likes