[英]javax.swing.UIManager.getIcon(Object key) returns null when String-keys are streamed from an ArrayList
What I want to achieve: I want to visualize some javax.swing.Icon
s from the javax.swing.UIManager
.我想什么来实现的:我希望显示一些
javax.swing.Icon
从S 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.在互联网上,我找到了
UIManager
-keys 的列表,它不仅会返回Icons
,还会返回Strings
, Colors
等等。 So in this step I want to filter the list of keys so only the Icon
-keys remain.所以在这一步中,我想过滤键列表,以便只保留
Icon
键。
My Approach: I copied a list of UIManager
keys into a textfile and included it as recource in my Java-Project.我的方法:我将
UIManager
键列表复制到文本文件中,并将其作为资源包含在我的 Java 项目中。 I successfully read the file so I split the file-content by lines and added them to an ArrayList
of Strings
.我成功读取了文件,因此我按行拆分了文件内容并将它们添加到
Strings
的ArrayList
中。 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...现在我想流式传输这个
ArrayList
的内容并通过UIManager.getIcon(Object key)
-Method 是否返回null
过滤键...
My Problem so far: the UIManager
always returns null
.到目前为止我的问题:
UIManager
总是返回null
。 I printed all the keys and the UIManager
result to the console ( see "Output / Test - stream keys" in my code ).我将所有键和
UIManager
结果打印到控制台(请参阅我的代码中的“输出/测试-流键” )。 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 ).当我将
String
附加到要打印到控制台的键时,会显示有趣的行为(请参阅代码中“输出/测试 - 流键”下的变量“后缀” )。 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.如果变量
suffix
不以“\\n”开头,则流中下面的print-Method只会打印suffix
,不再显示其他内容。 For example if I type String suffix = "test";
例如,如果我输入
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. .forEach(key->System.out.println(... + key + suffix);
只会打印“test” .forEach(key->System.out.println(... + key + suffix);
但是,这种行为不会出现在“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...来自“UIManagerKeys.txt”的片段:这里有一些用于测试和重现性目的的键......
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.
我想从 javax.swing.UIManager 可视化一些 javax.swing.Icons。
I copied a list of UIManager keys into a textfile
我将 UIManager 键列表复制到文本文件中
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:
您只需从 UIManager 获取所有属性并检查对象是否为图标:
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,
在互联网上我找到了一个 UIManager 键列表,
Check out: UIManager Defaults for a little graphical tool that displays all the properties.查看: UIManager Defaults一个显示所有属性的小图形工具。
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:
这是将文件的行读入 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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.