Results 1 to 3 of 3
  1. #1
    Jonte79's Avatar
    Jonte79 is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default Displaying a selected image with a JFileChooser into a textarea

    Hi,

    I'm current struggling to display a image in a text area that needs to be selected with a JFileChooser. I can manage to get the file browser to come up and select the file I wish to load but I can't seem to get the GUI working to display it in to a text area, i have tried several ways but none of them working. Any idea?
    I've shown my code below and any help would be great!

    Thanks
    Jonny

    code 1
    Java Code:
    ....
    b3.addActionListener(new openFile());
    ....
    code 2
    Java Code:
    class openFile implements ActionListener {
            
            public void actionPerformed(ActionEvent e){
                new showFile();
             }
      }
    
    class showFile extends JComponent {
            
          showFile(){
            JFileChooser fc = new JFileChooser(".");
            int resultat = fc.showOpenDialog(null);
               if(resultat == JFileChooser.APPROVE_OPTION) {
                  
                 //code for viewing image
                  }
                  else
                  {
                   middlecon.append("Cancelled");
                  }
            }
    
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Displaying a selected image with a JFileChooser into a textarea
    JTextArea is not designed to show images.
    You can show an image in a JLabel or draw it in a JPanel/JComponent.
    It is possible to show Images in a JTextPane but this is more advanced, ie, takes a lot more work.
    Java Code:
    public class Pseudo implements ActionListener {
        JFileChooser fileChooser;
        JLabel label = new JLabel();
    
        Pseudo() {
            instantiate and configure fileChooser
            you can use a FileFilter to show only image files
        }
    
        public void actionPerformed(ActionEvent e) {
            showDialog();
        }
    
        private void showDialog() {
            int retVal = fileChooser.showOpenDialog();
            if(retVal == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                // bmp, gif, jpg, png files okay
                BufferedImage image = ImageIO.read(file);
                // catch IOException
                label.setIcon(new ImageIcon(image));
                may need to revalidate label...
            }
        }
    
        public static void main(String[] args) {
           show label in JFrame along with a button to open dialog
           frame.add(new JScrollPane(label));  // center section
        }
    }

  3. #3
    bouchnak is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default java source code

    hi for all
    iwant to display an image in a textArea and then to encrypte this image with button click named crypter please can i get a help it's urgent and i have no time to wait please:eek::D

Similar Threads

  1. Displaying a georaster image using jsp
    By smart in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-27-2008, 11:45 AM
  2. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 07:01 AM
  3. Replies: 1
    Last Post: 11-13-2008, 08:43 AM
  4. A question about displaying an image...
    By SpaceY in forum New To Java
    Replies: 0
    Last Post: 08-24-2008, 07:50 PM
  5. Replies: 0
    Last Post: 03-10-2008, 03:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •