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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Lebcode
  • 75
  • 5

1 Answers1

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" };
VGR
  • 40,506
  • 4
  • 48
  • 63
  • Clever approach to use Unicode, but there is a catch(1). This [screenshot](https://i.stack.imgur.com/A2RdZ.png) 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). – Andrew Thompson Nov 10 '18 at 04:54
  • @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. – VGR Nov 10 '18 at 15:30
  • 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.) – Andrew Thompson Nov 10 '18 at 16:05
  • 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). – Andrew Thompson Nov 10 '18 at 16:13
  • 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. – Andrew Thompson Nov 10 '18 at 16:22