Having trouble using Java Swing on Linux with setting Look and Feel and getting file type icons : learnprogramming

I am having two problems, both which I believe are somewhat related. Source code, terminal output, and screenshots of GUI are below. The Linux Java is OpenJDK and the Windows/Mac is Oracle in case that makes a difference. All help is much appreciated.

First, on Kubuntu version 20.04 (KDE), I get the Metal Look and Feel when setting the Look and Feel to the system default, yet it works properly on Windows, Mac, and Ubuntu 22.04 (GNOME). I looked at the Java API docs (https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html) and they say that systems without GTK+ (which I assume is the case for Kubuntu because it uses KDE instead of GNOME) should default to Motif look and feel, not Metal. However Kubuntu still defaults to Metal even though the terminal output shows both GTK+ and Motif Look and Feels are available. Why is Kubuntu doing this? I know how to parse text in the list of available Look and Feels to choose an appropriate Look and Feel but would like to avoid it if possible.

Platform Default Look and Feel
Solaris, Linux with GTK+ 2.2 or later GTK+
Other Solaris, Linux Motif
Windows Windows
Macintosh Macintosh

Second, on both Ubuntu and Kubuntu, I cannot obtain the file type icon for files (like the triangular squiggly line for PDF) but I can on Windows and Mac. They don’t show up both when browsing through JFileChooser and when requesting the icon for a file. I checked on Kubuntu 20.04 three programs known to be written in Java (Netbeans, IntelliJ, and Eclipse) and I found Eclipse was able to correctly show the icons in its file chooser and IntelliJ showed different but still present versions of file icons, indicating there must be some way to get the icon. How do I obtain the file type icon?

Source code:

public static void main(String[] args)
	{
		//Display system information
		String os_name = System.getProperty("os.name");
		String os_arch = System.getProperty("os.arch");
		String os_ver = System.getProperty("os.version");
		System.out.println("Current System: " + os_name + " " + os_arch + " " + os_ver);
		String java_name = System.getProperty("java.vm.name");
		String java_vendor = System.getProperty("java.vm.vendor");
		String java_ver = System.getProperty("java.vm.version");
		System.out.println("Current Java VM: " + java_name + " (" + java_vendor + ") " + java_ver);
		System.out.println();

		//initial Look and Feel
		LookAndFeel feel = UIManager.getLookAndFeel();
		System.out.println("Look and Feel initially loaded when Java starts");
		System.out.println("Name: " + feel.getName());
		System.out.println("ID: " + feel.getID());
		System.out.println("Description: " + feel.getDescription());
		System.out.println();
		
		//get a list of all the look and feels installed on this system
		UIManager.LookAndFeelInfo[] info1 = UIManager.getInstalledLookAndFeels();
		System.out.println("List of installed Look and Feels:");
		for (int i = 0; i < info1.length; i++)
		{
			System.out.println(info1[i]);
		}
		System.out.println();
		
		//set Look and feel to what the system says is the default Look and Feel.
		try
		{
			String systemLF = UIManager.getSystemLookAndFeelClassName();
			System.out.println("Default system look and feel: " + systemLF);
			System.out.println("Trying to set Default system look and feel");
			System.out.println();
			UIManager.setLookAndFeel(systemLF);			
		}
		catch(Exception e)
		{
			System.out.println("Error with setting default system Look and Feel");
			System.out.println();
		}

		//new system Look and Feel
		LookAndFeel feel2 = UIManager.getLookAndFeel();
		System.out.println("Look and Feel after trying to set default system Look and Feel");
		System.out.println("Name: " + feel2.getName());
		System.out.println("ID: " + feel2.getID());
		System.out.println("Description: " + feel2.getDescription());
		System.out.println();

		JFileChooser chooser = new JFileChooser();
		chooser.showOpenDialog(null);
		File file = chooser.getSelectedFile();
		//System.out.println(file.getAbsolutePath());
		/*Doesn't work on Mac
		Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);*/
		final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();       
		Icon icon = fc.getUI().getFileView(fc).getIcon(file); //get filetype icon for chosen file
		JOptionPane.showMessageDialog(null, "This is the file icon", "", JOptionPane.INFORMATION_MESSAGE, icon);
	}

Screenshots:

  • Kubuntu 20.04

  • Ubuntu 22.04

  • Windows

  • Mac

Console output:

Kubuntu 20.04:

Current System: Linux amd64 5.4.0-110-generic
Current Java VM: OpenJDK 64-Bit Server VM (Private Build) 11.0.15+10-Ubuntu-0ubuntu0.20.04.1

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[GTK+ com.sun.java.swing.plaf.gtk.GTKLookAndFeel]

Default system look and feel: javax.swing.plaf.metal.MetalLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

Ubuntu 22.04:

Current System: Linux amd64 5.15.0-27-generic
Current Java VM: OpenJDK 64-Bit Server VM (Private Build) 11.0.15+10-Ubuntu-0ubuntu0.22.04.1

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[GTK+ com.sun.java.swing.plaf.gtk.GTKLookAndFeel]

Default system look and feel: com.sun.java.swing.plaf.gtk.GTKLookAndFeel
Trying to set Default system look and feel

Gtk-Message: 22:12:59.057: Failed to load module "canberra-gtk-module"
Look and Feel after trying to set default system Look and Feel
Name: GTK look and feel
ID: GTK
Description: GTK look and feel

Windows Output:

Current System: Windows 10 amd64 10.0
Current Java VM: Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation) 15.0.1+9-18

Look and Feel initially loaded when Java starts
Name: Metal
ID: Metal
Description: The Java(tm) Look and Feel

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Windows com.sun.java.swing.plaf.windows.WindowsLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Windows Classic com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel]

Default system look and feel: com.sun.java.swing.plaf.windows.WindowsLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Windows
ID: Windows
Description: The Microsoft Windows Look and Feel

Mac Output:

Current System: Mac OS X x86_64 10.16
Current Java VM: Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation) 13.0.1+9

Look and Feel initially loaded when Java starts
Name: Mac OS X
ID: Aqua
Description: Aqua Look and Feel for Mac OS X

List of installed Look and Feels:
javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel]
javax.swing.UIManager$LookAndFeelInfo[Mac OS X com.apple.laf.AquaLookAndFeel]

Default system look and feel: com.apple.laf.AquaLookAndFeel
Trying to set Default system look and feel

Look and Feel after trying to set default system Look and Feel
Name: Mac OS X
ID: Aqua
Description: Aqua Look and Feel for Mac OS X

Leave a Comment