In the company where I work, we derived a class from sun.awt.WToolkit to change some of the colors by calling setDesktopProperty(). And that worked fine for years. But now in JDK 8, WToolkit is final and cannot be subclassed. The easy way out could be doing some nasty reflection and call the protected method, though I'm not sure that this won't yield a security exception or something similar.
The right way out is to change these colors through the Look and Feel. Oracle in Windows Desktop Property Support states that
Programs do not need to access these properties directly; the Windows look and feel will automatically read and interpret these properties to provide proper visuals and behavior for the components.
But it does not state anything about customizing these properties through LaF modifications and certainly doing UIManager.put("win.3d.shadowColor", Color.gray); as it's mentioned in this doc is ineffective.
So my question is, can Windows Desktop Properties be changed by subclassing an existing Look and Feel, or should I resort to some kind of a hack?
Swing’s Windows Look&Feel will import the Window-specific desktop properties into its defaults table, but within this table, the standard, LaF-independent names are used which are usually composed from the component’s name and the property.
E.g.:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException|InstantiationException
|IllegalAccessException|UnsupportedLookAndFeelException ex) {
Logger.getLogger(LaFColors.class.getName()).log(Level.SEVERE, null, ex);
System.exit(1);
}
UIManager.put("Panel.background", Color.YELLOW);
UIManager.put("Button.foreground", Color.BLUE);
JFrame frame=new JFrame("Test");
frame.getContentPane().add(new JButton("See, it’s still "
+UIManager.getLookAndFeel().getName()+" LaF"), BorderLayout.NORTH);
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Related
I'm attempting to use the UIManager in java to make all my pop up GUIs and error GUIs use the same colors. The issue is I can't seem to change the button color when using the windows style, as well I can't manage to change the GUIs title bar.
Code: Its rather simple, I'm calling UIManager.getLookAndFeelDefaults().put("Button.background", buttonColor) anda few other UI changes. Nothing major...
UIManager.getLookAndFeelDefaults().put("Button.background", buttonColor)
For the color of the buttons from JOptionPane dialogs I don't understand why it doesn't work. It should. Maybe a side effect. We don't see all your code.
Try this simple code, you should see only green buttons.
package swing.uimanager;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
public class JFrameWithUIManger extends JFrame {
public static void main(String[] args) {
new JFrameWithUIManger();
}
public JFrameWithUIManger() {
final UIDefaults lookAndFeelDefaults = UIManager.getLookAndFeelDefaults();
lookAndFeelDefaults.put("Button.background", Color.GREEN);
add(new JButton("A button"));
pack();
setVisible(true);
JOptionPane.showMessageDialog(this, "hello I am a message dialog");
JOptionPane.showConfirmDialog(this, "hello I am a question dialog", "do you like me ?", JOptionPane.YES_NO_OPTION);
}
}
For the white background in the title, you have a color (white), so it seems to work. Maybe, a problem in a key-value.
Post it please if you want we understand better.
Edit
After seeing you code, I understood your problem. It's the look and feel used.
You don't use the default look and feel (metal).
Probably, you don't know but all the look and feels in Swing don't born equals.
In deed, some options and mix-options are supported by some look and feels but are not supported by other look and feels.
If you use the default look and feel, you should have less compatibility problem.
The best way to know if it correctly supported is to report to some official docs but it's true that a lot of information about it is dry, with errors, and not detailed
In the link you posted in your comment : http://nadeausoftware.com/articles/2008/11/all_ui_defaults_names_common_java_look_and_feels_windows_mac_os_x_and_linux#Button, the keys displayed in the tables don't mean that values are present or modifiable but that only the keys are present in the LAF.
Each LAF is responsible to take into consideration or not to the key and the possibility to change values associated to the keys.
I tried with the Windows LAF to set only the background color button, it doesn't work either. So, it doesn't seem to be a side effect but it looks like more a missing of support on this feature for the Windows LAF.
Anyway, you can interest to the Nimbus if you want an official, nice and more recent and more flexible look and feel than the classic metal look and feel.
Nimbus document
It's available since the Java SE 6 Update 10.
I agree with you, Metal is ugly. But why don't use nimbus instead of the windows LAF ? It's great. I tried your code which modifies some values of lAF with Nimbus, it seems working nicely. Maybe, you could have some minor modifications to adjust it but the basis should be nice.
If you are using JOptionPane,
You an change the title using this format to use your own title.
JOptionPane.showMessageDialog(null, "This is the message", "This is the title", JOptionPane.YES_NO_OPTION);
To set back ground color of dialog-
UIManager UI=new UIManager();
UI.put("OptionPane.background", Color.white);
UI.put("Panel.background", Color.white);
JOptionPane.showMessageDialog(null,"Text","SetColor",JOptionPane.YES_NO_OPTION);
For changing buttons in dialog or everything else, create your own JDialog and set the button characteristics which you want.
Hey I'm new to netbeans and I noticed a lot of applications (from textbooks) have a default style/appearance to their controls (buttons etc) as shown below.
(source: iforce.co.nz)
.
the appearance when I'm creating a GUI is just the standard windows xp or 7 button style. Is there a way to change this to the style shown in the image above?
Here is the appearance I am currently getting:
(source: iforce.co.nz)
.
Thanks in advance.
Yes, you can give Swing a Windows like look and feel with the following code:
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e){
System.out.println("UIManager Exception : "+e);
}
NetBeans will automatically choose a Look and Feel depending on your JDK and operating system. NB generated some code to set the L&F when you created the JFrame which made everything look like Windows components. If you want to change the L&F, look at the source for your JFrame and look for a collapsed bit of code that says something like "Look and feel setting code." If you expand it you can change it as you like, or even delete it, which will cause it to simply use the default L&F ("Metal"), which is the one in your picture. Bear in mind that you really shouldn't really just delete generated code, but I'm just trying to make a point here. If you're new to swing in general, I'd recommend writing some applications by hand, and they should just use the "Metal" L&F by default. This will allow you to get comfortable with working with swing. See here for more information.
See the nested layout example for code that offers a combo containing the available PLAFs, and allows the user to change the PLAF at run-time.
You can add Look and Feels. There are some free great looking ones which can be downloaded freely. If you only want Windows look and feel you can just add
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e){
e.printStacktrace();
}
Hope this answers your question.
I have a problem with JTextField.requestFocus() behavior that appears to be different on Mac OS X.
Here is my situation: I have a dialog with a JList and a JTextField. The user is supposed to write a boolean expression in the text field, and the list contains the names of all the variables that might be entered in the expression. Because the user is expected to continue entering the expression after clicking on a variable from the list, the program helpfully calls JTextField.requestFocus(). This way you can e.g click "pvalue" from the list and then type " < 0.05" without the need to click on the textfield in between.
This all works fine on my development machine (Linux), but I got a bug report from a Mac user that clicking on the list actually selects all text in the text field, making it easy to accidentally overwrite what was entered before.
I suspected this is a problem with the Mac look-and-feel, after some searching it seems that indeed there is a "Quaqua.TextComponent.autoSelect" property for the mac look-and-feel that seems to be related to this problem: http://www.randelshofer.ch/quaqua/guide/jtextcomponent.html
My general question is:
Can you suggest a workaround for this problem?
In case that is too broad, an answer to these subquestions would already be a big help:
A possible solution could be to change the property "Quaqua.TextComponent.autoSelect". How do I do that?
I'm not even sure what "Quaqua" is. It looks like it is a customized look and feel. What is the default look and feel for Mac OS X? Does it have a property similar to Quaqua.TextComponent.autoSelect?
Is there a possibility to tweak look and feel for a single component instance only? If so, how?
Is it possible to set the Mac look and feel on my Linux development machine so that I can actually confirm this bug (all the above is really based on hunches and suspicions)? If so, how?
Seems this is a bug of Mac OS. JTextFields select their contents when they gain focus though keyboard tab cycling. If the insertion point is in the middle of the text, the insertion point will remain and the entire text will not be selected.
As a workaround you can override this behavior with the following, it works fine for me:
textfield.setCaret(new DefaultCaret()).
More details you can refer to this and this.
To modify the default behaviour, you can set the system property to false before initializing the UI components: System.setProperty("Quaqua.TextComponent.autoSelect", "false"); To modify a single component, you can use JTextField#putClientProperty("Quaqua.TextComponent.autoSelect", Boolean.FALSE);.
You can find other MacOS L&F specific properties here:
Quaqua Look & Feel - User Guide
A workaround might be (and I haven't tested this) to make the JList that inserts the variable names unfocusable. That way the focus will remain in the text field when you click on an item in the list. I'd recommend to use setRequestEnabled(false) on the JList, so that they are still focusable if you tab to them, but clicking them with the mouse will not focus them.
Sorry to add to an old question, but I just came across this problem and used the following code, which seems a little more complete than the previous example:
// JTextField linkedText
final int
startBefore = linkedText.getSelectionStart(),
endBefore = linkedText.getSelectionEnd();
linkedText.requestFocus(); // this was the original code line!
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
linkedText.setSelectionStart(startBefore);
linkedText.setSelectionEnd(endBefore);
}
});
This appears to protect the current cursor position or selection. (Note: This code must already run in the event dispatch thread, but you need invokeLater anyway or it doesn't work.)
I have an 'is Mac' function, so I did this inside a test for that, but it probably doesn't do any harm to do it on all platforms.
I noticed when looking through the JavaDocs that requestFocus() "is discouraged because its behavior is platform dependent." You should use requestFocusInWindow() instead and see if the same problem occurs with it.
requestFocusInWindow is part of the Focus subsystem, introduced in Java 1.4.
On a side note, the default Apple Look and Feel has at least one property in the apple.laf namespace: apple.laf.useScreenMenuBar
Edit: According to Sun, the Macintosh look and feel is only available on Macs.
While using requestFocusInWindow() is indeed encouraged over requestFocus(), it still produces the same problematic behavior on Macs (e.g., highlighting of full text field).
One workaround I got to work was to explicitly set the cursor position after requesting focus:
JTextField.requestFocusInWindow();
JTextField.setCaretPosition(JTextField.getDocument().getLength() - 1);
Note the "-1" is necessary, otherwise it will continue to highlight the entire field.
I'm curious to know if this solution is platform independent. Does this screw up the desired Linux or Windows behavior?
Mac will select the contents of the text field when the field gains focus. You can restore the state of the text field if you listen for the focus change event.
// JTextField linkedText
// Cache the state of the JTextField prior to requesting focus
final int
startBefore = linkedText.getSelectionStart(),
endBefore = linkedText.getSelectionEnd();
linkedText.requestFocus(); // this was the original code line!
// Use a focus listener to listen for the focus change and then
// reset the selected text to protect the cursor position
linkedText.addFocusListener ( new FocusListener()
{
public void focusGained( FocusEvent event ) {
linkedText.setSelectionStart( startBefore );
linkedText.setSelectionEnd( endBefore );
}
public void focusLost( FocusEvent event ) {
// do nothing
}
} );
Thank you for sharing your ideas. I had the same problem on my java application where on my windows system there wasn't a problem, but on my Mac OS X Yosemite I couldn't change the input. The focus wouldn't stay on the JTextField. Thanks to this thread I was able to fix my problem.
If you change the look and feel of the buttons and input boxes you maintain the focus and you can type again. The reset of the frame stays in the standard Mac OS look.
This is my code that I use in my java main methode. If you want to fix the problem past the try-catch code in your main methode.
public class Venster extends JFrame {
public static void main(String[] args) {
//Change L&F for mac
//Mac JTextField Bug Fix
try {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException e) {
System.out.println("L&F not supported" + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("Fout: " + e.getMessage());
} catch (InstantiationException e) {
System.out.println("Fout: " + e.getMessage());
} catch (IllegalAccessException e) {
System.out.println("Fout: " + e.getMessage());
}
//The app
JFrame frame = new JFrame();
frame.setSize(1000, 520);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("10 More Bullets by Frank Peters");
frame.setContentPane(new SpeelVeld());
frame.setVisible(true);
frame.setLocationRelativeTo(null); //start app in center
}
}
Soure:
http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
I've never used Java AWT before and now I've got a piece of code that displays a JFrame and sets the font property for all child components to the same value. I'd like to set the property in one place only. How can I do this?
In .NET/WinForms, child controls inherit from their parent controls, thus it would be enough to set the font for the Form instance to have it propagate to all controls. Unexpectedly, this doesn't seem to hold for AWT.
The following little code sets the font for all components recursively:
private void setFontForAll(JFrame f, java.awt.Font font) {
f.setFont(font);
setFontRecursive(f.getContentPane().getComponents(), font);
}
private static void setFontRecursive(Component[] components, java.awt.Font font) {
for (Component c : components) {
c.setFont(font);
if (c instanceof java.awt.Container)
setFontRecursive(((java.awt.Container)c).getComponents(), font);
}
}
However, it has four drawbacks:
Extra code, which might actually be quite inefficient for large forms with nested layout panels.
Code is non-generic. If I need to do the same for another property in future, I've got to rewrite the method (or refactor it to be more general at the expense of conciseness).
Usage is non-declarative, i.e. has to be called at the very end of the form creation (after all child components have been initialized and added) instead of anywhere in a declarative manner.
It doesn't work. Components are set correctly but not all things are components. For example, the TitledBorders of JPanels don't get set.
The UIManager class is the thing you need. Before you build your user interface simply tell it what fonts you want. Be warned though; there are a lot of font keys defined and if you want to change them all, you'll have to set them all.
UIManager.put( "Button.font", new Font( "Verdana", Font.BOLD, 12f );
UIManager.put( "Label.font", new Font( "Wingdings", Font.ITALIC, 12f );
// ...etc...
You can see the keys and values that are set by programmatically inspecting UIManager.getDefaults() which returns a hashtable.
For Swing you can also set the fonts with command-line arguments:
# java -Dswing.plaf.metal.controlFont=Georgia -Dswing.plaf.metal.userFont=Tahoma -jar foo.jar foo.Foo
Add -Dswing.aatext=true for anti-aliasing which makes the whole GUI look a lot nicer. :)
I am able to display Japanese characters everywhere except for the title bar of the main window (JFrame) in Java. Is there a way to change the font of this title bar so it can display japanese characters? Thanks
I am using Windows XP. If this matters I am using the Java Substance look and feel too.
A window's title bar is managed by the system window manager, not by Swing. You don't say what OS/GUI you're using.
For Windows XP, open the Display control panel, select the "Appearance" tab, and click the "Advanced" button; you can change the title font there (although the fonts installed on your system may not have the glyphs you need).
Here's some code that checks whether the system default font supports the glyph that you want (I have no idea what the character is; it's a nice-looking glyph from the Katakana set):
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class GlyphCheck
{
public static void main(String[] argv) throws Exception {
final String title = "Testing: \u30CD";
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel label = new JLabel(title);
label.setSize(200, 100);
frame.setContentPane(label);
frame.pack();
frame.setVisible(true);
}
});
}
}
JFrame.setDefaultLookAndFeelDecorated(true);
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
UIManager.put( "InternalFrame.titleFont", Resources.jaDefault.deriveFont(16.0f) );
Try it ;)
In order to override the font of the Frame you need to tell the look and feel to take care of its appearance. This may or may not be desirable, but you'll be at the mercy of the system otherwise. Some look and feels have quite good window decorations, others not so. Substance's are okay. Tell the UIManager what font to use also.
// Do this before you display any JFrame.
UIManager.put( "Frame.font", new Font( "Japanese", 12, Font.PLAIN ) );
JFrame.setDefaultLookAndFeelDecorated( true );
JFrame frame = new JFrame( title );
This approach (should it work - not tested it sorry!) will mean you'll be able to distribute your program without telling users that they need to change their Windows settings, as per the other answer.
I'm not familiar with Java Substance, but I experienced this when working on a webapp. Basically the Japanese, Chinese and Korean characters would show in the content in the page, but not in the browser title bar.
This is due to the fact that the windowing system controls this title bar, not the browser. Based on kdgregory's comment, it sounds like this is a similar situation to yours.
For the windowing system to understand the characters and not show the unsupported 'box' you have to ensure the proper character sets are installed. For Windows XP, the following steps resolved the problem with the browser title bar:
On the Windows Start menu, open the Control Panel.
Click the Regional and Language Options icon, and then click the Languages tab.
In the Supplemental languages support box, check the box for Install files for East Asian languages.
Click Apply and OK.