Question: Question: How do I use FileChooser to select a file in my ControlFrame.java file? I want to select a file when I select Show Picture
Question: How do I use FileChooser to select a file in my ControlFrame.java file? I want to select a file when I select Show Picture from the file menu or when I select Load Sound from the File menu.
File: ControlFrame.java
// ControlFrame.java import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JMenuBar; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import javax.swing.JOptionPane; import javax.swing.JSlider; import javax.swing.SwingConstants; import javax.swing.event.ChangeListener; import javax.swing.event.ChangeEvent; import java.awt.BorderLayout; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.filechooser.*;
public class ControlFrame extends JFrame { private JPanel mainPanel; private final JPanel calcPanel; private JSlider widthJSlider; private JTextField xValTextField; private JTextField yValTextField; private JLabel calcJLabel; private JButton calcJButton; final FileChooser fc = new FileChooser(); private String xStr; private String yStr; public ControlFrame(String title) { super( title ); mainPanel = new JPanel( new BorderLayout() ); mainPanel.setSize(200, 250); calcPanel = new JPanel( new FlowLayout() ); calcPanel.setSize(200, 200);
final DrawControlPanel drawPanel = new DrawControlPanel(); drawPanel.setSize(200, 200); final FileChooser chooser = new FileChooser(); // set file name String imageFileName = "C:/intro-prog-java/mediasources/mickey4.jpg";
final DrawImageControlPanel drawImagePanel = new DrawImageControlPanel(imageFileName); drawImagePanel.setSize(800, 600); // set sound name String soundFileName = "C:/intro-prog-java/mediasources/thisisatest.wav"; final SoundControlPanel soundPanel = new SoundControlPanel(soundFileName); soundPanel.setSize(200, 200); this.setContentPane( mainPanel ); JMenu fileMenu = new JMenu( "File" ); fileMenu.setMnemonic( 'F' ); JMenuItem aboutItem = new JMenuItem( "About..." ); aboutItem.setMnemonic( 'A' ); fileMenu.add( aboutItem ); aboutItem.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( ControlFrame.this, "This application provides enhanced control over multimedia projects.", "About", JOptionPane.PLAIN_MESSAGE ); } } // End of anonymous inner class ); final JMenuBar bar = new JMenuBar(); // Create a JMenuBar so we can attach menus to it. setJMenuBar( bar ); // Attach the JMenuBar to the ControlFrame. bar.add( fileMenu ); // Add the file menu to the JMenuBar. final JMenu colorMenu = new JMenu( "Color" ); colorMenu.setMnemonic( 'C' ); JMenuItem redItem = new JMenuItem( "Red" ); colorMenu.add( redItem ); redItem.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawPanel.setFillColor( Color.RED ); repaint(); } } // End of anonymous inner class ); JMenuItem blueItem = new JMenuItem( "Blue" ); colorMenu.add( blueItem ); blueItem.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawPanel.setFillColor( Color.BLUE ); repaint(); } } // End of anonymous inner class ); JMenuItem magentaItem = new JMenuItem( "Magenta" ); colorMenu.add( magentaItem ); magentaItem.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawPanel.setFillColor( Color.MAGENTA ); repaint(); } } // End of anonymous inner class ); JMenuItem cyanItem = new JMenuItem( "Cyan" ); colorMenu.add( cyanItem ); cyanItem.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawPanel.setFillColor( Color.CYAN ); repaint(); } } // End of anonymous inner class );
// create Image menu which allows image manipulation operations to be performed. final JMenu imageMenu = new JMenu( "Image" ); imageMenu.setMnemonic( 'I' ); JMenuItem imageOp1Item = new JMenuItem( "Increase Red" ); imageMenu.add( imageOp1Item ); imageOp1Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawImagePanel.increaseRed(); repaint(); } } // End of anonymous inner class );
JMenuItem imageOp2Item = new JMenuItem( "Increase Green" ); imageMenu.add( imageOp2Item ); imageOp2Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawImagePanel.increaseGreen(); repaint(); } } // End of anonymous inner class ); JMenuItem imageOp3Item = new JMenuItem( "Increase Blue" ); imageMenu.add( imageOp3Item ); imageOp3Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawImagePanel.increaseBlue(); repaint(); } } // End of anonymous inner class ); JMenuItem imageOp4Item = new JMenuItem( "Decrease Red" ); imageMenu.add( imageOp4Item ); imageOp4Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawImagePanel.decreaseRedForEach(); repaint(); } } // End of anonymous inner class );
JMenuItem imageOp5Item = new JMenuItem( "Set Original" ); imageMenu.add( imageOp5Item ); imageOp5Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { drawImagePanel.setOriginalImage(); repaint(); } } // End of anonymous inner class ); // create Sound menu which loads a Sound object. final JMenu soundMenu = new JMenu( "Sound" ); imageMenu.setMnemonic( 'O' ); JMenuItem soundOp1Item = new JMenuItem( "Play" ); soundMenu.add( soundOp1Item ); soundOp1Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { soundPanel.blockingPlay(); repaint(); } } // End of anonymous inner class ); JMenuItem soundOp2Item = new JMenuItem( "Increase Volume" ); soundMenu.add( soundOp2Item ); soundOp2Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { soundPanel.increaseVolume(); soundPanel.blockingPlay(); repaint(); } } // End of anonymous inner class ); JMenuItem soundOp3Item = new JMenuItem( "Decrease Volume" ); soundMenu.add( soundOp3Item ); soundOp3Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { soundPanel.decreaseVolume(); soundPanel.blockingPlay(); repaint(); } } // End of anonymous inner class ); JMenuItem soundOp4Item = new JMenuItem( "Mirror" ); soundMenu.add( soundOp4Item ); soundOp4Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { soundPanel.mirrorFrontToBack(); soundPanel.blockingPlay(); repaint(); } } // End of anonymous inner class ); JMenuItem soundOp5Item = new JMenuItem( "Set Original" ); soundMenu.add( soundOp5Item ); soundOp5Item.addActionListener( new ActionListener() // Beginning of anonymous inner class { public void actionPerformed( ActionEvent event ) { soundPanel.setOriginalSound(); soundPanel.blockingPlay(); repaint(); } } // End of anonymous inner class ); JMenuItem calcPanelItem = new JMenuItem( "Calculate" ); calcPanelItem.setMnemonic( 'C' ); fileMenu.add( calcPanelItem ); calcPanelItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { //remove menus bar.remove( colorMenu ); bar.remove( soundMenu ); bar.remove( imageMenu ); //remove panels mainPanel.remove( drawPanel ); mainPanel.remove( widthJSlider ); mainPanel.remove( drawImagePanel ); mainPanel.remove( soundPanel ); xValTextField.setText(""); yValTextField.setText(""); calcJLabel.setText( "" ); mainPanel.add( calcPanel, BorderLayout.CENTER ); validate(); repaint(); } } ); JMenuItem drawPanelItem = new JMenuItem( "DrawPanel" ); drawPanelItem.setMnemonic( 'D' ); fileMenu.add( drawPanelItem ); drawPanelItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { //remove menus bar.remove( soundMenu ); bar.remove( imageMenu ); //remove panels mainPanel.remove( drawImagePanel ); mainPanel.remove( soundPanel ); mainPanel.remove( calcPanel ); bar.add( colorMenu ); drawPanel.setBackground( Color.WHITE ); mainPanel.add( drawPanel, BorderLayout.CENTER ); mainPanel.add( widthJSlider, BorderLayout.SOUTH ); validate(); repaint(); } } ); // Add a menu item named Show Picture to the File menu that will show the image of a Picture object. JMenuItem drawImagePanelItem = new JMenuItem( "Show Picture" ); drawImagePanelItem.setMnemonic( 'S' ); fileMenu.add( drawImagePanelItem ); drawImagePanelItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { // remove menus for color and sound bar.remove( colorMenu ); bar.remove( soundMenu ); // if sound is played, stop the sound //soundPanel.stop(); // remove panels for draw, sound and calculation mainPanel.remove( drawPanel ); mainPanel.remove( soundPanel ); mainPanel.remove( calcPanel ); // remove slider feature mainPanel.remove( widthJSlider ); // set original image to be shown drawImagePanel.setOriginalImage(); // add panel for showing image mainPanel.add( drawImagePanel, BorderLayout.CENTER ); // add image menu bar.add( imageMenu );
validate(); repaint(); } } ); // Add a menu item named Load Sound to the File menu that will load sound JMenuItem loadSoundPanelItem = new JMenuItem( "Load Sound" ); loadSoundPanelItem.setMnemonic( 'L' ); fileMenu.add( loadSoundPanelItem ); loadSoundPanelItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { // remove menus for color and image bar.remove( colorMenu ); bar.remove( imageMenu ); // remove panels for draw, draw image and calculation mainPanel.remove( drawPanel ); mainPanel.remove( calcPanel ); mainPanel.remove( drawImagePanel ); // remove slider feature mainPanel.remove( widthJSlider ); // set original sound final FileChooser chooser = new FileChooser(); // final soundFileName = chooser.pickAFile(); soundPanel.setOriginalSound(); // add panel for controlling sound mainPanel.add( soundPanel, BorderLayout.CENTER ); // add sound menu bar.add( soundMenu ); validate(); repaint(); } } ); JMenuItem exitItem = new JMenuItem( "Exit" ); exitItem.setMnemonic( 'x' ); fileMenu.add( exitItem ); exitItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit( 0 ); } } ); widthJSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 100, drawPanel.getOvalWidth() ); widthJSlider.setMajorTickSpacing( 10 ); widthJSlider.setPaintTicks( true ); widthJSlider.addChangeListener( new ChangeListener() { public void stateChanged( ChangeEvent e ) { drawPanel.setOvalWidth( widthJSlider.getValue() ); repaint(); } } ); xValTextField = new JTextField( 3 ); xValTextField.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { xStr = event.getActionCommand(); } } );
calcPanel.add( xValTextField );
yValTextField = new JTextField( 3 ); yValTextField.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { yStr = event.getActionCommand(); } } );
calcPanel.add( yValTextField ); calcJButton = new JButton( "Calculate" ); calcJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { try { int x = Integer.parseInt( xStr ); int y = Integer.parseInt( yStr ); int result = x + y; calcJLabel.setText(xStr + " + " + yStr + " = " + result); } catch (NumberFormatException e) { JOptionPane.showMessageDialog( ControlFrame.this, "You must enter a valid number and then
setSize( 200, 250 ); setVisible( true ); validate(); } }
File: DrawImageControlPanel.java
// DrawImageControlPanel.java import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; import javax.swing.JPanel;
public class DrawImageControlPanel extends JPanel {
private String imageFileName; private Picture pictObject; private int x; private int y;
public DrawImageControlPanel(String imageFileName) { //setSize(800, 600); this.imageFileName = imageFileName; this.pictObject = null; this.x = 0; this.y = 0; }
public void paintComponent( Graphics g ) { super.paintComponent( g ); // invoke the superclass paintComponent this.setBackground( Color.WHITE );
// get BufferedImage object BufferedImage bufferedImage = this.pictObject.getBufferedImage(); // get draw starting point after picture is rendered this.x = (this.getWidth() - this.pictObject.getWidth() ) / 2; this.y = ( this.getHeight() - this.pictObject.getHeight() ) / 2; // draw to panel g.drawImage(bufferedImage, x, y, null); }
public void setOriginalImage() { this.pictObject = new Picture( this.imageFileName ); }
public void increaseRed() { this.pictObject.increaseRed(); }
public void decreaseRedForEach() { this.pictObject.decreaseRedForEach(); }
public void increaseBlue() { this.pictObject.increaseBlue(); }
public void increaseGreen() { this.pictObject.increaseGreen(); }
}
File: FileChooser.java
import javax.swing.JFileChooser; import javax.swing.JFrame; import java.util.Properties; import java.io.*; /** * A class to make working with a file chooser easier * for students. It uses a JFileChooser to let the user * pick a file and returns the choosen file name. * * Copyright Georgia Institute of Technology 2004 * @author Barb Ericson ericson@cc.gatech.edu */ public class FileChooser {
///////////////////////////// class fields /////////////////// /** * Properities to use during execution */ private static Properties appProperties = null; /** * Property key for the media directory */ private static final String MEDIA_DIRECTORY = "mediaDirectory"; /** * Name for property file */ private static final String PROPERTY_FILE_NAME = "SimplePictureProperties.txt"; /////////////////////// methods ///////////////////////////// /** * Method to let the user pick a file and return * the full file name as a string. If the user didn't * pick a file then the file name will be null. * @return the full file name of the picked file or null */ public static String pickAFile() { JFileChooser fileChooser = null; // start off the file name as null String fileName = null; // get the current media directory String mediaDir = getMediaDirectory(); /* create a file for this and check that the directory exists * and if it does set the file chooser to use it */ try { File file = new File(mediaDir); if (file.exists()) fileChooser = new JFileChooser(file); } catch (Exception ex) { } // if no file chooser yet create one if (fileChooser == null) fileChooser = new JFileChooser(); /* create a JFrame to be the parent of the file * chooser open dialog if you don't do this then * you may not see the dialog. */ JFrame frame = new JFrame(); // get the return value from choosing a file int returnVal = fileChooser.showOpenDialog(frame); // if the return value says the user picked a file if (returnVal == JFileChooser.APPROVE_OPTION) fileName = fileChooser.getSelectedFile().getPath(); return fileName; } /** * Method to get the full path for the passed file name * @param fileName the name of a file * @return the full path for the file */ public static String getMediaPath(String fileName) { String path = null; String directory = getMediaDirectory(); // get the full path path = directory + fileName; return path; } /** * Method to get the directory for the media * @return the media directory */ public static String getMediaDirectory() { String directory = null; // check if the application properties are null if (appProperties == null) { appProperties = new Properties(); // load the properties from a file try { FileInputStream in = new FileInputStream(PROPERTY_FILE_NAME); appProperties.load(in); in.close(); } catch (Exception ex) { directory = "c:/intro-prog-java/mediasources/"; } } // get the media directory if (appProperties != null) directory = (String) appProperties.get(MEDIA_DIRECTORY); return directory; } /** * Method to set the media path by setting the directory to use * @param directory the directory to use for the media path */ public static void setMediaPath(String directory) { // check if the directory exists File file = new File(directory); if (!file.exists()) System.out.println("Sorry but " + directory + " doesn't exist, try a different directory."); else { /* check if there is an application properties object * and if not create one */ if (appProperties == null) appProperties = new Properties(); // set the media directory property appProperties.put(MEDIA_DIRECTORY,directory); // write out the application properties to a file try { FileOutputStream out = new FileOutputStream(PROPERTY_FILE_NAME); appProperties.store(out, "Properties for the Simple Picture class"); out.close(); System.out.println("The media directory is now " + directory); } catch (Exception ex) { System.out.println("Couldn't save media path to a file"); } } } }
File: DrawControlApp.java
// DrawControlApp.java import java.awt.Color; import javax.swing.JFrame;
public class DrawControlApp { public static void main( String args[] ) { JFrame frame = new ControlFrame( "Controlling Multimedia Projects..." ); frame.setSize( 900, 700 ); } }
Note: I know you need more files, but it won't accept the question if I include them.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
