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());
}
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);
}
So I've managed to insert a pdf file into my database using BLOBS.
now if I would like to retrieve the pdf file on another computer using the same database but on a computer that doesn't have the pdf file downloaded how can I do that?
I would like to have it so that it starts a download for the pdf when a button is pressed.
The code down below I use to insert files into the database.
btnBifogaFiler.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
file.setCurrentDirectory(new
File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new
FileNameExtensionFilter("*.Images", "jpg", "gif", "png", "pdf");
file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
selectedFile = file.getSelectedFile();
path = selectedFile.getAbsolutePath();
int i = path.lastIndexOf(".");
String nyPath = path.substring(i, i + 4);
lblBild.setText(nyPath);
s = path;
}
}
});
try {
PreparedStatement ps2 = connection.prepareStatement("INSERT INTO
FILER(FILID,FIL,TYP) VALUES (?,?,?)");
InputStream is = new FileInputStream(new File(s));
selectedFile = file.getSelectedFile();
path = selectedFile.getAbsolutePath();
extension = "";
int i = path.lastIndexOf(".");
extension = path.substring(i,i+4);
textLabel.setText(extension);
ps2.setInt(1,nyaVardet2);
ps2.setBlob(2, is);
ps2.setString(3,extension);
ps2.executeUpdate();
}
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 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