GUI- File Chooser

GUI- File Chooser

Page 1 of 1

4 Replies - 798 Views - Last Post: 02 February 2010 - 06:13 PM Rate Topic: -----

#1 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

GUI- File Chooser

Posted 02 February 2010 - 04:20 PM

I got stocked in Fike Chooser. It suppose to load the image to JFrame,
and than I need to work with an image. If it doesn't choose right extension (jpeg, gif, etc) htan JOption window pop=ups with message "WRONG FILE". Also when it loads to JFrame it has the button "save", so it should save in in the disk.
I've got lost in loading the image via File Chooser into JFrame. Please, HELP!!!

private JFileChooser fc;
......
private JButton getJButtonOpen()
  {
    if (JButtonOpen == null)
    {
      try
      {
        JButtonOpen = new JButton();
        JButtonOpen.setText("Open...");
        JButtonOpen.setEnabled(true);
        JButtonOpen.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(java.awt.event.ActionEvent e)
          {

//Set up the file chooser.
            if (fc == null) {
                fc = new JFileChooser();

    	    //Add a custom file filter and disable the default
    	    //(Accept All) file filter.
                fc.addChoosableFileFilter(new ImageFilter());
                fc.setAcceptAllFileFilterUsed(false);

    	    //Add custom icons for file types.
                fc.setFileView(new ImageFileView());

    	    //Add the preview pane.
                fc.setAccessory(new ImagePreview(fc));
            }

            //Show it.
            int returnVal = fc.showDialog(FileChooserDemo2.this,
                                          "Attach");

            //Process the results.
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                log.append("Attaching file: " + file.getName()
                           + "." + newline);
            } else {
                log.append("Attachment cancelled by user." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());

            //Reset the file chooser for the next time it's shown.
            fc.setSelectedFile(null);
            
          }
        });
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonOpen;
  }




Is This A Good Question/Topic? 0
  • +

Replies To: GUI- File Chooser

#2 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: GUI- File Chooser

Posted 02 February 2010 - 04:20 PM

I got stocked in Fike Chooser. It suppose to load the image to JFrame,
and than I need to work with an image. If it doesn't choose right extension (jpeg, gif, etc) htan JOption window pop=ups with message "WRONG FILE". Also when it loads to JFrame it has the button "save", so it should save in in the disk.
I've got lost in loading the image via File Chooser into JFrame. Please, HELP!!!

private JFileChooser fc;
......
private JButton getJButtonOpen()
  {
    if (JButtonOpen == null)
    {
      try
      {
        JButtonOpen = new JButton();
        JButtonOpen.setText("Open...");
        JButtonOpen.setEnabled(true);
        JButtonOpen.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(java.awt.event.ActionEvent e)
          {

//Set up the file chooser.
            if (fc == null) {
                fc = new JFileChooser();

    	    //Add a custom file filter and disable the default
    	    //(Accept All) file filter.
                fc.addChoosableFileFilter(new ImageFilter());
                fc.setAcceptAllFileFilterUsed(false);

    	    //Add custom icons for file types.
                fc.setFileView(new ImageFileView());

    	    //Add the preview pane.
                fc.setAccessory(new ImagePreview(fc));
            }

            //Show it.
            int returnVal = fc.showDialog(FileChooserDemo2.this,
                                          "Attach");

            //Process the results.
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                log.append("Attaching file: " + file.getName()
                           + "." + newline);
            } else {
                log.append("Attachment cancelled by user." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());

            //Reset the file chooser for the next time it's shown.
            fc.setSelectedFile(null);
            
          }
        });
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonOpen;
  }



Was This Post Helpful? 0
  • +
  • -

#3 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: GUI- File Chooser

Posted 02 February 2010 - 05:08 PM

I got stocked in File Chooser. It suppose to load the image to JFrame,
and than I need to work with an image. If it doesn't choose right extension (jpeg, gif, etc) than JOption window pop-ups with message "WRONG FILE". Also when it loads to JFrame it has the button "save", so it should save in in the disk.
I've got lost in loading the image via File Chooser into JFrame. Please, HELP!!!


private JFileChooser fc;
......
private JButton getJButtonOpen()
  {
    if (JButtonOpen == null)
    {
      try
      {
        JButtonOpen = new JButton();
        JButtonOpen.setText("Open...");
        JButtonOpen.setEnabled(true);
        JButtonOpen.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(java.awt.event.ActionEvent e)
          {

//Set up the file chooser.
            if (fc == null) {
                fc = new JFileChooser();

            //Add a custom file filter and disable the default
            //(Accept All) file filter.
                fc.addChoosableFileFilter(new ImageFilter());
                fc.setAcceptAllFileFilterUsed(false);

            //Add custom icons for file types.
                fc.setFileView(new ImageFileView());

            //Add the preview pane.
                fc.setAccessory(new ImagePreview(fc));
            }

            //Show it.
            int returnVal = fc.showDialog(FileChooserDemo2.this,
                                          "Attach");

            //Process the results.
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                log.append("Attaching file: " + file.getName()
                           + "." + newline);
            } else {
                log.append("Attachment cancelled by user." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());

            //Reset the file chooser for the next time it's shown.
            fc.setSelectedFile(null);
            
          }
        });
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonOpen;
  }





HERE IS THE FULL CODE OF GUI CLASS:
package snapshop.gui;

import java.awt.FlowLayout;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * @author Vera
 * 
 */
public class SnapShopGUI extends JFrame
{

  private static final long serialVersionUID = 1L;
  private JFileChooser fc;
  private JPanel JContentPane = null;
  private JButton JButtonEdgeDetect = null;
  private JButton JButtonEdgeHightlight = null;
  private JButton JButtonEdgeHorisontal = null;
  private JButton JButtonEdgeVertical = null;
  private JButton JButtonGrayscale = null;
  private JButton JButtonSharpen = null;
  private JButton JButtonSoften = null;
  private JButton JButtonOpen = null;
  private JButton JButtonSaveAs = null;
  private JButton JButtonUndo = null;

  /**
   * @throws HeadlessException
   */
  public SnapShopGUI() throws HeadlessException
  {
    // TODO Auto-generated constructor stub

    super();
    initialize();
  }

  /**
   * @param arg0
   */
  public SnapShopGUI(GraphicsConfiguration arg0)
  {
    super(arg0);
    // TODO Auto-generated constructor stub

    initialize();
  }

  /**
   * @param arg0
   * @throws HeadlessException
   */
  public SnapShopGUI(String arg0) throws HeadlessException
  {
    super(arg0);
    // TODO Auto-generated constructor stub

    initialize();
  }

  /**
   * @param arg0
   * @param arg1
   */
  public SnapShopGUI(String arg0, GraphicsConfiguration arg1)
  {
    super(arg0, arg1);
    // TODO Auto-generated constructor stub

    initialize();
  }

  /**
   * This method initializes JContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane()
  {
    if (JContentPane == null)
    {
      try
      {
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setAlignment(java.awt.FlowLayout.CENTER);
        JContentPane = new JPanel();
        JContentPane.setLayout(flowLayout);
        JContentPane.add(getJButtonEdgeDetect(), null);
        JContentPane.add(getJButtonEdgeHightlight(), null);
        JContentPane.add(getJButtonEdgeHorisontal(), null);
        JContentPane.add(getJButtonEdgeVertical(), null);
        JContentPane.add(getJButtonGrayscale(), null);
        JContentPane.add(getJButtonSharpen(), null);
        JContentPane.add(getJButtonSoften(), null);
        JContentPane.add(getJButtonOpen(), null);
        JContentPane.add(getJButtonSaveAs(), null);
        JContentPane.add(getJButtonUndo(), null);
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JContentPane;
  }

  /**
   * This method initializes JButtonEdgeDetect
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonEdgeDetect()
  {
    if (JButtonEdgeDetect == null)
    {
      try
      {
        JButtonEdgeDetect = new JButton();
        JButtonEdgeDetect.setText("Edge Detect");
        JButtonEdgeDetect.setEnabled(false);
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonEdgeDetect;
  }

  /**
   * This method initializes JButtonEdgeHightlight
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonEdgeHightlight()
  {
    if (JButtonEdgeHightlight == null)
    {
      try
      {
        JButtonEdgeHightlight = new JButton();
        JButtonEdgeHightlight.setText("Edge Highlight");
        JButtonEdgeHightlight.setEnabled(false);
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonEdgeHightlight;
  }

  /**
   * This method initializes JButtonEdgeHorisontal
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonEdgeHorisontal()
  {
    if (JButtonEdgeHorisontal == null)
    {
      try
      {
        JButtonEdgeHorisontal = new JButton();
        JButtonEdgeHorisontal.setText("Edge Horisontal");
        JButtonEdgeHorisontal.setEnabled(false);
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonEdgeHorisontal;
  }

  /**
   * This method initializes JButtonEdgeVertical
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonEdgeVertical()
  {
    if (JButtonEdgeVertical == null)
    {
      try
      {
        JButtonEdgeVertical = new JButton();
        JButtonEdgeVertical.setText("Edge Vertical");
        JButtonEdgeVertical.setEnabled(false);
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonEdgeVertical;
  }

  /**
   * This method initializes JButtonGrayscale
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonGrayscale()
  {
    if (JButtonGrayscale == null)
    {
      try
      {
        JButtonGrayscale = new JButton();
        JButtonGrayscale.setText("Grayscale");
        JButtonGrayscale.setEnabled(false);
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonGrayscale;
  }

  /**
   * This method initializes JButtonSharpen
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonSharpen()
  {
    if (JButtonSharpen == null)
    {
      try
      {
        JButtonSharpen = new JButton();
        JButtonSharpen.setText("Sharpen");
        JButtonSharpen.setEnabled(false);
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonSharpen;
  }

  /**
   * This method initializes JButtonSoften
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonSoften()
  {
    if (JButtonSoften == null)
    {
      try
      {
        JButtonSoften = new JButton();
        JButtonSoften.setText("Soften");
        JButtonSoften.setEnabled(false);
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonSoften;
  }

  /**
   * This method initializes JButtonOpen
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonOpen()
  {
    if (JButtonOpen == null)
    {
      try
      {
        JButtonOpen = new JButton();
        JButtonOpen.setText("Open...");
        JButtonOpen.setEnabled(true);
        JButtonOpen.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(java.awt.event.ActionEvent e)
          {
            JButtonSaveAs.setEnabled(true);
            JButtonSaveAs.setEnabled(true);
            JButtonUndo.setEnabled(true);
            JButtonEdgeDetect.setEnabled(true);
            JButtonEdgeHightlight.setEnabled(true);
            JButtonEdgeHorisontal.setEnabled(true);
            JButtonEdgeVertical.setEnabled(true);
            JButtonGrayscale.setEnabled(true);
            JButtonSharpen.setEnabled(true);
            JButtonSoften.setEnabled(true);
            JButtonSoften.setEnabled(true);

          }
        });
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonOpen;
  }

  /**
   * This method initializes JButtonSaveAs
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonSaveAs()
  {
    if (JButtonSaveAs == null)
    {
      try
      {
        JButtonSaveAs = new JButton();
        JButtonSaveAs.setText("Save as...");
        JButtonSaveAs.setEnabled(false);
        JButtonSaveAs.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(java.awt.event.ActionEvent e)
          {
            JButtonEdgeDetect.setEnabled(true);
            JButtonEdgeHightlight.setEnabled(true);
            JButtonEdgeHorisontal.setEnabled(true);
            JButtonEdgeVertical.setEnabled(true);
            JButtonGrayscale.setEnabled(true);
            JButtonSharpen.setEnabled(true);
            JButtonSoften.setEnabled(true);
            JButtonSoften.setEnabled(true);

          }
        });
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonSaveAs;
  }

  /**
   * This method initializes JButtonUndo
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButtonUndo()
  {
    if (JButtonUndo == null)
    {
      try
      {
        JButtonUndo = new JButton();
        JButtonUndo.setText("Undo");
        JButtonUndo.setEnabled(false);
        JButtonUndo.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(java.awt.event.ActionEvent e)
          {
            JButtonEdgeDetect.setEnabled(true);
            JButtonEdgeHightlight.setEnabled(true);
            JButtonEdgeHorisontal.setEnabled(true);
            JButtonEdgeVertical.setEnabled(true);
            JButtonGrayscale.setEnabled(true);
            JButtonSharpen.setEnabled(true);
            JButtonSoften.setEnabled(true);
            JButtonSoften.setEnabled(true);
          }
        });
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return JButtonUndo;
  }

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    // TODO Auto-generated method stub

    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        SnapShopGUI thisClass = new SnapShopGUI();
        thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        thisClass.setVisible(true);
      }
    });
  }

  /**
   * This method initializes this
   * 
   * @return void
   */
  private void initialize()
  {
    this.setSize(742, 300);
    this.setContentPane(getJContentPane());
    this.setTitle("TCSS 305 SnapShop");
  }
} // @jve:decl-index=0:visual-constraint="10,10"



This post has been edited by talkinrussian: 02 February 2010 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: GUI- File Chooser

Posted 02 February 2010 - 05:50 PM

Ok I have now merged all 3 threads you've opened for this, please dont open anymore duplicates :)
Was This Post Helpful? 0
  • +
  • -

#5 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: GUI- File Chooser

Posted 02 February 2010 - 06:13 PM

View PostPsychoCoder, on 02 February 2010 - 11:50 PM, said:

Ok I have now merged all 3 threads you've opened for this, please dont open anymore duplicates :)


Actually, the last thread is the right one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1