Question

good evening,

I have this part of code:

My problem is when I try to display the image of the BLOB Type. I am working with Netbeans IDE and apach derby database.

like this all things are well, but if I remove the line : JOptionPane.showMessageDialog(null, "hello"); the image apears and desapears fast,

String req = "select * from viewpiece where idpiece = "+idpiece;
        myConnection = MyConnection.getMyConnection() ;
        ResultSet rs = myConnection.executeSQL(req);  

        try {   
            if (rs.next()) {

                TextFieldValeur.setText(rs.getString(2));
                TextFieldMonnaie.setText(rs.getString(5));
                TextFieldPays.setText(rs.getString(20));

                Blob Rectoblob = rs.getBlob(21);
                byte[] RectoByte = Rectoblob.getBytes(1, (int)(Rectoblob.length()));
                 try {
                    rectoBufferedImage = ImageIO.read(new ByteArrayInputStream(RectoByte));
                    g = panelImageRecto.getGraphics();

                   JOptionPane.showMessageDialog(null,  "hello");
                   g.drawImage(rectoBufferedImage, 10, 20,175,175, null);

                } catch (IOException ex) {
                    Logger.getLogger(PieceDetaillee.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(rootPane, "Erreur recherche de details\n"+req);
        } 

where is the problem? and why that line of displaying message Hello is a solution?.

any help please? thank you very much.

Was it helpful?

Solution

Your problem is that you're using getGraphics() to get the Graphics context, and then drawing with it. This Graphics object is short-lived, and is not a safe way to draw an image in a Swing GUI. Much better would be to draw the image in a JPanel's paintComponent method using the Graphics object provided there for you by the JVM. Either that or display it as an ImageIcon in a JLabel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top