Kolibrie
Kolibrie

Reputation: 149

Include the minus superscript in a JButton's text

I am trying to set the text of my JButton to "Compute b⁻¹ (mod a)" in UTF-8 encoding, but neither of these two attempts work. The problem is the minus superscript, but not sure what I can do.

Attempt 1:

_computeModInverseButton = new JButton("Compute b⁻¹ (mod a)");

enter image description here

Attempt 2:

    _computeModInverseButton = new JButton("Compute b<html><sup>-1</sup></html> (mod a)"); 

enter image description here

So the problem was my HTML formatting, but now with

_computeModInverseButton = new JButton("Compute b-1 (mod a)");

It looks likeenter image description here

How can I format this to fit well with the superscript?

Upvotes: 0

Views: 665

Answers (3)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

As per my comment, you should wrap the whole String in HTML:

JButton myButton = new JButton("<html>Compute b<sup>-1</sup> (mod a)</html>");

Upvotes: 2

Kolibrie
Kolibrie

Reputation: 149

Solved: all text needed to be in the html tags.

_computeModInverseButton = new JButton("Compute b-1 (mod a)");

Upvotes: 0

Peter Tillemans
Peter Tillemans

Reputation: 35331

see https://docs.oracle.com/javase/tutorial/uiswing/components/html.html

To specify that a component's text has HTML formatting, just put the tag at the beginning of the text, then use any valid HTML in the remainder. Here is an example of using HTML in a button's text:

button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");

Upvotes: 5

Related Questions