Unicode has a set of characters that will let you produce fractions without having to do special formatting, e.g., ⁴⁄₉ or ⁵⁶⁄₁₀₀.
You could set up an array of superscript digits, and an array of subscript digits, and convert your numbers to strings of these digits, and then combine them with the fraction slash in between.
Advantages are that it's more general (you can use the code in other contexts), and the result will tend to look better than if you tried to reproduce it using HTML formatting. Of course, you need to have Unicode fonts on your system, but most systems do these days.
A possible implementation in code:
public String diagonalFraction(int numerator, int denominator) {
char numeratorDigits[] = new char[]{
'\u2070','\u00B9','\u00B2','\u00B3','\u2074',
'\u2075','\u2076','\u2077','\u2078','\u2079'};
char denominatorDigits[] = new char[]{
'\u2080','\u2081','\u2082','\u2083','\u2084',
'\u2085','\u2086','\u2087','\u2088','\u2089'};
char fractionSlash = '\u2044';
String numeratorStr = new String();
while(numerator > 0){
numeratorStr = numeratorDigits[numerator % 10] + numeratorStr;
numerator = numerator / 10;
}
String denominatorStr = new String();
while(denominator > 0){
denominatorStr = denominatorDigits[denominator % 10] + denominatorStr;
denominator = denominator / 10;
}
return numeratorStr + fractionSlash + denominatorStr;
}
JFormattedTextField
editable with a keyboard, although aJSpinner
would work pretty much as expected. Fraction characters would also limit you to a small set of predefined fractions. The alternative would be to draw the solidus (fraction bar) and various bits of text manually in a custom-painted component. – Decoupagewhat I want it to look like
, sure there is idea with Font size > 28, I can't problem split that to the twoJFormattedTextField
chained together by DocumentListener, but in my question I trying to avoid that :-) – CattleyaJFormattedTextField
, or do you just need a text input component that's compatible withJSpinner
and standalone display and editing? It's very difficult to do with aJFormattedTextField
, but it should be achievable with a customJTextComponent
. – DecoupageJFormattedTextField
? – Decoupage