0

I have a class already provided that creates a layout of buttons but only with text inside of them. I was wondering if there is anyway to add the corresponding icon to the button:

        public ShapeButtons(View view){
            String[] buttonLabels = { "Circle", "Rectangle", "Square", "Squiggle", "Polyline" };

            for (String label : buttonLabels) {
                Button button = new Button(label);
                button.setMinWidth(100);
                this.add(button, 0, row);
                row++;
                button.setOnAction(this);
    }

For the constructor of button, I know there is a way to do Button(Text, Node graphic) but I can't seem to figure out how to do it?

2

1 Answer 1

5

There are plenty of characters in the Geometric Shapes block of the Unicode specification, and some other blocks, which will help you:

String[] buttonLabels = { "\u25cf Circle", "\u25ac Rectangle", "\u25a0 Square", "\u2307 Squiggle", "\u29d6 Polyline" };
Sign up to request clarification or add additional context in comments.

5 Comments

Clever approach to use Unicode, but there is a catch(1). This screenshot shows the first three Unicode chars, but not the last two. The result comes down to what the font supports. There are ways to check that a font can render all the characters required, but it might(1) require special handling in the code. 1) This caveat is because I don't have experience dealing with Java-FX. Maybe it has some way of ensuring a compatible font is used (I doubt it though). The screenshot itself was, of course, done using Swing (with which I'm well versed).
@AndrewThompson What version of Java and what OS? They’re showing up for me in Java 8, 9, 10, and 11, in Windows 10 and in Debian. No unusual fonts installed as far as I know.
1) 1.8.0_45 2) Windows 10 Home. The font that was used is more relevant to the last two not rendering. (Which was the Dialog logical font, BTW.)
Further font details: A quick test shows that.. 3 of 248 installed fonts can display this string locally (when 'this string' is an amalgamation of the strings of the buttonLabels array).
Huh.. I just checked using another app. (devoted to Unicode characters) & it not only suggests there are a lot more than 3 fonts that support the squiggly line, but that Dialog font is one of them! It does seem like a local configuration problem. 😕

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.