I am using JFileChooser to load image from the desktop into JTextArea but when I load image from PC, the software hangs.
Here is the code of OpenActionPerformed method of the file chooser.
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
// What to do with the file, e.g. display it in a TextArea
textarea.read( new FileReader( file.getAbsolutePath() ), null );
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
A JTextArea is for text not for images.
If you want to display an image then add an ImageIcon to a JLabel and add the label to the JFrame.
Read the section from the Swing tutorial on How to Use Icons for more information on reading images and displaying Icons.
Related
I am using FreeTTS voice in my project for tts.
I want to save the voice to an audio file using JFileChooser to any location which user wants. I want to add a button which saves the audio file.Till now i have a "Open File" which opens text file and writes it into the JTextArea and a "Save File" button which saves the text written in JTextArea to output file in txt format.I am using NetBeans for the project.
The screenshot of the java application
// Code for Saving Text File
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser saver = new JFileChooser("./");
int returnVal = saver.showSaveDialog(this);
File file = saver.getSelectedFile();
BufferedWriter writer = null;
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
writer = new BufferedWriter( new FileWriter(file.getAbsolutePath()+".txt"));
writer.write(jTextArea1.getText());
writer.close( );
JOptionPane.showMessageDialog(this, "The Message was Saved Successfully!",
"Success!", JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, "The Text could not be Saved!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
//Code tried for saving audio file but did not worked
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser saver = new JFileChooser("./");
int returnVal = saver.showSaveDialog(this);
File file = saver.getSelectedFile();
VoiceManager voiceManager = VoiceManager.getInstance();
Voice saveVoice = voiceManager.getVoice(VOICENAME);
saveVoice.allocate();
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try {
audioPlayer = new SingleFileAudioPlayer(new FileWriter( file.getAbsolutePath()+AudioFileFormat.Type.WAVE));
saveVoice.setAudioPlayer(audioPlayer);
saveVoice.speak(jTextArea1.getText());
saveVoice.deallocate();
audioPlayer.close();
JOptionPane.showMessageDialog(this, "The Audio was Saved Successfully!",
"Success!", JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, "The Text could not be Saved!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
Similarly the code for the Opening text file is there.
I don't have much reputation that why i am not able to add screenshot directly but i have added the link to it.
An object of SingleFileAudioPlayer set as the audio player for the freetts voice can be used to write to a file.
You can find the solution on this question: how can i store output voice to an audio file in freetts
Please, I need your help. Could you tell me how to implement a SWT SaveAs Dialog in the next code? I need that user can choose where he wants to save the chart. Thanks!
try {
File file = new File("mychart.png");
float calidad = 1;
ChartUtilities.saveChartAsJPEG(file, calidad, chart, 800, 600);
MessageDialog.openInformation(shell, "Save Chart", "The file has been saved");
} catch (IOException e1) {
e1.printStackTrace();
MessageDialog.openInformation(shell, "Save Chart", "Error saving file. Please try again...");
}
Use the SWT FileDialog - something like:
Shell shell = ... current shell
FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
fileDialog.setFilterExtensions(new String [] {"*.png", "*.*"});
fileDialog.setFilterPath(.... any default path you want ....);
String filePath = fileDialog.open();
// TODO check for null 'filePath' - user canceled the save
File file = new File(filePath);
ChartUtilities.saveChartAsPNG(file, calidad, chart, 800, 600);
How can I know the path of the picture that will be store in this button. Also, what type of picture will you recommend me to upload in this button ?
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fc = new JFileChooser();
int result = fc.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File file = fc.getSelectedFile();
btnNewButton.setIcon(new ImageIcon(ImageIO.read(file)));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
});
how can i know the path of the picture that will be store in this
button
This can be easily done by calling File.getPath() method:
File file = fc.getSelectedFile();
System.out.println(file.getPath());
Additionaly you can store this path in the button through JComponent.putClientProperty(Object key, Object value):
File file = fc.getSelectedFile();
btnNewButton.putClientProperty("imagepath", file.getPath());
what type of picture will you recommend me to upload in this button ?
It can be JPG, PNG, BMP, WBMP and GIF, as per javax.imageio package description. Be aware Java doesn't support ICO format natively: Adding image to JButton
I need to upload and display an image selected with the JFileChooser (i.e the user wants to set his/her profile picture) in a JFrame.. How should I do it?
Here is my code for choosing the file:
private void UploadImageActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChosser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChosser.getSelectedFile();
// What to do with the file
// I want code for this part
try {
//code that might create an exception
}
catch (Exception e1) {
e.printStackTrace();
}
}
}
I solved it myself. I chose an image and displayed in a JLabel.
Here is My code:
private void uploadImageActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser filechooser = new JFileChooser();
filechooser.setDialogTitle("Choose Your File");
filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// below code selects the file
int returnval = filechooser.showOpenDialog(this);
if (returnval == JFileChooser.APPROVE_OPTION)
{
File file = filechooser.getSelectedFile();
BufferedImage bi;
try {
// display the image in a Jlabel
bi = ImageIO.read(file);
jLabel1.setIcon(new ImageIcon(bi));
} catch(IOException e) {
e.printStackTrace(); // todo: implement proper error handeling
}
this.pack();
}
}
I have a JLabel inside which I have saved my ImageIcon like this:
ImageIcon imageIcon = sample.map(); // a map method create an ImageIcon object
imageLabel.setIcon(imageIcon);
imageLabel.setVisible(true);
Now I would like to save this ImageIcon object into a PNG file when clicking on the Save item menu.
private void imageActionPerformed(java.awt.event.ActionEvent evt) {
Icon pic = imageLabel.getIcon();
JFileChooser fileChooser = new JFileChooser("C:/");
fileChooser.setSelectedFile(file);
// this filter will allow just PNG extension
FileFilter filter = new MyCustomFilter2();
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File fileToSave = fileChooser.getSelectedFile();
}
else
{
System.out.println("File access cancelled by user.");
}
}
Yes I know that this code is wrong and some part is missing, I think I should somehow save my Icon object called pic into a File object. This is my assumption. How can I do it please?
Thanks for any help,
Michal.
Here is my source code
private void imageActionPerformed(java.awt.event.ActionEvent evt) {
try{
Icon image = imageLabel.getIcon();
BufferedImage bi = new BufferedImage(image.getIconWidth(),image.getIconHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
File file = new File("outputFile");
JFileChooser fileChooser = new JFileChooser("C:/");
fileChooser.setSelectedFile(file);
FileFilter filter = new MyCustomFilter2();
fileChooser.setFileFilter(filter);
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
ImageIO.write(bi, "PNG", file);
File fileToSave = fileChooser.getSelectedFile();
}
else
{
System.out.println("File access cancelled by user.");
}
}
catch(IOException e){
e.printStackTrace();
}
}
The File object returned by the JFileChooser just represents the location on disk where the user would like to save the file. After that you'll want to use ImageIO.write() to save the file to disk.
e.g.
ImageIO.write(image, "png", file);
If you have an Icon, I think you may need to convert that to a BufferedImage before you can save it.