Look and Feel without hard coded values - java

Right now I set the LaF like this: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); It works fine and gives the look and feel I want to have. Sadly at some point in my code, I have to set some icons manually, because the automatical setting of those doesn't work properly (sets wrong icon). I currently have:
if (node.getAllowsChildren()) {
setIcon(UIManager.getIcon("FileView.directoryIcon"));
}else {
setIcon(UIManager.getIcon("FileView.fileIcon"));
}
but it doesn't use the LaF icons and I have no idea, on how to find the icons that look like the LaF. So my question is, is there an easy way to get lets say the LaF folder icon?
Design I currently get:
And the design I want to have:

Try using the UIDefaults class to get the values:
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
Icon icon = (Icon)defaults.get(...);
Check out UIManager Defaults. This Icons change when the LAF is changed.

Related

Java Windows UIManager

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.

Is there a way to set underline to mnemonic character in native look and feel under Win 7?

My code:
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
fileMenu.setDisplayedMnemonicIndex(0);
Javadocs for AbstractButton.setDisplayedMnemonicIndex() say that
Not all look and feels may support this.
I set my look and feel to UIManager.getSystemLookAndFeelClassName()
And I don't see underline on mnemonic even when the index is explicitely set by setDisplayedMnemonicIndex() - under Windows 7 (only when I press ALT).
It works however if I do not set the look & feel and leave just default java theme.
Is there a way to achieve this? Is it caused by the settings of Windows ?
You can change the behaviour that the underline only appears when pressing the Alt key by setting a property with the UIManager
UIManager.getDefaults().put("Button.showMnemonics", Boolean.TRUE);
I think you need to do this before you display your first Swing component, but I'm not sure.
An underline will appear when you press the ALT key. I think it's intended as a visual feedback for user, to let them know that the underlined characters are now mapped to the corresponding keys as shortcuts.

how to create a color palette inside a composite (swt)

i want to add a color palette inside a composite (swt).
there is ColorPalette class but i don't know how to use it inside my composite.
it will be inside eclipse view plugin
There are two possibilities here:
1.Use ColorDialog which looks like this:
2.Have a look ath this related question which seems to be what you are searching for. It looks like this:
I have used sub classes of following class from Windows Builder Core when i was working on Custom designer tool.
http://dev.eclipse.org/svnroot/tools/org.eclipse.windowbuilder/trunk/org.eclipse.wb.core/src/org/eclipse/wb/internal/core/utils/ui/dialogs/color/AbstractColorsGridComposite.java
This might help you to figure out solution to your problem.
I'm using Windows Builder for Eclipse. Hopefully, this code can be useful.
// COLOR PALETTE
{
JColorChooser tcc = new JColorChooser();
getContentPane().add(tcc, BorderLayout.PAGE_START);
}
I used this code inside a JDialog window. The result was this:
Place the code snippet inside the JDialog constructor, after the initial code that setups the window.

Changing colors of JButtons of LookAndFeel

I am working on an Java application, and I'm setting the LookAndFeel. Everything of the LAF I like except the default background color of the JButtons. What's the best way to set that (and other properties) after I set the look and feel? I don't want to have to create my own look and feel that extends it... would love to just be able to change it after the line I set the LAF...
Found it myself shortly afterwards.
UIManager.getDefaults().put("Button.background",new Color(0xFFFFFF));

Changing the default appearance of netbeans GUI controls

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.

Categories

Resources