What I want to achieve:
I want to visualize some javax.swing.Icon
s from the javax.swing.UIManager
. On the internet I've found a list of UIManager
-keys, which will not only return Icons
but also Strings
, Colors
and so on. So in this step I want to filter the list of keys so only the Icon
-keys remain.
My Approach:
I copied a list of UIManager
keys into a textfile and included it as recource in my Java-Project. I successfully read the file so I split the file-content by lines and added them to an ArrayList
of Strings
. Now i wanted to stream the content of this ArrayList
and filter the keys by wether the UIManager.getIcon(Object key)
-Method returns null
or not...
My Problem
so far: the UIManager
always returns null
. I printed all the keys and the UIManager
result to the console (see "Output / Test - stream keys" in my code). If i manually copy a key from the console (one that I know should work) and paste it into the exact same piece of code, it actually works (see "Output / Test - single Key" in my code).
Interesting Behavior shows when I append a String
to the key that I want to print to the console (See the variable "suffix" under "Output / Test - stream Keys" in my code). If the variable suffix
does not start with "\n", the following print-Method in the stream will only print the suffix
and no longer show the other content. For example if I type String suffix = "test";
only "test" will be printed from the .forEach(key->System.out.println(... + key + suffix);
However, this behavior does not show up in the "Output / Test - single Key"-Example.
I have no idea, what is going on or if the (in my opinion) strange behavior as anything to do with the problem. I appreciate any kind of help!
Piece from "UIManagerKeys.txt": Here are some keys for testing and reproducibility purposes...
FileView.computerIcon
FileView.directoryIcon
FileView.fileIcon
FileView.floppyDriveIcon
FileView.hardDriveIcon
FormattedTextField.background
FormattedTextField.border
windowText
My Code:
package main;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.UIManager;
public class Main {
public Main() {
}
public static void main(String args[]) {
ArrayList<String> uiKeys = new ArrayList<>();
String fileName = "recources/UIManagerKeys.txt";
ClassLoader classLoader = new Main().getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
// Check: is File found?
System.out.println("File Found : " + file.exists());
try {
// Read File Content
String content = new String(Files.readAllBytes(file.toPath()));
// Split by line and collect
String[] keys = content.split("\n");
uiKeys.addAll(Arrays.asList(keys));
} catch (IOException e) {
System.out.println("Error: " + e);
}
// Output / Test - stream Keys
System.out.println("Total Number of Keys: " + uiKeys.size());
String suffix = ""; // Interesting behavior when String is not empty
uiKeys.stream()
.map(key -> key.replaceAll(" ", "").replaceAll("\n", "")) // Just to be sure
.forEach(key -> System.out.println("IconFound: " + (UIManager.getIcon(key) != null) + "\tKey: " + key + suffix));
// Output / Test - single Key
System.out.println("\n");
String key = "FileView.directoryIcon"; // Copied from console
System.out.println("IconFound: " + (UIManager.getIcon(key) != null) + "\tKey: " + key + suffix);
}
}
I want to visualize some javax.swing.Icons from the javax.swing.UIManager.
I copied a list of UIManager keys into a textfile
There is no need to create the text file. You just get all the properties from the UIManager and check if the Object is an Icon:
public static void main(String[] args) throws Exception
{
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
for ( Enumeration enumm = defaults.keys(); enumm.hasMoreElements(); )
{
Object key = enumm.nextElement();
Object value = defaults.get( key );
if (value instanceof Icon)
System.out.println( key );
}
}
On the internet I've found a list of UIManager-keys,
Check out: UIManager Defaults for a little graphical tool that displays all the properties.
Your approach of reading the files seems a little complicated. Here is a simple approach to reading the lines of a file into a ListArray:
import java.io.*;
import java.util.*;
public class ReadFile
{
public static void main(String [] args) throws Exception
{
ArrayList<String> lines = new ArrayList<String>();
BufferedReader in = new BufferedReader( new FileReader( "ReadFile.java" ) );
String line;
while((line = in.readLine()) != null)
{
lines.add(line);
}
in.close();
System.out.println(lines.size() + " lines read");
}
}