Kamczatka
Kamczatka

Reputation: 161

Swing: how get active JInternalFrame

I have problem with getting active JInternalFrame. I need to indicate an active ImageInternalFrame(my class, thats extend JInternalFrame) because I will perform some operations on it. I do not know how to solve it, can somebody help me?

Here is my code:

public class MainFrame extends javax.swing.JFrame {

    ArrayList <ImageInternalFrame> imageInternalFrameList = new ArrayList();


    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             

        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        String filePath = f.getPath();

        BufferedImage bufferedImage;
        ImageInternalFrame imageInternalFrame;

        String mimetype= new MimetypesFileTypeMap().getContentType(f);
        String type = mimetype.split("/")[0];
        if(type.equals("image")){
            bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(new File(filePath));
            } catch (IOException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

            imageInternalFrame = new ImageInternalFrame(filePath);

            imageInternalFrame.setSize( bufferedImage.getWidth(), bufferedImage.getHeight() );
            imageInternalFrame.setVisible(true);
            imageInternalFrame.setLocation(imageInternalFrameList.size() * 25 , imageInternalFrameList.size() * 25);
            add(imageInternalFrame);
            imageInternalFrameList.add(imageInternalFrame);
        }        
        else{ 
            JOptionPane.showMessageDialog(null, "It's NOT an image");
        }}

public class ImageInternalFrame extends javax.swing.JInternalFrame {

    public ImageInternalFrame( String imagePath ) {
        initComponents();

        setImage(imagePath);
    }  

    public void setImage(String imagePath){
        imageLabel.setIcon( new ImageIcon(imagePath) );
        imageLabel.paintImmediately(imageLabel.getVisibleRect());
    }
}

Upvotes: 0

Views: 731

Answers (1)

camickr
camickr

Reputation: 324098

You can use the getSelectedFrame() method of your JDesktop class.

imageLabel.setIcon( new ImageIcon(imagePath) );
imageLabel.paintImmediately(imageLabel.getVisibleRect());

Don't use paintImmeditately. Swing will automatically schedule the repainting of the label when you change the Icon.

Upvotes: 2

Related Questions