【发布时间】: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