首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在jfilechooser中包括图像缩略图

在jfilechooser中包括图像缩略图
EN

Stack Overflow用户
提问于 2018-08-21 14:35:54
回答 1查看 305关注 0票数 0

我有一个jfilechooser,它可以帮助搜索和选择上传到项目数据库的图像。还有一个拇指钉类,它将上传的图像压缩到所需的大小。下面是运行文件选择器的按钮action_performed的代码:

代码语言:javascript
运行
复制
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:
    }

此代码所做的是从“临时图像表”中删除图像,将压缩后的选定图像从文件回波器插入到“临时图像表”中,并在用户最终接受选定图像并永久保存到数据库之前,由jlable显示从计算机中选择的用于预览的图像。

但是当文件回波器打开时,我希望在用户选择之前,所有的图像文件都在缩略图视图中。

请问如何在jfilechooser中包括图像缩略图??

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 17:01:05

使用JFileChooser没有简单的方法可以做到这一点。金属的外观和感觉非常有限。金属外观和感觉只提供了一个列表,如文件的排列,它不使用Windows界面,所以你将不得不使用另一种技术。

在这个可能重复的问题中,您可以通过一种简单的方法来实现它。另一种解决方案是使用FileDialog而不是JFileChooser。FileDialog类使用当前操作系统的外观和感觉,使用其组件,而不是按照自己的标准对其进行排列,因此用户可以将其视为LargeIcon (如果他/她愿意)。您可以找到它的文档这里。样本如下:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51951090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档