I have been crawling Oracle and Stack Overflow trying to find a solution to my problem, but I haven't managed to find what I am looking for.
Below I have two classes, and all the program needs to do at the moment is allow the user to open an image file using a JFileChooser
and display it in the GUI (So later some image manipulation can be done to it)
I have tested the code execution to the point that I know the variable img
in the ImageGUI
class is being initialized to the picture I want on line 99. My issue is getting the image to show in the GUI before I pack();
it.
What am I missing?
public class ImageEditLaunch {
public static void main(String[] args) {
ImageEditEngine program = new ImageEditEngine();
}
}
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageEditEngine {
private static int[][][] originalImage;
private static BufferedImage originalImageBuffer;
public ImageEditEngine() {
originalImage = new int[0][0][0];
ImageGUI gui = new ImageGUI();
gui.setVisible(true);
}
public static ImageIcon openImage(String filepath){
try{
File f = new File(filepath);
BufferedImage image = ImageIO.read(f);
originalImageBuffer = image;
ImageIcon icon = new ImageIcon(image);
return icon;
}
catch(Exception e){
e.printStackTrace();
return new ImageIcon();
}
}
}
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class ImageGUI extends JFrame implements ActionListener{
private JFrame window;
private JMenuItem mOpenFile;
private JMenuItem mSaveFile;
private JMenuItem mExitCommand;
private JMenuBar parentBar;
private JMenu fileMenu;
private JMenu commandMenu;
private JPanel mainPanel;
public ImageIcon img;
private JLabel mLabel;
public ImageGUI(){
super();
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e){
e.printStackTrace();
}
//setup CommandMenu
commandMenu = makeMenu("Command", 'C');
//setup FileMenu
fileMenu = makeMenu("File", 'F');
mOpenFile = makeMenuItem(fileMenu, "Open...", 'O');
mSaveFile = makeMenuItem(fileMenu, "Save...", 'S');
mExitCommand = makeMenuItem(fileMenu, "Exit", 'X');
//setup parentBar
parentBar = new JMenuBar();
parentBar.add(fileMenu);
parentBar.add(commandMenu);
//main panel
mainPanel = new JPanel(new BorderLayout());
mLabel = new JLabel();
mainPanel.add(mLabel, BorderLayout.CENTER);
setJMenuBar(parentBar);
setSize(new Dimension(400, 300));
}
protected JMenuItem makeMenuItem(JMenu menu, String name, char mnemonic){
JMenuItem m = new JMenuItem(name, (int) mnemonic);
m.addActionListener(this);
menu.add(m);
return m;
}
protected JMenu makeMenu(String name, char mnemonic){
JMenu menu = new JMenu(name);
menu.setMnemonic(mnemonic);
return menu;
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==mOpenFile){
String path = null;
JFileChooser jfc = new JFileChooser();
jfc.setCurrentDirectory(new File("."));
int result = jfc.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
path = file.getAbsolutePath();
}
img=ImageEditEngine.openImage(path);
mLabel= new JLabel(img);
mLabel.setVisible(true);
this.repaint();
pack();
}
}
}
The value of the toString() on mLabel right after:
mLabel = new JLabel(img);
is as follows
javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,
border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,
defaultIcon=javax.swing.ImageIcon@2bd58ce,disabledIcon=,
horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,
iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,
verticalTextPosition=CENTER]
The fundamental problem was that the panel was never added to the frame. There were many other problems with the code, some of which I fixed (by just removing entire sections of it), others that I tweaked. There are still other problems with the code shown below that need to be fixed (one or two of which I introduced just in lazy coding - but they are not fatal).
Having said that, this code now works. Look over it carefully for tips. Note that it is an MCVE that hot links to the image, so it should be easy to test.