0

Do programs written in Java that use components from Swing to build their GUI display exactly the same on all operating systems?

EDIT: let me ask the question this way: using Java is it possible to design a GUI that is guaranteed to display exactly the same on all platforms?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • Regarding visual appearance, you mean? – Timr Nov 18 '12 at 11:15
  • 7
    If you use the same look and feel, then they would display the same. But the default is that Swing uses the system look and feel to make the application look like a native application. –  Nov 18 '12 at 11:17
  • 1
    @a_horse_with_no_name you should post that as an answer – Abubakkar Nov 18 '12 at 11:20
  • @Timr yes. Someone told me they wrote a program in Java that doesn't appear the same in Linux as it appears in Windows. I find this hard to believe, unless the example is pathological and the program was designed to display different in Linux than in Windows. If the example isn't pathological how can it be possible that a Java GUI displays differently between operating systems, when it's not the OS but JVM that runs it? – Celeritas Nov 18 '12 at 11:23
  • @Celeritas: Different operating systems have different design philosophies and different UI conventions. The user has the best experience when all applications on his system have the same look&feel. A windows user prefers applications which look like all windows programs and a Gnome user prefers applications which look like all gnome programs. So by default the JVM uses a look&feel which follows the design principles of the platform it runs on. – Philipp Nov 18 '12 at 11:26

4 Answers4

4

Used correctly, common Swing components are pleasantly stable across platforms and L&Fs, but there are no guarantees. One should avoid the temptation to violate UI defaults gratuitously. Example of this abound, but common pitfalls regarding size are discussed here and here. Testing is mandatory, but widely available virtual machines mitigate the effort.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

Pretty much so. But using the UIManager you can determine a theme based on an operating system's theme.
Example:

UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
    try{
        //0-Swing, 1-Mac, 2-?, 3-Windows, 4-Old Windows
        UIManager.setLookAndFeel(looks[3].getClassName());
    }catch(Exception e){
        System.out.println(e.getMessage());
    }
Kevin Murani
  • 186
  • 1
  • 5
  • Ya thanks, I'm not counting the case when it *tries* to look different depending on the local environment. – Celeritas Nov 18 '12 at 11:24
2

yes they will as long as you use swing and no platform specific code.

That said: They will not feel 'natural' then. SWT is a tad better there

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
1

Saying shortly it will look differently by default, because swing uses system look & feel, but you can override it (http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html), in this case application will look the same on all platforms with the same look & feel, or you may disable some look & feel features like -Dswing.noxp=true

endryha
  • 7,166
  • 8
  • 40
  • 64