Обновление изображений меток значков с сохранением значков меток, которые не были изменены - PullRequest
0 голосов
/ 10 ноября 2019

В моем jframe 6 ярлыков с иконками изображений, рядом с другими текстовыми полями и датой. У меня проблема с кнопкой UPDATE, поскольку я не могу сохранить значок метки содержимого при обновлении чего-либо еще. Поэтому каждый раз, когда я запускаю запрос, он устанавливает все мои значки меток на Null.

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    try {
    int row = table_booking.getSelectedRow();
    String value = (table_booking.getModel().getValueAt(row, 0).toString());
    String sql = "UPDATE Booking SET BookingCode=?, PatientName = ?, PatientAge = ?, LowerAbdomen = ?, UpperAbdomen = ?,"
            + " Waist = ?, Hips = ?, Arm = ?, UpperLeg = ?, BMI = ?, VisceralFat = ?, FatMass = ?, Before = ?, "
            + "Before_right=?, Before_left=?, Before_back=?, FurtherInformation = ?, Weight = ?, Rarm =?, RupperLeg =?, "
            + "Height = ?, Date = ?, LowerAbdomenResult=?, Upper_AbdomenResult=?, WaistResult=?, HipsResult=?, ArmResult=?, "
            + "RarmResult=?, UpperLagResult=?, RupperLegResult=?, BMIResult=?, VisceralFatResult=?, FmassResult=?, "
            + "WeightResult=?, DateResult=?, After = ?, After_right=?, After_left=?, After_back=? "
            + "where BookingCode="+value;
    pst = conn.prepareStatement(sql);
            pst.setInt(1, Integer.parseInt(text_code.getText()));
            pst.setString(2, text_name.getText());
            pst.setString(3, text_age.getText());
            pst.setString(4, text_lowerAdomen.getText());
            pst.setString(5, text_upperAbdomen.getText());
            pst.setString(6, text_waist.getText());
            pst.setString(7, text_hips.getText());
            pst.setString(8, text_arm.getText());
            pst.setString(9, text_upperLeg.getText());
            pst.setString(10, text_BMI.getText());
            pst.setString(11, text_visceralFat.getText());
            pst.setString(12, text_fatMass.getText());

            pst.setBytes(13, BeforePhoto);

            pst.setBytes(14, BeforePhotoRight);
            pst.setBytes(15, BeforePhotoLeft);
            pst.setBytes(16, BeforePhotoBack);
            pst.setString(17, text_furtherInformation.getText());
            pst.setString(18, text_weight.getText());
            pst.setString(19, text_Rarm.getText());
            pst.setString(20, text_RupperLeg.getText());
            pst.setString(21, text_height.getText());
            pst.setString(22, ((JTextField)text_date.getDateEditor().getUiComponent()).getText());
            pst.setString(23, text_lowerAbdomenResult.getText());
            pst.setString(24, text_upperAbdomenResult.getText());
            pst.setString(25, text_waistResult.getText());
            pst.setString(26, text_hipsResult.getText());
            pst.setString(27, text_armResult.getText());
            pst.setString(28, text_RarmResult.getText());
            pst.setString(29, text_upperLegResult.getText());
            pst.setString(30, text_RupperLegResult.getText());
            pst.setString(31, text_BMIResult.getText());
            pst.setString(32, text_visceralFatResult.getText());
            pst.setString(33, text_fatMassResult.getText());
            pst.setString(34, text_weightResult.getText());
            pst.setString(35, ((JTextField)text_dateResult.getDateEditor().getUiComponent()).getText());
            pst.setBytes(36, AfterPhoto);
            pst.setBytes(37, AfterPhotoRight);
            pst.setBytes(38, AfterPhotoLeft);
            pst.setBytes(39, AfterPhotoBack);

    pst.executeUpdate();
    //pst.execute();
    JOptionPane.showMessageDialog(null, "Updated");
    }catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);            
        //e.printStackTrace();
    } finally {
        try {
            rs.close();
            pst.close();
        } catch(Exception e) {

        }
    }
   Update_Table();  
   FieldsCleaner();
}

Мне нужно только знать, как получить содержимое моей иконки jlabel, как я делаю это с другими текстовыми полями, чтобы я мог сохранитьэто когда я запускаю мой запрос на обновление, потому что, конечно, байтовые массивы BeforePhoto, BeforePhotoRight, BeforePhotoLeft, BeforePhotoBack все будут нулевыми в момент, когда я выполняю свой запрос на обновление.

ЭТО МОЯ функция JFileChooser:

private void cmd_insertBeforeActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    filename = f.getAbsolutePath();
    try {
        File image = new File(filename);
        FileInputStream fis = new FileInputStream(image);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        for(int readNum; (readNum=fis.read(buf))!=-1;){
        bos.write(buf,0,readNum);
        }
    BeforePhoto = bos.toByteArray();
    ImageIcon imageIcon = new ImageIcon(new ImageIcon(filename).getImage().getScaledInstance(label_before.getWidth(), label_before.getHeight(),Image.SCALE_SMOOTH));
    label_before.setIcon(imageIcon);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
       e.printStackTrace();

    } finally {
        try {
            rs.close();
            pst.close();
        } catch(Exception e) {

        }
    }

}

ЭТО МОЯ ФУНКЦИЯ СОХРАНЕНИЯ

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        String sql = "insert into Booking (BookingCode, PatientName, PatientAge, LowerAbdomen, UpperAbdomen, Waist, Hips, "
        + "Arm, UpperLeg, BMI, VisceralFat, FatMass, Before, Before_right, Before_left, Before_back, FurtherInformation, "
        + "Weight, Rarm, RupperLeg, Height, Date, LowerAbdomenResult, Upper_AbdomenResult, WaistResult, HipsResult, "
        + "ArmResult, RarmResult, UpperLagResult, RupperLegResult, BMIResult, VisceralFatResult, FmassResult, "
        + "WeightResult, DateResult, After, After_right, After_left, After_back) "
        + "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    pst = conn.prepareStatement(sql);

            pst.setInt(1, Integer.parseInt(text_code.getText()));
            pst.setString(2, text_name.getText());
            pst.setString(3, text_age.getText());
            pst.setString(4, text_lowerAdomen.getText());
            pst.setString(5, text_upperAbdomen.getText());
            pst.setString(6, text_waist.getText());
            pst.setString(7, text_hips.getText());
            pst.setString(8, text_arm.getText());
            pst.setString(9, text_upperLeg.getText());
            pst.setString(10, text_BMI.getText());
            pst.setString(11, text_visceralFat.getText());
            pst.setString(12, text_fatMass.getText());
            pst.setBytes(13, BeforePhoto);  
            pst.setBytes(14, BeforePhotoRight);
            pst.setBytes(15, BeforePhotoLeft);
            pst.setBytes(16, BeforePhotoBack);
            pst.setString(17, text_furtherInformation.getText());
            pst.setString(18, text_weight.getText());
            pst.setString(19, text_Rarm.getText());
            pst.setString(20, text_RupperLeg.getText());
            pst.setString(21, text_height.getText());
            pst.setString(22, ((JTextField)text_date.getDateEditor().getUiComponent()).getText());
            pst.setString(23, text_lowerAbdomenResult.getText());
            pst.setString(24, text_upperAbdomenResult.getText());
            pst.setString(25, text_waistResult.getText());
            pst.setString(26, text_hipsResult.getText());
            pst.setString(27, text_armResult.getText());
            pst.setString(28, text_RarmResult.getText());
            pst.setString(29, text_upperLegResult.getText());
            pst.setString(30, text_RupperLegResult.getText());
            pst.setString(31, text_BMIResult.getText());
            pst.setString(32, text_visceralFatResult.getText());
            pst.setString(33, text_fatMassResult.getText());
            pst.setString(34, text_weightResult.getText());

            if(text_dateResult.getDate() == null){
                pst.setString(35, ((JTextField)text_date.getDateEditor().getUiComponent()).getText());
            } else {
                pst.setString(35, ((JTextField)text_dateResult.getDateEditor().getUiComponent()).getText());
            }

            pst.setBytes(36, AfterPhoto);
            pst.setBytes(37, AfterPhotoRight);
            pst.setBytes(38, AfterPhotoLeft);
            pst.setBytes(39, AfterPhotoBack);

            pst.execute();
            JOptionPane.showMessageDialog(null, "Saved");
    }catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);

    } finally {
        try {
            rs.close();
            pst.close();
        } catch(Exception e) {

        }
    }
   Update_Table();
   FieldsCleaner();

}                                        

1 Ответ

0 голосов
/ 11 ноября 2019

БЛАГОДАРИМ, ЧТО ВЫ ПОЛУЧИЛИ НЕКОТОРЫЕ ОБРАТНЫЕ СВЯЗИ. Я ДАЛЬНЕЙШЕ ИССЛЕДОВАНИЯ И МОГЛ СДЕЛАТЬ РАБОТУ. ВСЕ, ЕСЛИ У ВАС ЕСТЬ ЛУЧШЕЕ РЕШЕНИЕ ДЛЯ ЭТОГО, ПОЖАЛУЙСТА, ПОДЕЛИТЕСЬ ...

ЗДЕСЬ РЕЗУЛЬТАТ, КОТОРЫЙ РАБОТАЕТ

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    try {
    int row = table_booking.getSelectedRow();
    String value = (table_booking.getModel().getValueAt(row, 0).toString());
    String sql = "UPDATE Booking SET BookingCode=?, PatientName = ?, PatientAge = ?, LowerAbdomen = ?, UpperAbdomen = ?,"
            + " Waist = ?, Hips = ?, Arm = ?, UpperLeg = ?, BMI = ?, VisceralFat = ?, FatMass = ?, Before = ?, "
            + "Before_right=?, Before_left=?, Before_back=?, FurtherInformation = ?, Weight = ?, Rarm =?, RupperLeg =?, "
            + "Height = ?, Date = ?, LowerAbdomenResult=?, Upper_AbdomenResult=?, WaistResult=?, HipsResult=?, ArmResult=?, "
            + "RarmResult=?, UpperLagResult=?, RupperLegResult=?, BMIResult=?, VisceralFatResult=?, FmassResult=?, "
            + "WeightResult=?, DateResult=?, After = ?, After_right=?, After_left=?, After_back=? "
            + "where BookingCode="+value;
    pst = conn.prepareStatement(sql);
            pst.setInt(1, Integer.parseInt(text_code.getText()));
            pst.setString(2, text_name.getText());
            pst.setString(3, text_age.getText());
            pst.setString(4, text_lowerAdomen.getText());
            pst.setString(5, text_upperAbdomen.getText());
            pst.setString(6, text_waist.getText());
            pst.setString(7, text_hips.getText());
            pst.setString(8, text_arm.getText());
            pst.setString(9, text_upperLeg.getText());
            pst.setString(10, text_BMI.getText());
            pst.setString(11, text_visceralFat.getText());
            pst.setString(12, text_fatMass.getText());

            if (label_before.getIcon() != null){
            try {
                Icon icons = label_before.getIcon();
                BufferedImage bi = new BufferedImage(icons.getIconWidth(), icons.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.createGraphics();
                icons.paintIcon(null, g, 0, 0);
                g.setColor(Color.WHITE);
                g.drawString(label_before.getText(), 10, 20);
                g.dispose();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", os);
                InputStream fis = new ByteArrayInputStream(os.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                }
                byte[] bytes = bos.toByteArray();
                BeforePhoto = bytes;
            } catch (IOException d) {
                JOptionPane.showMessageDialog(rootPane, d);
            }
            pst.setBytes(13, BeforePhoto);
            } else 
                pst.setBytes(13, BeforePhoto);

            if (label_before_right.getIcon() != null){
            try {
                Icon icons = label_before_right.getIcon();
                BufferedImage bi = new BufferedImage(icons.getIconWidth(), icons.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.createGraphics();
                icons.paintIcon(null, g, 0, 0);
                g.setColor(Color.WHITE);
                g.drawString(label_before_right.getText(), 10, 20);
                g.dispose();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", os);
                InputStream fis = new ByteArrayInputStream(os.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                }
                byte[] bytes = bos.toByteArray();
                BeforePhotoRight = bytes;
            } catch (IOException d) {
                JOptionPane.showMessageDialog(rootPane, d);
            }
            pst.setBytes(14, BeforePhotoRight);
            } else
                pst.setBytes(14, BeforePhotoRight);

            if (label_before_left.getIcon() != null){
            try {
                Icon icons = label_before_left.getIcon();
                BufferedImage bi = new BufferedImage(icons.getIconWidth(), icons.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.createGraphics();
                icons.paintIcon(null, g, 0, 0);
                g.setColor(Color.WHITE);
                g.drawString(label_before_left.getText(), 10, 20);
                g.dispose();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", os);
                InputStream fis = new ByteArrayInputStream(os.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                }
                byte[] bytes = bos.toByteArray();
                BeforePhotoLeft = bytes;
            } catch (IOException d) {
                JOptionPane.showMessageDialog(rootPane, d);
            }
            pst.setBytes(15, BeforePhotoLeft);
            } else
                pst.setBytes(15, BeforePhotoLeft);

            if (label_before_back.getIcon() != null){
            try {
                Icon icons = label_before_back.getIcon();
                BufferedImage bi = new BufferedImage(icons.getIconWidth(), icons.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.createGraphics();
                icons.paintIcon(null, g, 0, 0);
                g.setColor(Color.WHITE);
                g.drawString(label_before_back.getText(), 10, 20);
                g.dispose();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", os);
                InputStream fis = new ByteArrayInputStream(os.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                }
                byte[] bytes = bos.toByteArray();
                BeforePhotoBack = bytes;
            } catch (IOException d) {
                JOptionPane.showMessageDialog(rootPane, d);
            }                
            pst.setBytes(16, BeforePhotoBack);
            } else 
                pst.setBytes(16, BeforePhotoBack);

            pst.setString(17, text_furtherInformation.getText());
            pst.setString(18, text_weight.getText());
            pst.setString(19, text_Rarm.getText());
            pst.setString(20, text_RupperLeg.getText());
            pst.setString(21, text_height.getText());
            pst.setString(22, ((JTextField)text_date.getDateEditor().getUiComponent()).getText());
            pst.setString(23, text_lowerAbdomenResult.getText());
            pst.setString(24, text_upperAbdomenResult.getText());
            pst.setString(25, text_waistResult.getText());
            pst.setString(26, text_hipsResult.getText());
            pst.setString(27, text_armResult.getText());
            pst.setString(28, text_RarmResult.getText());
            pst.setString(29, text_upperLegResult.getText());
            pst.setString(30, text_RupperLegResult.getText());
            pst.setString(31, text_BMIResult.getText());
            pst.setString(32, text_visceralFatResult.getText());
            pst.setString(33, text_fatMassResult.getText());
            pst.setString(34, text_weightResult.getText());
            pst.setString(35, ((JTextField)text_dateResult.getDateEditor().getUiComponent()).getText());

            if (label_after.getIcon() != null) {
            try {
                Icon icons = label_after.getIcon();
                BufferedImage bi = new BufferedImage(icons.getIconWidth(), icons.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.createGraphics();
                icons.paintIcon(null, g, 0, 0);
                g.setColor(Color.WHITE);
                g.drawString(label_after.getText(), 10, 20);
                g.dispose();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", os);
                InputStream fis = new ByteArrayInputStream(os.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                }
                byte[] bytes = bos.toByteArray();
                AfterPhoto = bytes;
            } catch (IOException d) {
                JOptionPane.showMessageDialog(rootPane, d);
            }
            pst.setBytes(36, AfterPhoto);
            } else
                pst.setBytes(36, AfterPhoto);

            if (label_after_right.getIcon() != null) {
            try {
                Icon icons = label_after_right.getIcon();
                BufferedImage bi = new BufferedImage(icons.getIconWidth(), icons.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.createGraphics();
                icons.paintIcon(null, g, 0, 0);
                g.setColor(Color.WHITE);
                g.drawString(label_after_right.getText(), 10, 20);
                g.dispose();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", os);
                InputStream fis = new ByteArrayInputStream(os.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                }
                byte[] bytes = bos.toByteArray();
                AfterPhotoRight = bytes;
            } catch (IOException d) {
                JOptionPane.showMessageDialog(rootPane, d);
            }
            pst.setBytes(37, AfterPhotoRight);
            } else
                pst.setBytes(37, AfterPhotoRight);

            if (label_after_left.getIcon() != null) {
            try {
                Icon icons = label_after_left.getIcon();
                BufferedImage bi = new BufferedImage(icons.getIconWidth(), icons.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.createGraphics();
                icons.paintIcon(null, g, 0, 0);
                g.setColor(Color.WHITE);
                g.drawString(label_after_left.getText(), 10, 20);
                g.dispose();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", os);
                InputStream fis = new ByteArrayInputStream(os.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                }
                byte[] bytes = bos.toByteArray();
                AfterPhotoLeft = bytes;
            } catch (IOException d) {
                JOptionPane.showMessageDialog(rootPane, d);
            }
            pst.setBytes(38, AfterPhotoLeft);
            } else
                pst.setBytes(38, AfterPhotoLeft);

            if (label_after_back.getIcon() != null) {
            try {
                Icon icons = label_after_back.getIcon();
                BufferedImage bi = new BufferedImage(icons.getIconWidth(), icons.getIconHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.createGraphics();
                icons.paintIcon(null, g, 0, 0);
                g.setColor(Color.WHITE);
                g.drawString(label_after_back.getText(), 10, 20);
                g.dispose();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", os);
                InputStream fis = new ByteArrayInputStream(os.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum);
                }
                byte[] bytes = bos.toByteArray();
                AfterPhotoBack = bytes;
            } catch (IOException d) {
                JOptionPane.showMessageDialog(rootPane, d);
            }
            pst.setBytes(39, AfterPhotoBack);
            } else
                pst.setBytes(39, AfterPhotoBack);

    pst.executeUpdate();
    //pst.execute();
    JOptionPane.showMessageDialog(null, "Updated");
    }catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);            
        //e.printStackTrace();
    } finally {
        try {
            rs.close();
            pst.close();
        } catch(Exception e) {

        }
    }
   Update_Table();  
   FieldsCleaner();
}                                         
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...