println in Dialog with non-Latin symbol - java

So I want to println in ACM library dialog with a symbol from a non-English language in Java, but when I run it, only little squares appear.
IODialog dialog = getDialog();
dialog.println("ზაზა");

IODialog uses JOptionPane for its implementation, therefore it is subject to the same unicode handling problems that JOptionPane has.
Here there is a way of overcoming the problem. But we don't like links so let me summarize:
As per the comment above, playing with fonts is what you want to explore. Create a new font like this:
public class MyFont {
/*
Below code I extracted from
http://www.java-forums.org/java-tips/6522-swing-changing-component-default-font.html
then i customized it.
*/
public static void setUIFont (javax.swing.plaf.FontUIResource f){
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get (key);
if (value instanceof javax.swing.plaf.FontUIResource)
UIManager.put (key, f);
}
}
}
and then you set the actual font with whichever font it is that contains your unicode characters with this line:
MyFont.setUIFont(new javax.swing.plaf.FontUIResource("Iskoola pota",Font.BOLD,18)); // setting the default font for application
So what this does is it changes your default font. There is no more you have to do. If you need to change back to your default font before you made the change, well you have to reset the default font in this way.

Related

getText() always return empty string from dynamically created Chip component?

I'm trying to dynamically create some choice chip components based on an ArrayList of String from some computation and following are the code to create the chips and adding them to a ChipGroup created in layout XML file.
if (mChipGroup.getChildCount() == 0 ){
int i = 0;
for (Classifier.Recognition res: results){
Chip resultChip = new Chip(getDialog().getContext());
ChipDrawable chipDrawable =
ChipDrawable.createFromAttributes(
getActivity(),
null,
0,
R.style.Widget_MaterialComponents_Chip_Choice);
resultChip.setId(i++);
resultChip.setChipDrawable(chipDrawable);
resultChip.setText(res.getTitle());
mChipGroup.addView(resultChip);
}
}
The Chips displayed correctly with the text but when I tried to call getText() on the chips, it always return empty String but not the text contained by the chips. I tested this by setting the OnCheckedChangeListener on the ChipGroup and making a Toast with the text (though it didn;'t work). When I tried to display only the checkedId it works.
mChipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
Chip chip = group.findViewById(checkedId);
if(chip != null){
Toast.makeText(getContext(), chip.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
});
My current workaround is to have a variable holding the array results and use ArrayList.get(selectedChipId.getTitle()). but don't think it should be that way though
I also found that it is able to get text from Chips added in layout file but not run-time added Chips. Tried with both 1.1.0/alpha06 and 1.1.0/alpha07 release but am having no luck. Would like to have some advice if possible. Thank you very much.
So, it seems like a bug as per answered in here and here. Current workaround is to use ((ChipDrawable) chip.getChipDrawable()).getText() instead.

change font in JOptionPane.showInputDialog

I'm writing a program in java language and I want to make some changes in one part of my JOptionPane.showInputDialog. My dialog is this :
JOptionPane.showInputDialog("Total Amount Deposited:\t\t" +
totalAmount + "\n Enter Coin Value \n" + "(Enter 1 to stop)");
and I want to make the part that is saying (Enter 1 to stop) a little bit smaller than the other parts.
I'm beginner in java language (roughly 2 months :D) and don't have any other experience. so, please keep your answers simple. thanks in advance.
A JOptionPane will display the text in a JLabel, which supports basic HTML. So you will need to wrap your text string in HTML, then you can use different fonts, colors or whatever.
Simple example:
String text = "<html>Normal text <b>and bold text</b></html>";
JOptionPane.showInputDialog(text);
You can also use Font.pointSize() or Font.size() from java.awt.Font.
Create a String = "the text"
put in a label pa
use setFont();
Quick example :
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test extends JFrame {
public static void main(String[] args)
{
String a = "(enter 1 to stop)";
JLabel pa = new JLabel();
JFrame fr = new JFrame();
fr.setSize(200,200);
pa.setText(a);
pa.setFont(pa.getFont().deriveFont(11.0f)); //change the font size from here
fr.add(pa);
fr.setVisible(true);
}
}
For JDK 8.x, I find the following works to enlarge the font size of most portions of the built-in JOptionPane.showInputDialog, especially buttons, textboxes and comboboxes.
It is mostly generic, except for the two parts I want to be bold font.
It even allows for exceptions (think of it as an "all except" strategy) when you want to enlarge 99% of the pieces of an input dialog, except for one or two pieces.
Sorry for the bad formatting, but the "Code Sample" tool messed up everything and I don't have time to fix it.
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.swing.*;
/**
* Changes the font size used in JOptionPane.showInputDialogs to make them more
* ADA section 508 compliant by making the text size larger, which is very nice
* for older people and anyone else with vision problems.
*
* #param fontSize
* - size of the font in pixels
*/
private static void makeDialogsEasierToSee(int fontSize)
{
// This next one is very strange; but, without it,
// any subsequent attempt to set InternalFrame.titleFont will
// be ignored, so resist the temptation to remove it.
JDialog.setDefaultLookAndFeelDecorated(true);
// define normal and bold fonts that we will use to override the defaults
Font normalFont = new Font(Font.MONOSPACED, Font.PLAIN, fontSize);
Font boldFont = normalFont.deriveFont(Font.BOLD);
// get a list of objects that we can try to adjust font size and style for
List<Map.Entry<Object, Object>> entries = new ArrayList<>(UIManager.getLookAndFeelDefaults().entrySet());
// System.out.println(entries.size());
// remove anything that does NOT involve font selection
entries.removeIf(filter -> filter.getKey().toString().indexOf(".font") == -1);
// System.out.println(entries.size());
// Define a list of font sections of the screen that we do NOT want to
// enlarge/bold.
// The following is specific to jKarel so we do not obscure the display of
// "beeper piles" on the maps.
List<String> exempt = Arrays.asList("Panel.font");
// remove anything on the exempt list
entries.removeIf(filter -> exempt.contains(filter.getKey().toString()));
// System.out.println(entries.size());
// optional: sort the final list
Collections.sort(entries, Comparator.comparing(e -> Objects.toString(e.getKey())));
// apply normal font to all font objects that survived the filters
for (Map.Entry<Object, Object> entry : entries)
{
String key = entry.getKey().toString();
// System.out.println(key);
UIManager.put(key, normalFont);
}
UIManager.put("Label.font", boldFont);
UIManager.put("InternalFrame.titleFont", boldFont);
}
You basically have two straightforward options - Switch to JDialog or use HTML.
JOptionPane is intended for simple messages or interaction with the users. JDialog is a better choice if you want to break out of the canned use cases, and as you get more complex you will probably eventually have to switch to it.
To meet your immediate use case, you can send in an html message. The rules are:
You must begin and end with <html></html> tags. Put them in the middle and nothing happens.
You must remove all "\n"'s in your code. They don't work in html
anyway and the JPanel tries to use each line, as defined by \n's as a
separate html doc. Switch to
int totalAmount = 345; //for testing
String message = "<html>"
+ "Total Amount Deposited: " + totalAmount
+ "<br> Enter Coin Value "
+ "<br><span style='font-size:10'>(Enter 1 to stop)</span>"
+ "</html>";
JOptionPane.showInputDialog(message);

java swing minimal (or range for) font size for other application

For swing applications, default font settings for components are in current theme and can be retrieved using UIManager:
public class JavaTesting {
public static void main(String[] args) {
System.out.println(UIManager.get("Label.font"));
}
}
This can be adjusted in JAVA_HOME/lib/swing.properties for range of applications with for example:
swing.defaultlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel
or set at command line with:
java -Dswing.defaultlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel MyApp
Or the application itself remembers it's look and feel and has this value stored somewhere in configuration (file). It works, because application can set look and feel for itself, e.g.:
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
This all looks nice, but applications need often smaller fonts (e.g. unimportant status bar message for Label) or larger fonts (Label with some heading). Is there any recommended way for this?
As a user of high density monitor I had to discard lot of java applications, that use code like this - source code copied from squirrel sql:
Font tmp = (Font)UIManager.get("Label.font");
if (tmp != null) {
Font font = tmp.deriveFont(10.0f);
_statusBarFontInfo = new FontInfo(font);
}
This example code makes status bar completely unreadable, but most components are adjusted to new, bigger font.
For creating my own application, I might use some ratio (e.g. 50% to 150% of theme font), but hardcoding 0.50 - 1.50 range looks as a bad coding habbit as well. And it doesn't fix problem with applications that I don't have source code for. This belongs to the theme / L&F (Look & Feel, Look and Feel). Example:
FontUIResource Label_font_resource = (FontUIResource)javax.swing.UIManager.get("Label.font");
Font Label_font = Label_font_resource;
Font Label_font_bigger = Label_font.deriveFont(Label_font.getSize2D() * 1.5f);
It's worth asking before I try some ugly hacks like custom components replacing swing default ones, or Graphics2D.setFont adjustments, or any other scary stuff. :-)
Edit: the only thing that exists, is size variant, which is supported only by Nimbus theme. Allowed values are "mini", "small", "regular", "large". Example:
JComponent mini = new JButton("mini");
mini.putClientProperty("JComponent.sizeVariant", "mini");
However, looking into source code for Nimbus theme, it's not possible to simply adjust these values (e.g. scale to 150% for "large"), it's actually hard-coded!
package javax.swing.plaf.nimbus;
public final class NimbusStyle extends SynthStyle
{
public static final String LARGE_KEY = "large";
public static final String SMALL_KEY = "small";
public static final String MINI_KEY = "mini";
public static final double LARGE_SCALE = 1.15D;
public static final double SMALL_SCALE = 0.857D;
public static final double MINI_SCALE = 0.714D;
This value can be edited only by very advanced user (for example edit constant pool in JAVA_HOME/lib/rt.jar with program called "rej") - i tried it, and it works, BUT it doesn't work always, because they actually hard-coded (!!) the constants at several places (is this really best quality - standard library?), for example:
if ("large".equals(str))
{
this.scrollBarWidth = (int)(this.scrollBarWidth * 1.15D);
this.incrGap = (int)(this.incrGap * 1.15D);
this.decrGap = (int)(this.decrGap * 1.15D);
}
Bottom line: no, Java look and feel doesn't support that for now. I suggest using ratios, e.g. 0.7 - 1.3 of default font size.
JLabel smallLabel = new JLabel("some text");
smallLabel.setFont(smallLabel.getFont().deriveFont(0.8f * smallLabel.getFont().getSize2D()));
Instead of changing the Font, Nimbus has a sizeVariant for mini, small, regular and large components. This article has more examples: Resizing a Component.
myButton.putClientProperty("JComponent.sizeVariant", "mini");
What I've done in my Swing applications is include a Font class. Every other Swing component that needs a font calls the Font class.
import java.awt.Font;
public class MinesweeperFont {
protected static final String FONT_NAME = "Comic Sans MS";
public static Font getBoldFont(int pointSize) {
return new Font(FONT_NAME, Font.BOLD, pointSize);
}
}
Now in this case, I only needed one font type. I let the application pick the font size.
You could define a method for each font size you want, probably calling them small, medium, and large, so you can change the methods later to use a different point size or type if you want.
Since your entire application calls these font static methods, you only have to make font changes in one place.

Example text in JTextField

I am looking for a way to put example text into a swing JTextField and have it grayed out. The example text should then disappear as soon as any thing is entered into that text field. Some what similar to what stackoverflow does when a user is posting a question with the title field.
I would like it if it was already a extended implementation of JTextField so that I can just drop it in as a simple replacement. Anything from swingx would work. I guess if there is not an easy way to do this my option will probably be to override the paint method of JTextField do something that way maybe.
Thanks
The Text Prompt class provides the required functionality without using a custom JTextField.
It allows you to specify a prompt that is displayed when the text field is empty. As soon as you type text the prompt is removed.
The prompt is actually a JLabel so you can customize the font, style, colour, transparency etc..:
JTextField tf7 = new JTextField(10);
TextPrompt tp7 = new TextPrompt("First Name", tf7);
tp7.setForeground( Color.RED );
Some examples of customizing the look of the prompt:
If you can use external librairies, the Swing components from Jide software have what you are looking for; it's called LabeledTextField (javadoc) and it's part of the JIDE Common Layer (Open Source Project) - which is free. It's doing what mklhmnn suggested.
How about initialize the text field with default text and give it a focus listener such that when focus is gained, if the text .equals the default text, call selectAll() on the JTextField.
Rather than overriding, put a value in the field and add a KeyListener that would remove the value when a key stroke is registered. Maybe also have it change the foreground.
You could wrap this up into your own custom JTextField class that would take the default text in a constructor.
private JLabel l;
JPromptTextField(String prompt) {
l = new JLabel(prompt, SwingConstants.CENTER);
l.setForeground(Color.GRAY);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.getText().length() == 0) {
// Reshape the label if needed, then paint
final Rectangle mine = this.getBounds();
final Rectangle its = l.getBounds();
boolean resized = (mine.width != its.width) || (mine.height != its.height);
boolean moved = (mine.x != its.x) || (mine.y != its.y);
if (resized || moved)
l.setBounds(mine);
l.paint(g);
}
}
You can't do that with a plain text field, but you can put a disabled JLabel on top of the JTextField and hide it if the text field gets the focus.
Do it like this:
Define the string with the initial text you like and set up your TextField:
String initialText = "Enter your initial text here";
jTextField1.setText(initialText);
Add a Focus Listener to your TextField, which selects the entire contents of the TextField if it still has the initial value. Anything you may type in will replace the entire contents, since it is selected.
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
if (jTextField1.getText().equals(initialText)) {
jTextField1.selectAll();
}
}
});

How do you set the tab size in a JEditorPane?

A JTextArea's tab size can easily be set using setTabSize(int).
Is there a similar way to do it with a JEditorPane?
Right now, text with tabs in my pane looks like:
if (stuff){
more stuff;
}
And, I'd prefer a much smaller tab stop:
if (stuff){
more stuff;
}
As JEditorPane is designed to support different kinds of content types, it does not provide a way to specify a "tab size" directly, because the meaning of that should be defined by the content model.
However when you use a model that's a PlainDocument or one of its descendants, there is a "tabSizeAttribute" that provides what you are looking for.
Example:
JEditorPane pane = new JEditorPane(...);
...
Document doc = pane.getDocument();
if (doc instanceof PlainDocument) {
doc.putProperty(PlainDocument.tabSizeAttribute, 8);
}
...
From the Javadoc:
/**
* Name of the attribute that specifies the tab
* size for tabs contained in the content. The
* type for the value is Integer.
*/
public static final String tabSizeAttribute = "tabSize";
In case anyone's using a StyledDocument (The link on the other answer died)
You create a TabSet which is an array of TabStops. In my case I only cared about the 1st tab, and I wanted it 20px from the left, so this code worked for me:
StyleContext sc = StyleContext.getDefaultStyleContext();
TabSet tabs = new TabSet(new TabStop[] { new TabStop(20) });
AttributeSet paraSet = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabs);
pane.setParagraphAttributes(paraSet, false);
Took me a while to figure this out.
And decided to use TabStop's in a TabSet that have calculated width based on the font size.
This has to be reset when ever the font size changes (in the paint() method of the JEditPane).
Complicated stuff! :(

Categories