在 JEditorPane/JTextArea 上显示和附加图标,每个图标都以换行符显示?
我正在尝试显示 JEditorpane 文件扩展名的系统图标,但只能显示最后一个图标?我想在换行符上附加并显示每个图标?
String fileList[] = {".pdf", ".txt", ".doc", ".exe"}
JLabel label;
FileSystemView fsv = FileSystemView.getFileSystemView();
icon = fsv.getSystemIcon(File.createTempFile("file.", "." + filetype[i]));
label = new JLabel(icon);
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setComponent(labelStyle, label);
try {
document.insertString(document.getLength(), "\n", labelStyle);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
myjEditorPane.setDocument(document);
I m trying to display System icons for file extention of JEditorpane i m able to display but only last icon get displayed ?I want to append and display each icon on newline?
String fileList[] = {".pdf", ".txt", ".doc", ".exe"}
JLabel label;
FileSystemView fsv = FileSystemView.getFileSystemView();
icon = fsv.getSystemIcon(File.createTempFile("file.", "." + filetype[i]));
label = new JLabel(icon);
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setComponent(labelStyle, label);
try {
document.insertString(document.getLength(), "\n", labelStyle);
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
myjEditorPane.setDocument(document);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的标签样式一次只能有一个图标的样式。将样式添加到文档时,不会保存样式。
如果您想使用不同的图标,请使用:
在这种情况下,执行语句时会将唯一的图标插入到文档中。
Your label style can only have a style for one icon at a time. The style is not saved at the time you do the addition of the style to the document.
If you want to use different icons then use:
In this case a unique icon is inserted into the document when the statement is executed.