Vipul.Johri
Vipul.Johri

Reputation: 9

Can't load Image into JTextArea using Swing

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.");
}

Upvotes: 0

Views: 44

Answers (1)

camickr
camickr

Reputation: 324207

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.

Upvotes: 1

Related Questions