Monday, 5 June 2017

java - JfileChooser wont show image selected inside Jpanel

I am having no luck getting an image to show in a Jpanel when selected using Java's JFileChooser. I also want the selected image to be automatically resized and fit the set size of my Jpanel?



Here is the code i have written so far. The JfileChooser is shown in a new JFrame and i want to also close this Jframe when an image is successfully uploaded and viewable in Jpanel.



Here is the Upload Button that should open the JFileChooser, allow file to be selected and then upload the image (to be resized, NEED HELP IN HOW TO MAKE IMAGE AUTOMATICALLY RESIZED), then close the Jframe "frame" when the image is show in the Jpanel "PicturePanel"?



EDIT: ADDED A SSCCE of what i am trying to accomplish.




I want to after the "upload" button click, show the image sized appropriately (MUST FIT THE JPANEl "Picture panel") Show the image in the Black bordered panel.



import classes.BackgroundPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Test {
public static void main(String[] args) {
final JFileChooser chooser = new JFileChooser();
JButton button = new JButton();
button.setText("Upload");
JFrame frame = new JFrame("My Frame");

final JFrame imageFrame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFileChooser fc = new JFileChooser();
final Test_Image t = new Test_Image();
JPanel panel = new JPanel();
JPanel picturePanel = new JPanel();
// chooser.showOpenDialog(null);
Dimension d = new Dimension(1261, 765);
Dimension d2 = new Dimension(1300, 900);
picturePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
Dimension d3 = new Dimension(343, 247);
picturePanel.setSize(d3);
//picturePanel.setSize(d);
panel.add(button);
panel.setSize(d3);


//panel.setVisible(true);
//panel.add(picturePanel);


button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
{
if(chooser.showOpenDialog(imageFrame) == JFileChooser.APPROVE_OPTION) {

try {
Image bi = ImageIO.read(
chooser.getSelectedFile());

BackgroundPanel bp = new BackgroundPanel(bi);
if (bi != null)
bp.setImage(bi);
else
JOptionPane.showMessageDialog(imageFrame,
"File is not an image!");
} catch (IOException ioe) {
JOptionPane.showMessageDialog(imageFrame,
"Error Reading File!");
}

}

}
});


frame.setSize(d2);
frame.add(picturePanel).setLocation(100, 100);
frame.add(panel);
frame.setVisible(true);


}

}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...