Results 1 to 2 of 2
  1. #1
    jambon is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Question image does not refresh-driving me crazy

    Hello-
    I'm totally stumped on this.
    This may seem to be a question on JDBC but it's not.
    That part works fine.
    Below is a method which is invoked through the user clicking on a JComoBox item.
    The first time the user selects from the combo box the image comes up perfectly.
    Any further attempts appear to work but the image remains the same.

    Java Code:
        private void DisplayPhoto(int p){
            int pager=p;
            scalefactor=1.0;
            if(imageLabel!=null){
                
            imagePane.remove(imageLabel);
            imageScrollPane.remove(imagePane);
            imagePane.revalidate();
            imageScrollPane.revalidate();
            topPane.revalidate();
            viewerPane.revalidate();
            mainFrame.getContentPane().validate();
            }
            String sql = "Select * from photo order by id";
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                conn = DriverManager.getConnection(connectionURL, "user1", "f8H359MNa");
                Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
                ResultSet results = stmt.executeQuery(sql);
                // System.out.println("results = "+results.getBlob(2));
            for(int k=0;k<pager;k++){
            System.out.println("trying..."+k);
                results.next();
            }
                // BufferedReader fo = new BufferedReader((new InputStreamReader(results.getBinaryStream(2))));
                BLOB blob;
                String title="";
                blob = (BLOB)(results).getBlob(2);
                title=(results).getString(5);
                int w=(results).getInt(3);
                int h=(results).getInt(4);
                String tempfilename=mainPath+"/temp.jpg";
                File tfile = new File(tempfilename);
                if(tfile.exists())tfile.delete();
                curWidth = w;
                curHeight = h;
                int result=0;
                if(!tfile.exists());
                result=createTempFile(blob);
                System.out.println("title = "+title);
            if(result==1){
                imageicon= new ImageIcon(tempfilename);
                //imageicon=scale(imageicon.getImage(),scalefactor);
                PhotoLabel.setText(title);
                imageLabel = new JLabel(imageicon);
                imageLabel.setPreferredSize(new Dimension(w,h));
                imageLabel.setMinimumSize(new Dimension(w,h));
                imageLabel.setMaximumSize(new Dimension(w,h));
                imagePane = new JPanel();
                imagePane.setBackground(Color.red);
                imagePane.setPreferredSize(new Dimension(w,h));
                imagePane.setMinimumSize(new Dimension(w,h));
                imagePane.setMaximumSize(new Dimension(w,h));
                imagePane.add(imageLabel);
                imageScrollPane.setViewportView(imagePane);
                topPane.revalidate();
                viewerPane.revalidate();
                mainFrame.getContentPane().validate();
                results.close();
                stmt.close();
                conn.close();
            }
             // src.getWidth(mainFrame)         
                System.out.println("width = "+w+" height = "+h);
            } catch (SQLException sq) {
                System.err.println(sq.getMessage());
            } 
            catch (ClassNotFoundException cnf) {
                           System.out.println("Caught ClassNotFoundException " +
                                              cnf.getLocalizedMessage());
                       }
    
        }
            public static int createTempFile(BLOB blob)
          { 
         String tempfilename=mainPath+"/temp.jpg";
         File temp = new File(tempfilename);
         if(temp.exists())temp.delete();
            int success = -1;
         
            try
            {
              File              blobFile   = new File(tempfilename); 
              FileOutputStream  outStream  = new FileOutputStream(blobFile); 
              InputStream       inStream   = blob.getBinaryStream(); 
         
              int     length  = -1; 
              int     size    = blob.getBufferSize(); 
              byte[]  buffer  = new byte[size]; 
                blobFile.deleteOnExit();
                length = inStream.read(buffer);
              while (length!= -1) 
              { 
                outStream.write(buffer, 0, length); 
                outStream.flush(); 
                length = inStream.read(buffer);
              } 
                success = 1;
              inStream.close(); 
              outStream.close(); 
            }
            catch (Exception e)
            {
              e.printStackTrace();
              System.out.println("ERROR(createTempFile) Unable to export:"+tempfilename);
              success = 0;
            }
              return success;
         
          }
    I would greatly appreciate any insights anyone may care to offer.
    -Jambon

  2. #2
    jambon is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Hello All-
    I think I've fixed it.
    For anyone interested,Here is the code.
    Java Code:
            if(result==1){
                BufferedImage bufferedImage =null;
                try{
                bufferedImage = ImageIO.read( new File(tempfilename) );
                }catch(Exception e){
                    System.err.println(e.getMessage());
                    }
                if(bufferedImage!=null){
                imageicon= new ImageIcon(bufferedImage);
                PhotoLabel.setText(title);
                imageLabel.setIcon(imageicon);
                imageLabel.setPreferredSize(new Dimension(w,h));
                imageLabel.setMinimumSize(new Dimension(w,h));
                imageLabel.setMaximumSize(new Dimension(w,h));
                imagePane.setBackground(new Color(196, 241, 255));
                imagePane.setPreferredSize(new Dimension(w,h));
                imagePane.setMinimumSize(new Dimension(w,h));
                imagePane.setMaximumSize(new Dimension(w,h));
                imagePane.add(imageLabel);
                imageScrollPane.setViewportView(imagePane);
                topPane.revalidate();
                viewerPane.revalidate();
                mainFrame.getContentPane().validate();
                results.close();
                stmt.close();
                conn.close();
                }
            }
                System.out.println("width = "+w+" height = "+h);
            } catch (SQLException sq) {
                System.err.println(sq.getMessage());
            } 
            catch (ClassNotFoundException cnf) {
                           System.out.println("Caught ClassNotFoundException " +
                                              cnf.getLocalizedMessage());
                       }
    :D

Similar Threads

  1. A crazy gui match
    By amarenash23 in forum New To Java
    Replies: 8
    Last Post: 12-30-2009, 04:39 PM
  2. THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it
    By syntrax in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 05:27 AM
  3. Loop driving me loopy!!!!!
    By soc86 in forum New To Java
    Replies: 8
    Last Post: 01-16-2009, 02:00 AM
  4. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 04:23 PM
  5. Random Image on Refresh (FROM Folder not set number)
    By QuinnMal in forum Java Servlet
    Replies: 1
    Last Post: 07-06-2007, 08:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •