Display and append Icons on JEditorPane/JTextArea with every icon displayed in newline?

1.9k views Asked by At

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);
2

There are 2 answers

0
Dorjay On
   try{

        StyledDocument doc = jTextPane1.getStyledDocument();
            Style def =  StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE );
        Style regular = doc.addStyle( "regular", def );
        Icon icon ;
        FileSystemView fsv = FileSystemView.getFileSystemView();
        for(int i=0;i<3;i++ ){
        icon = fsv.getSystemIcon(File.createTempFile("myfile.",".pdf"));
        jTextPane1.insertIcon(icon);
        doc.insertString( doc.getLength(),"pdfBook"+i+"\n", regular );
        }

  }catch(Exception excep){
         System.out.println("Exception");
      }
0
camickr On

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:

textPane.insertIcon(...);

In this case a unique icon is inserted into the document when the statement is executed.