Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java GUI > why doesn't the...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 9686 of 9821
Post > Topic >>

why doesn't the ImageIcon appear in the JscrollPanel?

by Anatorian <anatoranato@[EMAIL PROTECTED] > May 4, 2008 at 03:06 PM

I wrote a simple Swing program. When the click a button and select a jpg
file, I want to show this picture in the JScollPanel, but it doesn't
appear. The source is here:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {    
                                    
    if(evt.getSource() == this.jButton3) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG
& GIF Images", "jpg", "gif");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
           
this.jTextField4.setText(chooser.getSelectedFile().getAbsolutePath());
            try{
                this.jScrollPane2.add(new ImagePanel(1,
chooser.getSelectedFile()));
            } catch(Exception e) {
                e.printStackTrace();
            }
            
        }
    }
}  

package auction.client;

im****t java.awt.Graphics;
im****t java.awt.Image;
im****t java.awt.geom.Point2D;
im****t java.io.File;
im****t java.io.IOException;
im****t java.util.HashSet;
im****t java.util.Set;
im****t java.util.logging.Level;
im****t java.util.logging.Logger;
im****t javax.imageio.ImageIO;
im****t javax.swing.ImageIcon;
im****t javax.swing.JPanel;

public class ImagePanel extends JPanel {
    //identifier
    private int ID;
    
    //on-screen position
    private Point2D.Double position;
    
    //imageIcon to paint on screen
    private ImageIcon imageIcon;
    
    //stores all ImagePanel children
    private Set panelChildren;
    
    //constructor initilizes position and image
    public ImagePanel(int identifier, String imageFileName) throws
IOException {
        super(null);//specify null layou
        this.imageIcon = this.createImageIcon(imageFileName);
        
        this.init(identifier);
    }
    
    public ImagePanel(int identifier, byte[] imageData) {
        super(null);
        this.imageIcon = this.createImageIcon(imageData);
        
        this.init(identifier);
    }
    
    public ImagePanel(int identifier, ImageIcon icon) {
        super(null);
        this.imageIcon = icon;
        
        this.init(identifier);
    }
    
    public ImagePanel(int identifier, File iconFile) throws IOException {
        super(null);
        this.imageIcon = this.createImageIcon(iconFile);
        
        this.init(identifier);
    }
    
    private void init(int identifier) {
        setOpaque(false);//make transparent
        
        // set unique identifier
        ID = identifier;
        
        // set location
        position = new Point2D.Double(0, 0);
        setLocation(0,0);
        
        Image image = imageIcon.getImage();
        setSize(image.getWidth(this), image.getHeight(this));
        
        //create Set to store Panel childre
        panelChildren = new HashSet();
    }
    
    private ImageIcon createImageIcon(String fileName) {
        try {
            return new ImageIcon(ImageIO.read(new File(fileName)));
        } catch (IOException ex) {
            Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE,
null, ex);
        }
        return null;
    }
    
    private ImageIcon createImageIcon(byte[] imageData) {
        return new ImageIcon(imageData);
    }
    
    private ImageIcon createImageIcon(File file) throws IOException {
        return new ImageIcon(ImageIO.read(file));
    }
    
    //paint Panel to scree
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        //if image is ready, paint it to screen
        imageIcon.paintIcon(this, g, 0, 0);
    }
    
    public void add(ImagePanel panel, int index) {
        panelChildren.add(panel);
        super.add(panel, index);
    }
    
    public void remove(ImagePanel panel) {
        panelChildren.remove(panel);
        super.remove(panel);
    }
    
    public void setIcon(ImageIcon icon) {
        this.imageIcon = icon;
    }

    public ImageIcon getImageIcon() {
        return imageIcon;
    }

    public Point2D.Double getPosition() {
        return position;
    }

    public void setPosition(double x, double y) {
        this.position.setLocation(x, y);
        this.setLocation((int) x, (int) y);
    }

    public int getID() {
        return ID;
    }
    
    public Set getChildren() {
        return panelChildren;
    }
    
}

Who can help me?
 




 5 Posts in Topic:
why doesn't the ImageIcon appear in the JscrollPanel?
Anatorian <anatoranato  2008-05-04 15:06:07 
Re: why doesn't the ImageIcon appear in the JscrollPanel?
Knute Johnson <nospam@  2008-05-04 10:39:09 
Re: why doesn't the ImageIcon appear in the JscrollPanel?
Anatorian <anatoranato  2008-05-05 09:22:57 
Re: why doesn't the ImageIcon appear in the JscrollPanel?
Roedy Green <see_websi  2008-05-08 19:22:01 
Re: why doesn't the ImageIcon appear in the JscrollPanel?
Roedy Green <see_websi  2008-05-15 12:42:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 23:13:01 CDT 2008.