troubleshooting Question

cant solve error adding to JPanel - cant see anything wrong with code

Avatar of ellandrd
ellandrdFlag for Ireland asked on
Java
9 Comments1 Solution208 ViewsLast Modified:
package ColorClustering;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ColourClusteringDemo extends JPanel
{
    private JFileChooser fileChooser;
    private ImageModel imageModel;
   
    public ColourClusteringDemo()
    {
        super();  
       
        fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory( new File( System.getProperty( "user.dir" ) ) );
       
        imageModel = new ImageModel( 640, 480 );
       
        this.setLayout( new BorderLayout() );
        this.add( imageModel, BorderLayout.CENTER );  <<<<<<<<<<<<<<<< getting red error here
       
        try
        {
            if ( fileChooser.showOpenDialog( this ) == JFileChooser.APPROVE_OPTION )
            {
               imageModel.setImage( ImageIO.read( fileChooser.getSelectedFile() ) );
            }
        }
        catch ( IOException e )
        {
            System.out.println( "Error reading image file: " + fileChooser.getSelectedFile() );
            System.out.println( e.getStackTrace() );
           
            JOptionPane.showMessageDialog( this, "Error loading image file: " +
                    fileChooser.getSelectedFile() + "\n" +
                    e.getMessage(), "Load error", JOptionPane.ERROR_MESSAGE );
           
            imageModel.resetImage();
        }
    }
   
    public static void main( String[] args )
    {
        ColourClusteringDemo ccd = new ColourClusteringDemo();
        JFrame frame = new JFrame();
        frame.add( ccd );
        frame.setTitle( "Colour Clustering Demo" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE  );
        frame.setLocationRelativeTo( null ) ;
        frame.setVisible( true );
        frame.pack();
    }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros