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
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
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
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(); }
}
}