Hi. I'm trying to write a program that, when ran, opens an undecorated JFrame, and shows a list of icons that when clicked on, run a program, or open a folder. I'm having trouble getting the actual Icon Image. When I try, it gives me one of the default Java icons that are shown when using the JFileChooser. Does anyone know how to obtain the image? Also, I'm not trying to read the .ICO file, but get it directly from each individual icon. Thanks for any tips/advice you guys have.

Here's the code that I do have

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Background extends JFrame implements KeyListener
{
    ArrayList<Icon> icons;
    
    int x, y = 0;
    int width = 500;
    int height = 150;
    
    JFileChooser chooser;
    
    public Background(){
        super("");
        this.setUndecorated(true);
        this.setPreferredSize(new Dimension(width, height));
        try{
            Robot r = new Robot();    
            x = (int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()-width)/2;
            y = (int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()-height)/2;

        }catch(Exception e){}
        this.setVisible(true);
        this.setLocation(x,y);
        this.setSize(this.getPreferredSize());
        
        this.addKeyListener(this);
        icons = new ArrayList<Icon>();
    }
    
    public void paint(Graphics g){
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0,0, width, height);
        if(icons.size()>0)icons.get(0).paintIcon(this, g, 25, 25); //paint the first icon grabbed
    }
    
    public void keyPressed(KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_F2){
            createFileChooser();
        }
    }
    public void keyReleased(KeyEvent e){ }
    public void keyTyped(KeyEvent e){ }
    
    public void createFileChooser(){
        chooser = new JFileChooser();
        chooser.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getActionCommand().equals("ApproveSelection")){
                    icons.add(chooser.getIcon((chooser.getSelectedFile())));
                    System.out.println(icons.size());
                    repaint();
                }
            }
        });
        chooser.showDialog(this, "Select");
        chooser.setVisible(true);
    }
    
    public static void main(String[] args){
        new Background();
    }
}

Thanks for the link! I think I found a site that talks about it pretty well (It was actually the Sun site, hahaha). As soon as I get home (Saturday =[ ) I'll put that to use!!! Thanks again!

This article has been dead for over six months. Start a new discussion instead.