I have searched on google, looked at the documentation on oracle's webpage, read a chapter about graphics in java but I can't seem to figure it out so please help! I want a button that uploads the image onto a JLabel which is located in a JPanel which is located in a JFrame. Nothing more than that! Why isn't my code working? What happens when I execute the program is absolutely nothing. Nothing gets appended to the log and there aren't any errors... Just nothing that happens, I've been programming java for quite some time now but never worked with graphics so I can't solve the problem by myself...
JLabel imageLabel = new JLabel();
private void uploadButtonActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File("C:\\"));
fileChooser.setDialogTitle("Choose file");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "
+ fileChooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "
+ fileChooser.getSelectedFile());
imagePath = fileChooser.getSelectedFile().toString();
ImageIcon imageIcon = new ImageIcon(imagePath);
imageLabel.setIcon(imageIcon);
}
}