Results 1 to 4 of 4
  1. #1
    toro1984 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Problem with JFileChooser and BufferedImage Class

    Hello,
    I'm new on this forum, sorry for my english - it's not my native lang.
    I'm trying to write simple image viewer aplication. I have almost no java programming experience and all my knowledge is taken from oracle tutorial webpage. I'm stuck from two days trying to figure solution out. Now I'll describe my program construction(navy coloured code is the code that in my opinion may couse problems).
    My program is built from four files, each file contains one class:
    1. Main.java
    2. Viewer.java - creates GUI, handles all app events
    3. FileChooser - handles file choice
    4. LoadImage - handles images loading

    Problem occurs when i try to choose file using FileChooser. Selected file isn't loaded in the panel in the main app frame. I have no idea why? Here you have codes of the files:

    File Viewer.java

    Java Code:
    import javax.swing.*;
    import java.awt.*; //gridlayout
    import java.util.*; //biblioteka do i18n
    import java.awt.event.*;
    import java.io.*;
    
    public class Viewer extends JFrame{
        private JFrame frame;
    
        [COLOR="Navy"]private JPanel mainPanel;[/COLOR]
    	
       [COLOR="Navy"] private LoadImage image, image2;
    
        private FileChooser fc;[/COLOR]
    
        private JTextArea infoArea;
    
        private String menuFileText, menuOptionsText, menuHelpText, menuFileOpenText, menuFileEditText,
    	      	   menuFileCloseText, menuOptionsLangText, plRadioButtonText, enRadioButtonText, 
    		   menuHelpAboutText, buttonNextText, buttonPrevText, imageName;
    
        private JMenu menuFile, menuOptions, menuOptionsLang, menuHelp;
    	
        private JMenuItem menuFileOpen, menuFileEdit, menuFileClose, menuHelpAbout;
    	
        private JRadioButtonMenuItem plRadioButton, enRadioButton;	
    	
        private JButton buttonNext, buttonPrev;
    
        public Viewer() {
    	setResizable(false);
    	setTitle("Viewer");
    	setSize(900,600);
    	setLocation(200, 100);
    	initializeStrings();
    	setContentPane(createGUI());
    	setJMenuBar(createMenu());
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame = this;
    	} //Viewer()
    
    
        public void initializeStrings() {
    	Locale defLocale = Locale.getDefault();  
    	ResourceBundle texts = ResourceBundle.getBundle("MessagesBundle", defLocale); 
    	menuFileText = texts.getString("menuFileText");
    	menuOptionsText = texts.getString("menuOptionsText");
    	menuHelpText = texts.getString("menuHelpText");
    	menuFileOpenText = texts.getString("menuFileOpenText");
    	menuFileEditText = texts.getString("menuFileEditText");
    	menuFileCloseText = texts.getString("menuFileCloseText");
    	menuOptionsLangText = texts.getString("menuOptionsLangText");
    	plRadioButtonText = texts.getString("plRadioButtonText");
    	enRadioButtonText = texts.getString("enRadioButtonText");
    	menuHelpAboutText = texts.getString("menuHelpAboutText");
    	buttonNextText = texts.getString("buttonNextText");
    	buttonPrevText = texts.getString("buttonPrevText");
        	} //initializeStrings()
    
        public void updateStrings() {
    	menuFile.setText(menuFileText);
    	menuOptions.setText(menuOptionsText);
    	menuHelp.setText(menuHelpText);
    	menuFileOpen.setText(menuFileOpenText);
    	menuFileEdit.setText(menuFileEditText);
    	menuFileClose.setText(menuFileCloseText);
    	menuOptionsLang.setText(menuOptionsLangText);
    	plRadioButton.setText(plRadioButtonText);
    	enRadioButton.setText(enRadioButtonText);
    	menuHelpAbout.setText(menuHelpAboutText);
    	buttonNext.setText(buttonNextText);
    	buttonPrev.setText(buttonPrevText);
    	} //updateStrings()
    
        [COLOR="Navy"]public JPanel createGUI() {[/COLOR]
    	mainPanel = new JPanel(new BorderLayout(5,5));
    	buttonNext = new JButton(buttonNextText);
    	buttonPrev = new JButton(buttonPrevText);
    	infoArea = new JTextArea(2, 20);
    	[COLOR="Navy"]image = new LoadImage(imageName);[/COLOR]
    	mainPanel.add(buttonNext, BorderLayout.EAST);
    	mainPanel.add(buttonPrev, BorderLayout.WEST);
    	mainPanel.add(image, BorderLayout.CENTER);
    	mainPanel.add(infoArea, BorderLayout.SOUTH);
    	return mainPanel;
    	} //createGUI()
    	
        public JMenuBar createMenu() {
    	JMenuBar menuBar = new JMenuBar();
    		
    	menuFile = new JMenu(menuFileText);
    	
    	menuFileOpen = new JMenuItem(menuFileOpenText);
    	[COLOR="Navy"]menuFileOpen.addActionListener(new ActionListener() {
    	    public void actionPerformed(ActionEvent event) {
    		fc = new FileChooser(frame);
    		imageName = fc.getFileName();
    		infoArea.setText(imageName);
    		image = new LoadImage(imageName);
    		mainPanel.add(image);
    		mainPanel.repaint();
    	    }   //actionPerformed
    	});    //actionListener[/COLOR]
    	menuFile.add(menuFileOpen);
          	
    	menuFileEdit = new JMenuItem(menuFileEditText);
    		menuFile.add(menuFileEdit);
    		menuBar.add(menuFile);
    	
    	menuFileClose = new JMenuItem(menuFileCloseText);
    	menuFileClose.addActionListener(new ActionListener() {
    	    public void actionPerformed(ActionEvent event){
    		dispose();  //zamyka aplikację i czy¶ci pamięć
    		}
    	    }); 
    	menuFile.add(menuFileClose);
    		
    	menuOptions = new JMenu(menuOptionsText);
    	menuOptionsLang = new JMenu(menuOptionsLangText);
    		
    	ButtonGroup group = new ButtonGroup(); 
    	plRadioButton = new JRadioButtonMenuItem(plRadioButtonText);
    	plRadioButton.addActionListener(new ActionListener() {
    	    public void actionPerformed(ActionEvent event) {
    		Locale.setDefault(new Locale("pl", "PL")); 
    		initializeStrings();
    		updateStrings();			       	
    		}
    	    });
    	group.add(plRadioButton);
    	
    	enRadioButton = new JRadioButtonMenuItem(enRadioButtonText);
    	enRadioButton.addActionListener(new ActionListener() {
    	    public void actionPerformed(ActionEvent event) {
    		Locale.setDefault(new Locale("en", "GB"));
    		initializeStrings();
    		updateStrings();
    		}
    	    });
    	group.add(enRadioButton);
    	
    	menuOptionsLang.add(plRadioButton);
    	menuOptionsLang.add(enRadioButton);
    	menuOptions.add(menuOptionsLang);
    	menuBar.add(menuOptions);
    		
    	Locale defLocale = Locale.getDefault();
    	if(defLocale.toString().equals("pl_PL")){
    	    plRadioButton.setSelected(true);
    	} else {
    	    enRadioButton.setSelected(true);
    	}
    
    	menuHelp = new JMenu(menuHelpText);
    	menuHelpAbout = new JMenuItem(menuHelpAboutText);
    	menuHelpAbout.addActionListener(new ActionListener(){
    	    public void actionPerformed(ActionEvent event){
    		About about = new About(frame);
    		about.setVisible(true);
    		}
    	    });
    
    	menuHelp.add(menuHelpAbout);
    	menuBar.add(menuHelp);	
    	return menuBar;
        } //createMenu()
    }   //class

    FILE FileChooser.java

    Java Code:
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.SwingUtilities;
    import javax.swing.filechooser.*;
    
    public class FileChooser extends JDialog {
    {
        private JFileChooser fc;
        private String fileName;
        private LoadImage newImage;
    	 
        public FileChooser(JFrame frame) {		
    	fc = new JFileChooser();
            
    	fc.addChoosableFileFilter(new FCfileFilter()); 
    	fc.setAcceptAllFileFilterUsed(false);
    	fc.setFileView(new FCfileView());	
    
    	int returnVal = fc.showDialog(FileChooser.this, "Otwórz");
    	if (returnVal == JFileChooser.APPROVE_OPTION) {
    	    File file = fc.getSelectedFile();
    	    fileName = file.getName();
    	    }	//if
    	}
    
        public String getFileName() {
    	return fileName;
        }
    }
    FILE LoadImage.java

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    public class LoadImage extends JComponent {
              
        BufferedImage img;
        String fileName;
    
        public void paintComponent(Graphics g) {
            g.drawImage(img, 0, 0, 700, 550, null);
        }
    
        public LoadImage(String fileName) { 
    	if (fileName == null)
    	    fileName = "images/b.jpg";
    	    try {
    		img = ImageIO.read(new File(fileName));
    	    } catch (IOException e) { e.printStackTrace(); }		 
        }
    }

  2. #2
    toro1984 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Any ideas??

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Is the file name being shown in the info area? I guessed you have checked that the image actually exists. Is it just the leaf name of the file? Perhaps the full path+name is needed.

  4. #4
    toro1984 is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Yes, i've already checked if the file exists (it's selected in JFileChooser so it must exists, am i right?), in the infoArea field is shown the file name, that was chosen in FileChooser class. But I think the problem may be with the repaint, because i don't know where, when and witch component should be repainted.

Similar Threads

  1. JFileChooser problem
    By jperson in forum New To Java
    Replies: 2
    Last Post: 02-06-2010, 07:31 PM
  2. free memory of bufferedimage problem
    By mr_empty in forum Java 2D
    Replies: 2
    Last Post: 01-17-2010, 06:27 PM
  3. jfilechooser and jtextarea problem
    By kostinio in forum AWT / Swing
    Replies: 7
    Last Post: 10-07-2009, 08:46 AM
  4. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  5. JFileChooser horizontal scrollbar problem
    By nenadm in forum AWT / Swing
    Replies: 4
    Last Post: 11-12-2008, 11:16 AM

Posting Permissions

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