Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Image display problem on JFrame...Whats wrong......need help?????'

  1. #1
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Unhappy Image display problem on JFrame...Whats wrong......need help?????'

    I have created a project in netbeans'
    It has a main file (input_image.java).
    Using JFileChooser, it browses for an image file on computer, and displays it on the frame.....'
    '
    I want, whenever the mouse is rolled over the image, another JFrame (msgbox_full_image.java) should open displaying the larger preview of the same image....'
    And when mouse is exited from the image, tha JFrame showing larger preview should close...'
    '
    I have succeeded in displaying and closing the JFrame by using mouse events (mouseEntered and mouseExited)'
    '
    Now....the main problem'
    -----------The image is not displayed on the JFrame (msgbox_full_image.java)-----------'
    '
    '
    (Attachment of project also attached )'
    '
    ::::::::::CODE FOR input_image.java:::::::::::::::
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
     
    public class input_image extends javax.swing.JFrame
    {
        public static Icon full_image=null;                     //Image transferred to other class
        public input_image()
        {
            initComponents();
            setLocationRelativeTo(null);
        }
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            imagechooser = new javax.swing.JFileChooser();
            input_image_panel = new javax.swing.JPanel();
            imagescreen = new javax.swing.JLabel();
            seperator = new javax.swing.JSeparator();
            browse = new javax.swing.JButton();
            exit = new javax.swing.JButton();
            path = new javax.swing.JLabel();
            imagename = new javax.swing.JLabel();
            tester = new javax.swing.JLabel();
     
            imagechooser.setApproveButtonText("Select");
            imagechooser.setBackground(java.awt.Color.white);
            imagechooser.setCurrentDirectory(new java.io.File("C:\\"));
                imagechooser.setDialogTitle("Select Image from Disk");
                imagechooser.setToolTipText("");
                imagechooser.setMaximumSize(new java.awt.Dimension(512, 452));
                imagechooser.setMinimumSize(new java.awt.Dimension(400, 250));
                imagechooser.setPreferredSize(new java.awt.Dimension(512, 452));
     
                setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                setTitle("Select an image");
                setName(""); // NOI18N
                setUndecorated(true);
     
                input_image_panel.setBackground(new java.awt.Color(175, 175, 240));
                input_image_panel.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 2, 2, new java.awt.Color(0, 0, 0)));
                input_image_panel.setPreferredSize(new java.awt.Dimension(543, 345));
     
                imagescreen.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
                imagescreen.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseEntered(java.awt.event.MouseEvent evt) {
                        imagescreenMouseEntered(evt);
                    }
                    public void mouseExited(java.awt.event.MouseEvent evt) {
                        imagescreenMouseExited(evt);
                    }
                });
     
                browse.setMnemonic('B');
                browse.setText("Browse...");
                browse.setToolTipText("Click to Browse");
                browse.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
                browse.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        browseActionPerformed(evt);
                    }
                });
     
                exit.setMnemonic('E');
                exit.setText("Exit");
                exit.setToolTipText("Click to Exit");
                exit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
                exit.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        exitActionPerformed(evt);
                    }
                });
     
                imagename.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
     
                tester.setEnabled(false);
     
                javax.swing.GroupLayout input_image_panelLayout = new javax.swing.GroupLayout(input_image_panel);
                input_image_panel.setLayout(input_image_panelLayout);
                input_image_panelLayout.setHorizontalGroup(
                    input_image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(input_image_panelLayout.createSequentialGroup()
                        .addGroup(input_image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(input_image_panelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(seperator, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE))
                            .addGroup(input_image_panelLayout.createSequentialGroup()
                                .addGap(71, 71, 71)
                                .addComponent(tester)
                                .addGap(62, 62, 62)
                                .addGroup(input_image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(input_image_panelLayout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(imagename, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(imagescreen, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, input_image_panelLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(path, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(browse)
                        .addGap(63, 63, 63))
                    .addGroup(input_image_panelLayout.createSequentialGroup()
                        .addGap(242, 242, 242)
                        .addComponent(exit)
                        .addContainerGap(246, Short.MAX_VALUE))
                );
                input_image_panelLayout.setVerticalGroup(
                    input_image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(input_image_panelLayout.createSequentialGroup()
                        .addGroup(input_image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(input_image_panelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(imagescreen, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(imagename, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(input_image_panelLayout.createSequentialGroup()
                                .addGap(81, 81, 81)
                                .addComponent(tester)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(seperator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(input_image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(browse)
                            .addComponent(path, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addComponent(exit)
                        .addContainerGap(29, Short.MAX_VALUE))
                );
     
                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(input_image_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );
                layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(input_image_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );
     
                pack();
            }// </editor-fold>
     
        private void exitActionPerformed(java.awt.event.ActionEvent evt) {                                     
            System.exit(0);
        }                                    
     
        private void browseActionPerformed(java.awt.event.ActionEvent evt) {                                       
            FileReader reader = null;
            if(imagechooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
            {
                File get_image = imagechooser.getSelectedFile();
                try
                {
                    reader = new FileReader(get_image);
                }
                catch (FileNotFoundException ex)
                {
                    Logger.getLogger(input_image.class.getName()).log(Level.SEVERE, null, ex);
                }
                String value=get_image.toString();
                path.setText(value);
                ImageIcon icon = new ImageIcon(value);
                imagescreen.setIcon(icon);  
                if(!
                   (
                    (value.endsWith(".jpg"))
                    ||
                    (value.endsWith(".bmp"))
                    ||
                    (value.endsWith(".png"))
                    ||
                    (value.endsWith(".jpeg"))
                    ||
                    (value.endsWith(".JPG"))
                    ||
                    (value.endsWith(".BMP"))
                    ||
                    (value.endsWith(".PNG"))
                    ||
                    (value.endsWith(".JPEG"))
                   )
                  )
                {
                    msgbox_invalid_image.main(null);
                }
                if(imagechooser.getSelectedFile()!=null)
                {
                    imagename.setText(imagechooser.getSelectedFile().getName());
                }
                full_image=imagescreen.getIcon();           //Stored image in full_image
            }
        }                                      
        msgbox_full_image show_img=new msgbox_full_image(); // Created object of other class
        private void imagescreenMouseEntered(java.awt.event.MouseEvent evt) {                                         
           if(imagechooser.getSelectedFile()!=null)
           {
                show_img.setVisible(true);                  //Called object of other class
            }
        }                                        
     
        private void imagescreenMouseExited(java.awt.event.MouseEvent evt) {                                        
            if(imagechooser.getSelectedFile()!=null)
            {
               show_img.setVisible(false);                  //Called object of other class
            }
        }                                       
     
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new input_image().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton browse;
        private javax.swing.JButton exit;
        private javax.swing.JFileChooser imagechooser;
        private javax.swing.JLabel imagename;
        private javax.swing.JLabel imagescreen;
        private javax.swing.JPanel input_image_panel;
        private javax.swing.JLabel path;
        private javax.swing.JSeparator seperator;
        private javax.swing.JLabel tester;
        // End of variables declaration
    }

    ::::::::::CODE FOR msgbox_full_image.java:::::::::::::::
    import javax.swing.Icon;
     
    public class msgbox_full_image extends javax.swing.JFrame
    {
        public msgbox_full_image()
        {
            initComponents();
            Icon x_image=input_image.full_image;        //Image received
            full_image_screen.setIcon(x_image);
            int image_width=full_image_screen.getWidth();
            int image_height=full_image_screen.getHeight();
            full_image_panel.setBounds(0, 0, image_width, image_height);
        }
     
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            full_image_panel = new javax.swing.JPanel();
            full_image_screen = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            setName(""); // NOI18N
            setResizable(false);
            setUndecorated(true);
     
            full_image_panel.setBackground(new java.awt.Color(0, 0, 0));
     
            full_image_screen.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
     
            javax.swing.GroupLayout full_image_panelLayout = new javax.swing.GroupLayout(full_image_panel);
            full_image_panel.setLayout(full_image_panelLayout);
            full_image_panelLayout.setHorizontalGroup(
                full_image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(full_image_screen, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
            );
            full_image_panelLayout.setVerticalGroup(
                full_image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(full_image_screen, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(full_image_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(full_image_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
     
            pack();
        }// </editor-fold>                        
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
                @Override
            public void run()
            {
                        new msgbox_full_image().setVisible(true);
            }
        });
    }
        // Variables declaration - do not modify                     
        private javax.swing.JPanel full_image_panel;
        private javax.swing.JLabel full_image_screen;
        // End of variables declaration                   
    }

    ::::::::::CODE FOR i_image.java:::::::::::::::
     
    public class msgbox_invalid_image extends javax.swing.JFrame {
    public msgbox_invalid_image()
        {
            initComponents();
            setLocationRelativeTo(null);
        }
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            msg_panel = new javax.swing.JPanel();
            msg = new javax.swing.JLabel();
            dotextension = new javax.swing.JLabel();
            ok = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            msg_panel.setBackground(new java.awt.Color(175, 175, 255));
     
            msg.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            msg.setText("Please Select an Image File");
     
            dotextension.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            dotextension.setText("(.jpg , .png, .bmp etc.)");
     
            ok.setText("OK");
            ok.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    okActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout msg_panelLayout = new javax.swing.GroupLayout(msg_panel);
            msg_panel.setLayout(msg_panelLayout);
            msg_panelLayout.setHorizontalGroup(
                msg_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(msg_panelLayout.createSequentialGroup()
                    .addGap(76, 76, 76)
                    .addGroup(msg_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(msg_panelLayout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(dotextension))
                        .addComponent(msg))
                    .addContainerGap(59, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, msg_panelLayout.createSequentialGroup()
                    .addContainerGap(145, Short.MAX_VALUE)
                    .addComponent(ok)
                    .addGap(127, 127, 127))
            );
            msg_panelLayout.setVerticalGroup(
                msg_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(msg_panelLayout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addComponent(msg, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(dotextension, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(ok)
                    .addContainerGap(36, Short.MAX_VALUE))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(msg_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(msg_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
     
            pack();
        }// </editor-fold>
     
        private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
            dispose();
        }                                  
     
    public static void main(String args[])
        {
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(msgbox_invalid_image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(msgbox_invalid_image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(msgbox_invalid_image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(msgbox_invalid_image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
            java.awt.EventQueue.invokeLater(new Runnable()
            {
     
                @Override
                public void run()
                {
                    new msgbox_invalid_image().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JLabel dotextension;
        private javax.swing.JLabel msg;
        private javax.swing.JPanel msg_panel;
        private javax.swing.JButton ok;
        // End of variables declaration
    }
    '
    Thanx in advance...........'
    Attached Files Attached Files


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Image display problem on JFrame...Whats wrong......need help?????'

    Isn't one question on this site enough?

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Image display problem on JFrame...Whats wrong......need help?????'

    I've looked at your code and have a question: Where do you pass the image into the top-level window that you wish to display it in?

    Ah, I think I see what you're trying to do, and it won't work. You're trying to use a static variable to share data between classes, and not only is this a bad idea, it won't work as you're implementing it (as you're finding out).

    Instead, consider giving your image viewing class a method, say called setIcon(Icon icon), and in your mouse listener call the method passing in an ImageIcon of the image that you want displayed. then in the displaying class, show the icon in a JLabel.

    Also as a general rule you want to avoid using static variables except in certain circumstances (this isn't one), and instead deal with instance variables and methods as much as possible.
    Last edited by curmudgeon; October 21st, 2012 at 10:34 AM.

  4. #4
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Image display problem on JFrame...Whats wrong......need help?????'

    How can i call method and return icon?'

Similar Threads

  1. Replies: 16
    Last Post: September 17th, 2012, 12:59 AM
  2. Problem with import java.util.*; dont know whats wrong
    By TheLeader in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 3rd, 2012, 01:33 PM
  3. How to display a Rectangle object in JFrame (Problem).
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 08:22 PM
  4. Replies: 0
    Last Post: April 3rd, 2012, 09:57 AM
  5. variable might not be initialized problem. Can someone tell me whats wrong?
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 09:25 AM

Tags for this Thread