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
Related
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);
}
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
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);
}
I have a small requirement please help me
Firstly, I have a label, i set a icon to that label as
lbl_photo.setIcon(new javax.swing.ImageIcon(getClass().getResource(
"/images/photo.png")));
and i have a button browse to select the image.
private void btn_browseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"jpeg, gif and png files", "jpg", "gif", "png");
int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
image = chooser.getSelectedFile();
try {
BufferedImage originalImage = ImageIO.read(image);
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB
: originalImage.getType();
BufferedImage resizeImageJpg = resizeImage(originalImage, type);
photo = new ImageIcon(toImage(resizeImageJpg));
raster = resizeImageJpg.getRaster();
data = (DataBufferByte) raster.getDataBuffer();
} catch (Exception e) {
System.out.println(e.getMessage());
}
lbl_photo.setIcon(photo);
}
}
now, I am storing the selected image from browse button into database
Date date1 = new Date();
Timestamp timestamp1 = new Timestamp(date1.getTime());
String sql4 = "insert into std_photos values(?,?,?)";
pstmt5 = con.prepareStatement(sql4);
pstmt5.setInt(1, Integer.parseInt(txt_eno.getText()));
pstmt5.setString(2, "");
pstmt5.setTimestamp(3, timestamp1);
byte[] extractBytes = data.getData();
pstmt5.setBytes(2, extractBytes);
System.out.println(sql4);
image is successfully storing.but,if the user doesn't select the image through browse button the default jlabel icon should be store in the database.
please help me as early as possible
You are already checking the return value of chooser.showOpenDialog(this) for JFileChooser.APPROVE_OPTION.
In case you receive some other option or if the try-catch block reading the new image fails you could just get the old icon and write it into the database.
I currently access my .csv file via hardcoded ArrayList. I want to be able to select the file, instead of it being hard coded like it is currently.
I just added the JFileChooser. I cannot get get my rowData to read line by line of the file selected through JOptionPane. How do I do this?
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV Files", "csv");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
System.out.println("You chose to open file: " + chooser.getSelectedFile().getName());
}
my Original Code started here (without the commented line), and it works. I just don't want it hard coded in.
ArrayList<String> rowData = new ArrayList<String>();
FileConnections excelConn = new FileConnections();
//rowData = excelConn.read(chooser);
rowData = excelConn.read(new File("11738 IPACC INFINITY Unconfirmed OIVS Responses.csv"));
Try this (untested btw!):
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV Files", "csv");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
System.out.println("You chose to open file: " + chooser.getSelectedFile().getName());
ArrayList<String> rowData = new ArrayList<String>();
FileConnections excelConn = new FileConnections();
rowData = excelConn.read(chooser.getSelectedFile());
}
Is equivalent to:
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("CSV Files", "csv"));
if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open file: " + chooser.getSelectedFile().getName());
ArrayList<String> rowData = new FileConnections().read(chooser.getSelectedFile());
}