Java 在jfilechooser中包含图像缩略图
我有一个jfilechooser,它可以帮助搜索和选择要上传到项目数据库中的图像。还有一个thumbnailator类可以将上传的图像压缩到所需的大小。为运行文件选择器而执行的按钮操作的代码如下:Java 在jfilechooser中包含图像缩略图,java,swing,Java,Swing,我有一个jfilechooser,它可以帮助搜索和选择要上传到项目数据库中的图像。还有一个thumbnailator类可以将上传的图像压缩到所需的大小。为运行文件选择器而执行的按钮操作的代码如下: private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) { try{ String sql = "delete fro
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中包含图像缩略图???使用jfilechooser没有简单的方法。金属外观和手感非常有限。金属外观仅提供文件排列列表,它不使用Windows界面,因此您必须使用其他技术 在这种情况下,你可以通过一种黑客的方式来实现它。另一种解决方案是使用FileDialog而不是JFileChooser。FileDialog类使用当前操作系统的外观,使用其组件,而不是按照自己的标准进行排列,因此用户可以将其视为大图标(如果他/她愿意)。你可以找到它的文档。样本如下:
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
只是澄清一下,您想让您的用户查看他/她可以在Windows大图标视图中另存为的图像的所有选项吗?可能重复当fileChooser打开时,在您选择所需内容之前,所有文件都是列表格式的。我不希望文件是列表格式。这是文件的缩略图视图。使用filechooser的主要目的是上载图像。因此,我希望在选择图像文件之前,所有文件都不是列表格式,而是缩略图格式@jreznotChanges@AndrewThompson。感谢添加信息@RodneyNart别忘了检查答案是否正确如果它有效,这样会联系到更多有同样问题的人。当然@Pelicer有点忙,所以还没有尝试过。surley今天会这样做并给你反馈吗?请这样做。如果你想的话,我有一些可能对你有帮助的个人项目。那么这些项目是什么呢,兄弟。请给我你的电子邮件地址