2

I want to create my own file with icon that inherit from JFrame icon, that I set it, in java and my own file use FileOutputStream and ObjectOutputStream

try {
    ObjectOutputStream oos;
    //I create own file with own extension in drive D:
    FileOutputStream fos = new FileOutputStream("D:/myFile.ckl");
    oos = new ObjectOutputStream(fos);
    //Write Document in JTextPane to File
    oos.writeObject(jTextPane.getStyledDocument());
    oos.close();
    fos.close();
} catch (Exception exp) {
    JOptionPane.showMessageDialog(null, "" + exp.getStackTrace());
}

Thank you in advance

Alex K
  • 22,315
  • 19
  • 108
  • 236
Eric
  • 248
  • 2
  • 8
  • 21

2 Answers2

4

@David is correct that the host platform owns the JFrame decorations, but you may be able to leverage the JInternalFrame icons, which typically recapitulate those of the platform. For example,

private static final Icon ICON = (Icon) UIManager.get("InternalFrame.closeIcon");

Other decorative defaults are enumerated here.

SSCCE:

enter image description here

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

/** @see http://stackoverflow.com/a/10360374/230513 */
public class InternalFrameIcons extends JPanel {

    public InternalFrameIcons() {
        this.setLayout(new GridLayout(0, 1, 5, 5));
        this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        this.add(createLabel("InternalFrame.closeIcon"));
        this.add(createLabel("InternalFrame.maximizeIcon"));
        this.add(createLabel("InternalFrame.minimizeIcon"));
    }

    private JLabel createLabel(String name) {
        Icon icon = (Icon) UIManager.get(name);
        JLabel label = new JLabel(name, icon, JLabel.CENTER);
        label.setHorizontalTextPosition(JLabel.CENTER);
        label.setVerticalTextPosition(JLabel.BOTTOM);
        return label;
    }

    private void display() {
        JFrame f = new JFrame("InternalFrameIcons");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new InternalFrameIcons().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thanks you... it is so useful for me too, but it is not what i need – Eric Apr 28 '12 at 04:49
  • Ah, sorry. Once you have a reference to the icon, you can render it to a `BufferedImage` and save it to a file , as shown [here](http://stackoverflow.com/a/5853992/230513). Most operating systems can take it from there. – trashgod Apr 28 '12 at 09:24
2

The Operating System chooses what icon a file is displayed with. It is your job to write data to the file and give it a file extension (in this case that's 'ckl') but it is up to the OS that the file ends up on as to what icon it is given.

It is possible to embed icons in some files (many executables often have their own icon) but ultimately it's still up to the OS.

David
  • 1,940
  • 3
  • 17
  • 30