0

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());
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kamczatka
  • 151
  • 1
  • 2
  • 15
  • Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Mar 14 '17 at 23:28

1 Answers1

2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288