Why in my Java application am I seeing "strange" scrollbars? - java

In my Java application I have a window which holds a JTextArea within a JScrollPane with scrollbars policies set to AS_NEEDED.
As I run my application I see that JTextArea this way:
Why am I seeing the scrollbars with that cutaway knob (which doesn't reflect a "standard" representation like this)?
The Layout for the frame is GridBagLayout, and I'm on Mac OS X 10.8.2, should that matter.

This is based on the Look and Feel your app is using, and the limitations of Java's integration with the native OS layout components. The one in your screenshot looks like Nimbus.
Swing applications always custom-render the look and feel, and don't do a very good job of using the native OS widgets everywhere. The result is that you get weird looks that might be consistent the OS only some of the time, or only with certain layout components.
Welcome to developing cross-platform desktop apps in Java. :(

To attempt to get the system look and feel when your application starts you can do this:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassname());
} catch (Exception e) {
// Handle exception
}
This will set the look and feel to that of the system regardless of what you run it on.
And as mentioned, the default look and feel for your application appears to be Nimbus and not OSX's Aqua, which again can be fixed with he above snippet and you could (should you care to) offer a UI option to the user to change the look and feel of the application to whatever they chose.

You are with Nimbus LookAndFeel
http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html

Related

Java Genuine Windows Dialogs

I am making a Java application, until I realized that Java doesn't use the Genuine Windows dialog layout.
I used this to set the UI layout to the system default (in this case, Windows):
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
I realized that dialogs created using this look and feel is different from Windows default.
Compare these two dialogs (first is Windows, second is Java):
Am I wrong, or is there a way?
Doesn't look like OP is on SO anymore, but regardless, if your goal is to create an application with the same look and feel as native applications, you might consider using Eclipse SWT.
To achieve nearly the same look and feel as the top image, you can use the SWT MessageBox.
For example:
final MessageBox box = new MessageBox(shell, SWT.YES | SWT.NO | SWT.ICON_WARNING);
box.setText("Rename");
box.setMessage("If you change a file name extension, the file might become unusable.\n\nAre you sure you want to change it?");
box.open();
will create:
I'm not aware of a way to remove the X cancel button, but there may be a way.

Swing and GTK font

I have write a little GUI with java Swing. But i have some little problems with the default look and feel using com.sun.java.swing.plaf.gtk.GTKLookAndFeel
As you can see the menus and the buttons doesn't have the same font of the other application in my Desktop (In the picture Eclipse and Nautilus), I'm using GTK on Gnome2.
Meanwhile using the relative look and feel on windows there are not differences:
The font look identical.
How is it possible this? Which parameters can I modify to edit the font look on GTK?
You should let the System decide which L&F to use
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
In your above image you are using Ubuntu with Unity (I guess?), but Unity does not use GTK but Nux.
Another way to create a native Look and Feel is the using the SWT (Standard Widget Toolkit) that provides you with system dependent libraries for Windows, Linux and OSX, so that the graphical elements in your program use the actual native elements from those systems. But I have to warn you, it may not be that easy (and sometimes even weird and annoying :) ) to use, because you cannot use any Swing components. So maybe that's not worth the trouble – it's for you to decide.
Consider setting defaults for the fonts on components after setting your look and feel.
UIManager.put("Menu.font", new FontUIResource(your_desired_font));
UIManager.put("Button.font", new FontUIResource(your_desired_font));
Windows L&F is using by default new Font("Tahoma", Font.PLAIN, 11);
For GTK, set the font standard by UIManager.getLookAndFeelDefaults() does not work... then I found a hack via reflection that works, you can find it here in response:
https://stackoverflow.com/a/31345102/3757320

Changing Java program LookAndFeel doesn't affect

I'm trying to change LAF of my program in this way:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception ex) {
Logger.getLogger(MainWin.class.getName()).log(Level.SEVERE, null, ex);
}
But this doesn't affect and program still looks as METAL while this reports "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" that means it must be changed:
UIManager.getLookAndFeel().getClass().getName();
Changing to other LAFs has the same problem!
What's the problem?
Thanks
Do you set look and feel before you create your GUI? Because if your GUI is already created (even if not shown), you have to tell Swing that LAF was updated:
SwingUtilities.updateComponentTreeUI(frame);
GTKLookAndFeel only gets applied if the operating system is Linux.
Using GTKLookAndFeel for a application running on Windows does not change the Look and Feel.
It will still display the Metal Look and Feel.
Note: The GTK+ L&F will only run on UNIX or Linux systems with GTK+
2.2 or later installed, while the Windows L&F runs only on Windows
systems. Like the Java (Metal) L&F, the Motif L&F will run on any
platform.
More info on Modifying the Look and Feel

Swing UI does not have native OS look

I am building an application in java swing and I am using the following code to give the UI a native OS look
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
On a OS X, the look is fine, but on windows (XP and 7) the buttons look like this.
alt text http://img710.imageshack.us/img710/8735/buttonsoc.png
I have used this exact same code on other projects and it works fine. But in this particular project I get a completely different look.
I am using Java 1.6
Thanks in advance!
Are you possibly creating your GUI elements before actually setting the L&F? If you already created (e.g.) JButton instances and called methods on them, they allocate their UI peer - changes to the L&F after that won't affect the already created instances.
This would explain why it works on Mac (the L&F defaults to Mac on Apple's JVM IIRC), but not on Windows. You can test this quickly if you move setting the L&F directly into your main method as the very first call (this assuming your main class does NOT contain any statically initialized GUI instances of course).

java - adjusting jslider looks on windows laf

been trying this for a while now and can't seem to get it to work. I wrote a little GUI app that uses the OS's default Look And Feel. While I wrote it on linux, it is mainly intended to be used on Windows. The JSliders under linux are fine by me, but on windows the thumbs(sliders? I don't know the right word) become very narrow, and they stop displaying the value above the thumb, too. I thought I could get around this problem with something like this:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.getLookAndFeelDefaults().put("Slider.thumbWidth", 20);
But, obviously, it doesn't work. How should I do this? seems like it should be a trivial thing, and I already spent more time on it than I'll ever want to admit.
thanks a lot
Not all LAFs will use the UIManager properties.
I'm not aware of the "thumbWidth" property. Maybe you meant to use the "trackWidth"?
Here is a complete list of the UIManager Defaults.

Categories