【问题标题】:include image thumbnails in jfilechooser在 jfilechooser 中包含图像缩略图
【发布时间】:2018-08-21 14:35:54
【问题描述】:

我有一个 jfilechooser,可以帮助搜索和选择要上传到项目数据库的图像。并且有一个缩略图器类可以将上传的图像压缩成所需的大小。用于运行文件选择器的按钮 action_performed 的代码如下:

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    try{
    String sql = "delete from TempImage";
    pst=con.prepareStatement(sql);
    pst.execute();
    }catch(SQLException | HeadlessException e){
    JOptionPane.showMessageDialog(null, e);
    }finally{
                try{
                    rs.close();
                    pst.close();
                }
                catch(Exception e){
                }
            }
        JFileChooser chooser =new JFileChooser();
        chooser.showOpenDialog(null);
        File f =chooser.getSelectedFile();
        filename=f.getAbsolutePath();
        image1.setText(filename);

        try{
            File imgs =new File(filename);
            BufferedImage bufferedimage=ImageIO.read(imgs);
            BufferedImage thumbnail=Thumbnails.of(bufferedimage)
            .size(125, 114)
            .asBufferedImage();
            ByteArrayOutputStream os = new  ByteArrayOutputStream();
            ImageIO.write(thumbnail,"jpeg", os);
            InputStream is=new ByteArrayInputStream(os.toByteArray());
            ByteArrayOutputStream bos = new  ByteArrayOutputStream();
            byte[] buf =new byte[1024];
            try{
                for(int readNum; (readNum=is.read(buf))!=-1;){
                    bos.write(buf,0,readNum);
                    System.out.println("Read" +readNum+ "bytes,");
                }
            }catch(IOException ex){
                Logger.getLogger(null);
            }
            person_image=bos.toByteArray();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }finally{
            try{
                rs.close();
                pst.close();
            }
            catch(Exception e){
            }
        }
        try{
        String sql="insert into TempImage(Image)values(?)";
        pst = con.prepareStatement(sql);
        pst.setBytes(1, person_image);
        pst.execute();
        }catch(SQLException | HeadlessException ep){
        JOptionPane.showMessageDialog(null,ep);
        }finally{
            try{
                rs.close();
                pst.close();
            }
            catch(Exception e){

            }
        } 
        try{
         String sql ="select Image from TempImage";
            pst=con.prepareStatement(sql);
            rs=pst.executeQuery();

            if(rs.next()){
                byte[] imagedata = rs.getBytes("Image");
                format =new ImageIcon(imagedata);
                image.setIcon(format);
            } }catch(SQLException | HeadlessException e){
            JOptionPane.showMessageDialog(null, e);
            }
       finally{
            try{
                rs.close();
                pst.close();
            }
            catch(Exception e){
            }
        }// TODO add your handling code here:
    }

此代码的作用是从“临时图像表”中删除图像,将文件选择器中选择的压缩图像插入“临时图像表”,然后在用户最终接受和将所选图像永久保存到数据库中。
但是当打开文件选择器时,我希望在用户选择他的选择之前所有图像文件都处于缩略图视图中。
请问如何在 jfilechooser 中包含图像缩略图??

【问题讨论】:

  • 只是澄清一下,您希望您的用户查看他/她可以在 Windows 大图标视图中保存的所有图像选项吗?
  • fileChooser 打开时,所有文件都是列表格式,然后再选择您想要的文件。我不希望文件采用列表格式。我先看一个文件的缩略图。使用文件选择器的主要目的是上传图片。所以我不喜欢所有文件都不是列表格式,而是在选择图像文件之前的缩略图视图@jreznot

标签: java swing


【解决方案1】:

使用 JFileChooser 没有简单的方法可以做到这一点。金属的外观和感觉非常有限。 Metal Look and Feel 仅提供文件排列之类的列表,它不使用 Windows 界面,因此您必须使用另一种技术。

就像在possible duplicated question 中一样,您可以通过hackish 方式实现它。另一种解决方案是使用 FileDialog 而不是 JFileChooser。 FileDialog 类使用当前操作系统的外观和感觉,使用它的组件而不是按照自己的标准排列它,因此如果他/她愿意,您的用户可以将其视为大图标。你可以找到它的文档here。示例如下:

FileDialog fileDialog = new FileDialog(yourJFrame, "Choose a file", FileDialog.LOAD);
fd.setDirectory("the directory you want the dialog to be opened in");
fd.setFile("*.desiredExtension");
fd.setVisible(true);
String filename = fd.getFile();
if (filename == null)
    //your user cancelled the choise
else
    //file chosen

【讨论】:

  • 已进行更改,@AndrewThompson。感谢您添加信息!
  • @RodneyNart 不要忘记检查答案是否正确,这样可以接触到更多有同样问题的人。
  • 确定@Pelicer 有点忙,所以还没有尝试过。萨利今天会这样做并给你反馈
  • 请这样做。如果你愿意,我有一些个人项目可能会对你有所帮助。
  • 那么他们是什么项目,兄弟。请给我您的电子邮件地址
猜你喜欢
  • 2011-05-05
  • 2010-11-13
  • 2012-01-27
  • 2020-06-24
  • 2012-05-18
  • 2014-07-23
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多