How to print the Indian Rupee Symbol in java - java

I tried to display rupee symbol by using below code. But in output I'm getting a box symbol instead of rupee:
Code:
public class Applet extends JApplet {
public static final String RUPEE = "\u20B9";
public void init () {
setLayout(new FlowLayout());
JLabel b = new JLabel("rupee : " + RUPEE +123);
b.setFont(new Font("Arial", Font.PLAIN, 14));
add(b);
}
}

This box symbol is usually a sign that your font doesn't support this character. There's nothing you can do short of using a font that supports it instead of the current one.
Newer versions of Arial support this, so maybe your system needs an update.

Related

how to print a glyph of supplementary characters in java onto my JTextField when i just click the button

I have a simple program just need to set the character whose Unicode value larger the character data type (supplementary character) on JTextField when the button is click .Tell me i am really fed up and how i will do it .This problem have already taken my 4 days.
//importing the packages
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
//My own custom class
public class UnicodeTest implements ActionListener
{
JFrame jf;
JLabel jl;
JTextField jtf;
JButton jb;
UnicodeTest()
{
jf=new JFrame();// making a frame
jf.setLayout(null); //seting the layout null of this frame container
jl=new JLabel("enter text"); //making the label
jtf=new JTextField();// making a textfied onto which a character will be shown
jb=new JButton("enter");
//setting the bounds
jl.setBounds(50,50,100,50);
jtf.setBounds(50,120,400,100);
jb.setBounds(50, 230, 100, 100);
jf.add(jl);jf.add(jtf);jf.add(jb);
jf.setSize(400,400);
jf.setVisible(true); //making frame visible
jb.addActionListener(this); // registering the listener object
}
public void actionPerformed(ActionEvent e) // event generated on the button click
{ try{
int x=66560; //to print the character of this code point
jtf.setText(""+(char)x);// i have to set the textfiled with a code point character which is supplementary in this case
}
catch(Exception ee)// caughting the exception if arrived
{ ee.printStackTrace(); // it will trace the stack frame where exception arrive
}
}
// making the main method the starting point of our program
public static void main(String[] args)
{
//creating and showing this application's GUI.
new UnicodeTest();
}
}
Since you are not giving enough information on what's wrong, I can only guess that either or both:
You are not using a font that can display the character.
You are not giving the text field the correct string representation of the text.
Setting a font that can display the character
Not all fonts can display all characters. You have to find one (or more) that can and set the Swing component to use that font. The fonts available to you are system dependent, so what works for you might not work for others. You can bundle fonts when you deploy your application to ensure it works for everyone.
To find a font on your system that can display your character, I used
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font f : fonts) {
if (f.canDisplay(66560)) {
System.out.println(f);
textField.setFont(f.deriveFont(20f));
}
}
The output (for me) is a single font, so I allowed myself to set it in the loop:
java.awt.Font[family=Segoe UI Symbol,name=Segoe UI Symbol,style=plain,size=1]
as also noted in the comments to the question by Andrew Thompson.
Giving the text field the correct string representation
The text fields require UTF-16. Supplementary characters in UTF-16 are encoded in two code units (2 of these: \u12CD). Assuming you start from a codepoint, you can convert it to characters and then make a string from them:
int x = 66560;
char[] chars = Character.toChars(x); // chars [0] is \uD801 and chars[1] is \uDC00
textField.setText(new String(chars)); // The string is "\uD801\uDC00"
// or just
textField.setText(new String(Character.toChars(x)));
as notes by Andrew Thompson in the comments to this answer (previously I used a StringBuilder).

How do edit the size of swings?

So, I just need to add a title to my swing. Here's the code:
import javax.swing.*;
import java.util.Scanner;
public class Wall extends JFrame {
public static void main(String[] args) {
new Wall();
}
public Wall() {
final String title = "Wall Game";
this.setSize(300,300); //sets the screen
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle(title);
this.setVisible(true);
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("Welcome to the Wall Game!");
JLabel label2 = new JLabel("Click the button to read the instructions!");
panel1.add(label1);
panel1.add(label2);
this.add(panel1);
}
}
I want the Welcome to the Wall Game! part of this to be bigger. Is it possible to make it bigger? If there's a bonus, I wonder if you can add different fonts. But that's not important right now.
Thanks to anyone who knows :P.
I believe all you need is something like this:
JLabel label1 = new JLabel("Welcome to the Wall Game!");
label1.setFont(new java.awt.Font("Arial", 1, 24));
This will set the font to type Arial, '1' will set it to plain, and '24' is the font size. Just read this if you want to do more with the font: https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html
Font font = new Font("Font Name", Font.TYPE_OF_FONT, size);
yourLabel.setFont(font);
Basically, you're creating a "new" font (not really "new" because Arial already exists, but you're just making it bigger (deriving it)).
The first parameter: "Font Name" is self-explanatory: The name of the font; e.g Arial.
The second parameter: Font.TYPE_OF_FONT determines what type your font is: for example, Font.ITALIC italicizes your label. Font.BOLD bolds your label. For more information, see here.
The third parameter: size, which is, in fact, the size! Changing size to 42 makes the font size 42, and changing it to 12345.67890f changes the font size to 12345.67890.
Applying the font
label.setFont(font);
Pretty self-explanatory.
For more information, read this link.

JFrame Text Size

I have been working to try and make a JFrame that has text in the middle that is really big, but every attempt I've made to change the size of the text has just resulted in teeny tinny little words in the corner of the screen.
public class StuffMost {
public static JLabel three = new JLabel();
public static JFrame one = new JFrame();
public static JButton four = new JButton();
public static void seconday() {
one.setVisible(true);
one.setResizable(true);
one.setDefaultCloseOperation(one.EXIT_ON_CLOSE);
one.setSize(1280, 800);
one.setTitle("Hello! Welcome to my window. You Shall Never Leave.");
one.setBackground(Color.BLUE);
three.setText("Dont");
one.add(three);
three.setSize(900, 300);
one.add(four);
}
}
Do not use setSize on the Label, for that does not change the font. If you want to make the font bigger then you would use:
three.setFont(new Font("Courier", Font.BOLD,75));
if you wanted a different font, then you could change it to "Arial" or something like that, if you don't want it bolded, then change the second parameter to Font.PLAIN, and the last parameter is the font size, which in this case is 75

How do I add text to a JTextArea?

I'm making a java Text Editior and I can't seems to know how to insert a line of text that is "[code][/code]" Here is what I'm trying to program. The method for the inserting is called "insert". So it has to be something that is insert,(something that inserts strings of text in JTextArea)
/////////////////// CODE //////////////////////////////////////////////////////////////////////////////////////////////////
this.insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
/////////////// END OF CODE ///////////////////////////////////////////////////////////////////
Simple example to set/assign text to JTextArea .. this is not the solution but it will help you...
JTextArea textArea = new JTextArea(
"This is an editable JTextArea. " +
"A text area is a \"plain\" text component, " +
"which means that although it can display text " +
"in any font, all of the text is in the same font."
);
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
Although to set the text .. use this method
void insert(String str, int pos)
Inserts the specified text at the specified position.
public void setText(String t)
Sets the text of JTextArea
For refrerence and help please follow jtextareaguide
Link to video tutorial
Guide for Simple Editor in Java
Java already provides a method for inserting text in the JTextArea class. Try this...
JTextArea t = new JTextArea();
t.setText("specified string");
t.append("+ added string");
use:
JTextArea text=new JTextArea();
text.setText("Message..");
Here is a doc.
public class JTextArea extends JTextComponent
A JTextArea is a multi-line area that displays plain text. It is intended to be a lightweight component that provides source compatibility with the java.awt.TextArea class where it can reasonably do so. You can find information and examples of using all the text components in Using Text Components, a section in The Java Tutorial.
Try this:
JTextArea textj1 = new JTextArea();
textj1.setText(textj1.getText().trim() + "a string or even an arraylist");
Better using:
textArea.setText(textArea.getText()+" Message");

How to use Wingdings font in Java Swing

When I try to use the Wingdings font (or other symbol fonts), the text comes out as rectangles instead of the correct text. How do I get the correct characters to show?
import java.awt.*;
import javax.swing.*;
public class WingdingsFontDisplay extends JFrame
{
public static void main(String[] args)
{
new WingdingsFontDisplay();
}
public WingdingsFontDisplay()
{
this.setSize(500,150);
this.setTitle("Fun with Fonts");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//This shows that I do have "Wingdings" available
GraphicsEnvironment g;
g = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = g.getAvailableFontFamilyNames();
for(String f : fonts)
{
System.out.println(f);
}
//Displaying text in the Wingdings font shows rectangles
JLabel wingdingsText = new JLabel("All work and no play makes Jack a dull boy");
Font f1 = new Font("Wingdings", Font.PLAIN, 14);
wingdingsText.setFont(f1);
this.add(wingdingsText, BorderLayout.NORTH);
//Displaying text in Arial works correctly
JLabel arialText = new JLabel("All work and no play makes Jack a dull boy");
Font f2 = new Font("Arial", Font.PLAIN, 14);
arialText.setFont(f2);
this.add(arialText, BorderLayout.SOUTH);
this.setVisible(true);
}
}
You need to use the appropriate Unicode range for the symbols you seek. In Java, symbols are not overlaid on the ASCII range, but have their own distinct character codes.
You can find a reference to the appropriate symbol codes at http://unicode.org/~asmus/web-wing-ding-ext.pdf . Most common symbols are in the 0x2200 and 0x2700 Unicode ranges.
Your Java install may include the SymbolTest sample applet, which makes it straightforward to preview the presentation of Unicode ranges with available fonts. Be warned, however, that better Java implementations will use font substitutions for symbols or characters not in the specified font, so you'll want to be sure you're actually getting the specified font.

Categories

Resources