简体   繁体   English

javax.swing.UIManager.getIcon(Object key) 从 ArrayList 流式传输字符串键时返回 null

[英]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 ,还会返回StringsColors等等。 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 .我成功读取了文件,因此我按行拆分了文件内容并将它们添加到StringsArrayList中。 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.

相关问题 NullPointerException:无法调用“Object.toString()”,因为“javax.swing.JButton.getIcon()”的返回值为 null - NullPointerException: Cannot invoke "Object.toString()" because the return value of "javax.swing.JButton.getIcon()" is null Swing UIManager.getColor()键 - Swing UIManager.getColor() keys 从ArrayList获取字符串返回null - Getting a String from an ArrayList returns null HashMap(key: String, value: ArrayList) 返回一个 Object 而不是 ArrayList? - HashMap(key: String, value: ArrayList) returns an Object instead of ArrayList? UIManager.getColor有时返回null - UIManager.getColor returns null sometimes 如何将Swing组件和对象添加到arraylist中的swing组件中? - How to add a Swing component from and object into a swing component in an arraylist? ArrayList中的JComboBox <String> -无法正常工作-Java Swing - JComboBox from ArrayList<String> - Not working - Java Swing 如何更改UIManager.getIcon(“ OptionPane.errorIcon)的图标大小? - How to change icon size of UIManager.getIcon("OptionPane.errorIcon)? 如果不匹配,则从ArrayList进行流传输将返回null错误 - Streaming from ArrayList returns null error if no match 使用 arraylist 提取数据时,尝试在 null object 引用上调用虚拟方法“java.lang.String” - Attempt to invoke virtual method 'java.lang.String' on a null object reference when extracting data using from an arraylist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM