Java: open image on a jlabel - java

I have seen tons of questions about this thing but i just cant completely understand why it doesnt work.
I want to open an image using JFileChooser and then show it on the jLabel on the other jFrame. So why it doesnt work? What is so wrong about it?
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
Icon icon = fileopen.getIcon(file);
origin.jLabel1.setIcon(icon);}
By the way will it work for .bmp files, not only .jpg, .png and .gif?

You need to use ImageIcon. References can be found here: ImageIcon java Docs and Swing tutorial.
Here is the updated source:
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
ImageIcon icon = new ImageIcon(file.getPath());
jLabel1.setIcon(icon);
}

Related

JFileChooser not 'highlighting' and filtering documents

When I run the code below, filechooser opens fine, however all the documents are not "highlighted" and I am unable to select them without double clicking. Please see screenshot Adding a filter for only txt files also doesn't work.
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File(System.getProperty("user.home")));
fc.setDialogTitle("choose input file");
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES","txt");
fc.addChoosableFileFilter(filter);
int result = fc.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fc.getSelectedFile();
String path = selectedFile.getAbsolutePath();
String name = selectedFile.getName();
open.setText(name);
}

Show Dialog "Save To.."

I m searching why my show *.showDialog for a "save to" doesn't work.
It works correctly when i m launching it with my IDE Intellij-idea.
But when i want to start my app with the .jar file, the showDialog doesn't work.
It would be run when i click on "Valider".
This is my code:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."+File.separator));
int reponse = chooser.showDialog(chooser, "Enregistrer sous");
if(reponse == JFileChooser.APPROVE_OPTION) {
String fichier = chooser.getSelectedFile().toString();
document.save(fichier+".pdf");
}
Edit:
This is the new code:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int reponse = chooser.showDialog(chooser, "Enregistrer sous");
if(reponse == JFileChooser.APPROVE_OPTION) {
String fichier = chooser.getSelectedFile().toString();
document.save(fichier+".pdf");
}
Edit 14/03:
I have found my error:
"javax.imageio.IIOException: Can't read input file!"
I'm trying to resolve this one, it probably because the file path is uncorrectly defined.
This is my code to draw my image:
PDImageXObject pdImage = PDImageXObject.createFromFile("myImage.png", document);
contentStream.drawImage(pdImage, 480, 720);
Thanks

Show selected file

I want to show selected file that was chosen with JFileChooser on JTextarea in JFrame, like this:
JTextArea textArea = new JTextArea(6, 12);
contentPane.add(textArea);
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogTitle("XML Datei auswählen");
int ret = fileChooser.showDialog( fileChooser, "auswählen");
if (ret == JFileChooser.APPROVE_OPTION) {
file3 = fileChooser.getSelectedFile().getAbsoluteFile();
textArea.setText(file3.getName());
but it shows me nothing on textarea, what am I doing wrong here?
Try this method
int ret = fileChooser.showOpenDialog(this);
if (ret == JFileChooser.APPROVE_OPTION) {
String file3 = fileChooser.getSelectedFile().getAbsolutePath();
textArea.setText(file3);
yes is simple, possible to replace, change
textArea.setText(file3.getName());
with
JTextArea.read(Reader in, Object desc) throws IOException

Saving png image with Java using JFileChooser

I am trying to save an image that is formed with a BufferedImage. I get the BufferedImage by doing
(BufferedImage) fg;
fg is an image of my jPanel's graphics. I am successful at saving the image by hardcoding the path in directly as follows:
ImageIO.write((BufferedImage)fg,"png",new File("C:\\Users\\Geiger\\Documents\\test.png"));
But when I attempt to add the JFileChooser to the mix the image that is saved comes up being blank with nothing but the jPanel's background color.
My code for my attempt at utilizing the JFileChooser is as follows:
JFileChooser jfc = new JFileChooser();
int retVal = jfc.showSaveDialog(null);
if(retVal==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String test = f.getAbsolutePath();
ImageIO.write((BufferedImage)fg,"png",new File(test));
}
EDIT:To clarify on the issue a little bit more:
The issue is not that a file doesn't appear its that the graphics don't appear on the image when using the JFileChooser object.
I update my image when the JFrame has a mouse presses event:
fg = jPanel2.createImage(jPanel2.getWidth(), jPanel2.getHeight());
try to put this line of code I think that's what you need:
ImageIO.write(buffer, "png", fileDialog.getSelectedFile());
Hope that helps
Graphics2D graphics2D = image.createGraphics();
scribblePane.paint(graphics2D);
Use these two lines of code to add graphics.
That's works for me
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("*.png", "png"));
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
ImageIO.write((BufferedImage) image, "png", new File(file.getAbsolutePath()));
} catch (IOException ex) {
System.out.println("Failed to save image!");
}
} else {
System.out.println("No file choosen!");
}
}

image upload-showing image on a form

I am using netbeans6.7.1 and phpmyadmin for my db to develop a java application
to manage students records
i want to upload students photos through browsing by clicking a browse buton which i have
included in my interface
I mean when i click on that button a JFilechooser pops up which filter only images(i have acomplished this)
what i need is when i click on the "Attach button" of the JFilechooser, i want the image i chose to be attached to a jtextArea on the form i'm working with and the JFilechooser be diposed off.
Also how i can save this form together with the image to a database table
Is there a place where i can find a good guide/tutorial about that
JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);
jButton1.addActionListener(this);
if(e.getSource()==jButton1)
{
int i = chooser.showOpenDialog(jPanel1);
if(i==JFileChooser.APPROVE_OPTION)
{
jPanel2.removeAll();
jPanel2.repaint();
File image = chooser.getSelectedFile();
ImageIcon photo = new ImageIcon(image.getAbsolutePath());
//jPanel2.add(new JLabel(photo));
JLabel label=new JLabel("",photo,JLabel.CENTER);
jPanel2.add(label,BorderLayout.CENTER);
jPanel2.repaint();// sets a default image in image field.
jPanel2.revalidate();
}
}
Note:You should set borderlayout for jpanel2
and the selected image size must be the size of jpanel2

Categories

Resources