How do I get the default font for Swing JTabbedPane labels? - java

Does the text in Swing components have a default font? In particular, what about tab labels on JTabbedPanes?
I'm working on a mock-up of a GUI made with Swing and want it to blend it with a screen image I grabbed of a Swing app.

It depends on the Look and Feel. If it's an application you've written, get the values from UIManager.getDefaults().getFont("TabbedPane.font")

The UIManager Defaults shows what the values are for all properties for all components (including "TabbedPane.font").

Based on the answer of Reverend Gonzo, this piece of code lets you know what keys are in the UIDefaults. As the keys are self-explanatory, you know what key you can use. I had to know the key for the JTextField font, for example, and could only find it this way.
Set<Object> keys = UIManager.getDefaults().keySet();
for (Object key : keys) {
if (key instanceof String && ((String) key).contains("font")) {
System.out.println(key + "=" + UIManager.getDefaults().get(key));
}
}
If you're looking for a font, in your case, just cast the key to a String and check whether it contains the word "font". This way you narrow the set of keys you have potential interest for.
I got a list
Menu.font=...
TextField.font=...
RadioButtonMenuItem.font=...
ToolTip.font=...
TitledBorder.font=...
...
TabbedPane.font=...
...
And thus you would need to pick TabbedPane.font.

It may depend on the 'Look and Feel' you are using, but for me Swing's default font is
DejaVu Sans - Plain
For most components the font size defaults to around 12 or 13

It looks like it's Arial. That's what Identifont tells me and it looks right.

The Java GUI default font is "Helvetica", bold size 9, color gray.

Related

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.

Java Nimbus Button.foreground not working

I am using the Nimbus LAF on my application and I want to change all buttons foreground colors. I do this setting:
UIManager.put("Button.foreground", Color.WHITE);
But this is not working. Maybe it is because I should only use the primary and secondary Nimbus colors? Could anyone help me please?
Thanks a lot.
simple way
1) you can set Color once by put value to UIManager, then will be valid for whole (for example JLabel) instance
2) dynamically set and override UIManager repeatedly
most complex way
3) create own UIManager, for example by aephyr
EDIT:
< to avoiding to create own Painters /> maybe correct way could be use non_buggy and todays Custom Look and Feel

Show user that something is on from JButton

Okay, I'll admit - the title is not the most descriptive/helpful but I couldn't really think of a better way to put it, which is probably also why I couldn't find an answer when I was searching.
Basically, I'm making a basic MS Notepad like text editor and I want to add two JButton's to make a JTextArea's font Bold and Italic. I need someway of indicating whether the text is currently bold and/or italic like in MS Office programs where the background of the buttons are orange when text is bold, italic or underlined - only I can't seem to change the background of the button. I think this is due to the fact that I am using the operating system's look and feel, but that information still doesn't solve my problem.
So does anyone have any suggestions on how I can provide some feedback as to whether the text is bold and/or italic through the JButton like in Microsoft Office Word? Thanks in advance.
Hmm...sounds like you'll want to make use of the JToggleButton class.
You can set the background color of a JButton object with the method setBackground.
So:
JButtonName.setBackground(Color.ORANGE);
In addition to David's and mre's comments, JButtons and by extension JToggleButtons are not opaque by default, so the background is not painted. In addition to setting the background color, you also need:
jButtonName.setOpaque(true);

Set property for all child components

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. :)

getting TableCellEditor colors to match match look and feel

So I have custom CellEditors and CellRenderers and although I am doing
component.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
component.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
component.setOpaque(true);
in my getTableCellRendererCompoent, the colors only match on every OTHER row, since most of hte look and feels I've tried seem to alternate them. How can I pull the color values in a way that they'll match whatever the rest of the table looks like? I'd also really like to be able to make nice borders to match the renderers descended from DefaultTableCellRenderer.
I've tried to dissect the DefaultTableCellRenderer, and I got lost trying to track down this UI object. Do I just need the right properties to pull from the UIManager? A lead in the right direction will be much appreciated.
Thanks all, this site rocks.
Joshua
Yes, you should use the Swing palette colors. For example:
final Color tableBackground = javax.swing.UIManager.getDefaults().getColor("Table.background");
Here's the color key values for tables:
Table.background Table.dropLineColor
Table.dropLineShortColor
Table.focusCellBackground
Table.focusCellForeground
Table.foreground Table.gridColor
Table.selectionBackground
Table.selectionForeground
Table.sortIconColor
TableHeader.background
TableHeader.focusCellBackground
TableHeader.foreground
Alternatively, you could also use the system colors. For example:
Normal background: SystemColor.window
Selected background: SystemColor.textHighlight
Normal foreground: SystemColor.text
Selected foreground: SystemColor.textText

Categories

Resources