Basically Swing JComponents are able to display numbers in fractions in this form 2 2/3
. How can I paint fraction in the nicest form, for example 2⅔?
.
EDIT
.
as see I have only one way JTable
inside JSpinner
with one TableColumn
and TableRow
(that could simulated plain JtextField
too), where TableRenderer
could be some of JTextComponent
formatted by using Html and on TableCellEdit
event the TableEditor
to swith to the plain JFormattedTextField
,
is there another way, could it be possible with plain J(Formatted)TextField
too ???
To specify that a component's text has HTML formatting, just put the <html> 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>"); Here is the resulting button.
We know that Swing is a GUI widget toolkit for Java. Every application has some basic interactive interface for the user. For example, a button, check-box, radio-button, text-field, etc. These together form the components in Swing.
Swing API architecture follows loosely based MVC architecture in the following manner. Model represents component's data. View represents visual representation of the component's data. Controller takes the input from the user on the view and reflects the changes in Component's data.
On reflection, Unicode fractions among the Latin-1 Supplement and Number Forms offer limited coverage, and fancy equations may be overkill. This example uses HTML in Swing Components.
Addendum: The approach shown lends itself fairly well to rendering mixed numbers. For editing, key bindings to +
and /
could be added for calculator-style input in a text component. I've used org.jscience.mathematics.number.Rational
to model rational numbers, and this parser could be adapted to evaluating rational expressions.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/** @see https://stackoverflow.com/questions/7448216 */
public class HTMLFractions extends JPanel {
private static int N = 8;
public HTMLFractions() {
this.setLayout(new GridLayout(N, N, N, N));
this.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
this.add(create(r + N, r + 1, c + 2));
}
}
}
private JLabel create(int w, int n, int d) {
StringBuilder sb = new StringBuilder();
sb.append("<html><body>");
sb.append(w);
sb.append("<sup>");
sb.append(n);
sb.append("</sup>");
sb.append("<font size=+1>/<font size=-1>");
sb.append("<sub>");
sb.append(d);
sb.append("</sub>");
sb.append("</html></body>");
JLabel label = new JLabel(sb.toString(), JLabel.CENTER);
label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
return label;
}
private void display() {
JFrame f = new JFrame("HTMLFractions");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new HTMLFractions().display();
}
});
}
}
Use the Java2D API. The is an excellent book on it from O'Reilly.
1- Use the font you like. 2- Convert the Glyphs you need (e.g. "2" "/" and "3") into Java2D shapes. 3- Use the Java@d method to scales and place the shapes together
4- This part depends on the component. I think a lot of components take some kind of image instead of text. Convert your shapes into whatever fits into the components you w ant.
5- This should look really professional if you do a good job :)
Come on give me 50!!!
=============
Thanks so much for the points. Here is an example of how to do the first step. It'll show how to get an instance of enter code here
Shape from a character in the font of your choice.
Once you have your Shape
You can use Graphics2D to create the image you want (scale, compose, etc). All the swing components are different but all have a graphics context. Using the graphics content you can draw on any Swing Component. You can also make transparent layers and stick a transport JPanel over anything you want. If you just want to display a fraction on a label that's easy. If you had some sort of word processor in mind that's hard.
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
public class Utils {
public static Shape generateShapeFromText(Font font, char ch) {
return generateShapeFromText(font, String.valueOf(ch));
}
public static Shape generateShapeFromText(Font font, String string) {
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
try {
GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string);
Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY());
return shape;
} finally {
g2.dispose();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With