Please find the below sample code, The UTF-8 character properly displaying in windows machine. But, its not proper for Linux machine (Ubuntu).
import javax.swing.JOptionPane;
public class JContPaneTest
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3002\u30c7\u30fc\u30bf\u30d9\u30fc", "Error",JOptionPane.ERROR_MESSAGE);
}
}
Is there any way to solve this problem?
Not all fonts contain the full unicode set of glyphs. The blobs are probably the result of a deficient font, but in java it's hard to determine what font is actually being used, and I don't know a way to determine if these blobs are being emitted except by seeing them.
I had huge problems with unicode in pop-up menus, which probably is a very similar problem
to yours.
2 things to try.
1) Write a font test to display your string in all fonts that are available to java.
2) Try using a non-swing component. Non-swing components use the underlying OS font support.
Related
I have written many projects using JOptionPanes, and almost every time I am frustrated by the fact that I cannot wrap text inside them.
I know JOptionPane supports HTML, but that always seems to fall short of doing what I want. For one thing, JOptionPane doesn't fully support HTML - the max-width property would be awesome to have, for example. For another, neither <br /> nor <p> tags provide the functionality I'm really after - a JOptionPane that is small when the message is small, and can grow when the message is larger, but which does not get too large, and eventually breaks to a new line.
That brings me to my question. Is there some way to write a custom component of my own, maybe inheriting from a JFrame or something similar, which will allow me to simulate the effects of a JOptionPane, but which provides finer-grain control of the text displayed? This should include, at minimum, functionality to control max width and to wrap text.
I have not found an answer to this by searching other questions (maybe the answer is drowned out by all the answers saying "use HTML"). This is NOT a duplicate of those questions.
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class OptionPaneUtilities {
public static void main(String[] args) {
OptionPaneUtilities.showMessage(null, "I sometimes I think we forget just how flexible the JOptionPane API is and what it can do. A little effort could go a long way");
}
public static void showMessage(Component comp, String message) {
JTextArea ta = new JTextArea(message, 1, 20);
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
ta.setOpaque(false);
ta.setBorder(null);
ta.setEditable(false);
ta.setFocusable(false);
JOptionPane.showMessageDialog(comp, ta);
}
}
Now, obviously, this is just a really simple example, you're going to have to spend some time thinking about how best to apply this concept to your own API so it meets your needs and provides you with the greatest amount of flexibility.
You could make use of a JEditorPane instead and get both plain text and HTML to work with out to much more effort
Are you talking about something such as just adding a line break so the option box doesn't show this long string of text? If so :
JOptionPane.showMessageDialog(null, "This
"\nmessage
"\nhas
"\nlinebreaks.")
should suffice. Using \n(text) creates a line break. Hopefully this helps, have a good night. :)
Okay, I'm only in chapter 3 of my Java book, and I'm wanting to do something that would help with reading the output window of my Netbeans 8.0.2 IDE. I want this so I can change the color of outputs that are answers or results of something that my program is creating.
As far as I know, what you can do is only client-side and if you try to transfer the program itself to a new computer, it will not change the color of text. What I am looking for is some line(s) of code that can declare a color to a letter or line of characters.
Something around the likes of:
System.out.println.colorRED;
And when this line of code is hit in the program, instead of the IDE showing black (default color) in the output window, it displays it as the color Red, or #ff0000.
Arrows are what I want to be Red
I am also looking for something else that would be very helpful in terms of clearing up the window where the IDE outputs System.out.println to. I would like a "cls" of sorts, and any includes that would be needed for said code to work. I don't want a simulated window clear, I want a legitimate clear like C++ has. It's okay if it's OS dependent. (I want this so that I can remove the tons of output spam whenever the user is looping through my program)
As you can see in the picture above, it has 2+ runs of the program and will soon start using the slider to go to the next operation. I want it so before it prints out the output, that it clears that window, and starts over as if the program was stopped and re-ran (Text appearing at the top instead of the simulated cls, at the bottom).
Thanks to anyone who can help with this.
If the console supports ANSI coloring, you can use special ANSI characters to define colors.
http://en.wikipedia.org/wiki/ANSI_escape_code
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";
public static void main(String[] args) {
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
}
I have a big project to debug, and I was wondering if there is anyway I could use to change the System.out.println method in the output of eclipse
for example :
System.out.println("I want this to be red");
System.out.println("I want this to be blue");
System.out.println("I want this to be yellow");
System.out.println("I want this to be magenta");
for more readability.
EDIT
with sysout I have this
with syserr I have this
Within Eclipse, the simplest approach would be to use System.err.println for lines you want to be in red - I believe that's the default. (You can change it in Preferences -> Run/Debug -> Console).
That difference won't show up when running in a real console of course, but I don't think the Eclipse console supports ANSI colour escape sequences etc.
EDIT: For the Windows console, I'd expect ANSI escape sequences to work. It's not hugely portable, but if that's not a problem, you could just create a class to encapsulate the escape sequences appropriately, so you could call something like:
ansiConsole.printRed("sample line in red");
ansiConsole.printBlue("sample line in blue");
(I'd probably make those methods return back to whatever the "current" colour was after each call.)
EDIT: As noted in comments, the Jansi library already exists, so you might as well use that. It doesn't have the methods described above, but I'm sure it'll still do what you want...
Please Refer the following code.Also refer this link for ANSI color codes. http://en.wikipedia.org/wiki/ANSI_escape_code
public class ColourConsoleDemo {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("\033[0m BLACK");
System.out.println("\033[31m RED");
System.out.println("\033[32m GREEN");
System.out.println("\033[33m YELLOW");
System.out.println("\033[34m BLUE");
System.out.println("\033[35m MAGENTA");
System.out.println("\033[36m CYAN");
System.out.println("\033[37m WHITE");
}
}
Please have a look at Jansi (Jansi's Github)
Jansi is a small java library that allows you to use ANSI escape
sequences to format your console output which works even on windows.
It seems that you want to highlight the output of System.out.println() using different colours in order to help you to debug , why don't redirect all the output of System.out to a file in your program entry point :
FileOutputStream fis = new FileOutputStream(new File("log.txt"));
PrintStream out = new PrintStream(fis);
System.setOut(out);
Then using some free and portable real-time log file monitoring tool that has configurable highlighting function using different colours , such as BareTail , to view this file.
To align my JFrame from righ-to-left, I use:
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
but this works only if I use the following style (decoration) of the JFrame:
public class RightToLeft {
public static void main(String []args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }
catch (Exception e) { e.printStackTrace(); }
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("العنوان بالعربي");
frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
but I want it to work without this decoration. How to solve this issue?
EDIT:
#mre I want a JFrame like this one:
EDIT2:
I really really need this issue to be fixed, so I offer 500+ to who will give a JFrame like this (with WindowsLookAndFeel):
The following explains what you observe through your code snippet:
ComponentOrientation is applicable only to Swing (and AWT actually) components.
When using JFrame.setDefaultLookAndFeelDecorated(true);, then the whole frame decoration is performed by Swing (the LookAndFeel itself in fact), so this works as expected.
If you don't set this option though, that means that the OS is in charge of the frame decoration, however the OS cannot be aware of the ComponentOrientation used by your Swing components!
I expect the OS (did you mention what OS you use exactly? It seems to be Windows 7 right?) to perform the correct decoration based on the currently selected Locale. Hence if you have an Arabic locale setup and selected on your OS, I guess all windows decorations are right to left. Did you try changing that Locale (through the "Region and Language" control panel)? Did it work?
Note: I don't think that you can change these OS settings directly from Java, but you can read them with Locale.getDefault().
To sum up:
first of all, you have to ensure that your OS is properly configured in terms of text orientation; sorry I can't help much here because I don't have any right-to-left languages installed on my Windows machine.
then, use the system look and feel and ensure that JFrame.setDefaultLookAndFeelDecorated(false);
if that doesn't work, then you may consider posting your code snippet, along with your system configuration to Oracle Java bugs list
What follows are extra notes on how to improve this part of your code (but this is not a fix for your issue)
By the way, if you let the user define its OS language preferences, then you shouldn't explicitly hard-code frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); but rather use something like:
frame.applyComponentOrientation(
ComponentOrientation.getOrientation(Locale.getDefault()));
Where Locale.getDefault(), unless explicitly changed within your application, will return the OS-level currently selected Locale.
Also note that it is preferable to use applyComponentOrientation() rather than setComponentOrientation(), because the former recursively sets the given orientation to the whole hierarchy of components.
Finally, you will have to ensure in your windows that the LayoutManager used is right-to-left aware; this is normally OK with all standard Swing layouts, but not for all 3rd-party layout managers, if you use some.
#Eng.Fouad
just joke and this one ???...
code:
import java.awt.ComponentOrientation;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel;
public class RightToLeft {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
UIManager.setLookAndFeel(new SubstanceOfficeSilver2007LookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("العنوان بالعربي");
frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
});
}
private RightToLeft() {
}
}
I suspect it has to do more with the OS. Normally (if you don't call setDefaultLookAndFeelDecorated) it is the OS that provides the frame decoration, not the LAF.
You should try changing your preferences in the OS to say you want right to left orientation.
Sorry, I don't know where those settings would be.
Once you do this, then you should be able to remove the call to setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
as the LAF will pick up the OS settings from the Locale.
EDIT
This Link describes how to enable right-to-left text on Windows 7. Then I think you would also need to change your locale.
It looks like the Component Orientation feature is not supported with the Windows LookAndFeel (at least not for the title bar)
Here is one posibility. This utility is designed for Mac users who have switched to Windows and want the window buttons on the left, but it should serve the same needs as yours.
This solution has nothing to do with Java (so I don't know if it's even acceptable for your needs) and sounds like it would be external to your application. I have not been able to try it out myself (I'm not running Windows), so I can't vouch for it, but it might be worth a try.
You only need to add this line of code and I am sure it works 100%.
frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
basically, I was wondering if there is any way of specifying the colours of the background and text in a Java console application. I am writing a console menu. Also, if there is any easy way of clearing the console.
Thanks.
You can use ANSI escape codes to specify the foreground and background colors of text for console apps on many platforms, assuming the terminal you're using to run the application supports ANSI mode. You don't need any additional libraries to use these codes, you can just embed them directly in your strings. However, since they're a bit messy looking, you can use a library such as JCurses to make it a bit easier to apply the various ANSI codes.
Here's an example program:
public static void main(String args[]) {
System.out.println((char)27+"[01;31m;This text is red."+(char)27+"[00;00m");
System.out.println((char)27+"[01;32m;This text is green."+(char)27+"[00;00m");
}
As a bonus, ANSI escape codes will help you with screen clearing and cursors positioning, as well.