image upload-showing image on a form - java

I am using netbeans6.7.1 and phpmyadmin for my db to develop a java application
to manage students records
i want to upload students photos through browsing by clicking a browse buton which i have
included in my interface
I mean when i click on that button a JFilechooser pops up which filter only images(i have acomplished this)
what i need is when i click on the "Attach button" of the JFilechooser, i want the image i chose to be attached to a jtextArea on the form i'm working with and the JFilechooser be diposed off.
Also how i can save this form together with the image to a database table
Is there a place where i can find a good guide/tutorial about that

JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);
jButton1.addActionListener(this);
if(e.getSource()==jButton1)
{
int i = chooser.showOpenDialog(jPanel1);
if(i==JFileChooser.APPROVE_OPTION)
{
jPanel2.removeAll();
jPanel2.repaint();
File image = chooser.getSelectedFile();
ImageIcon photo = new ImageIcon(image.getAbsolutePath());
//jPanel2.add(new JLabel(photo));
JLabel label=new JLabel("",photo,JLabel.CENTER);
jPanel2.add(label,BorderLayout.CENTER);
jPanel2.repaint();// sets a default image in image field.
jPanel2.revalidate();
}
}
Note:You should set borderlayout for jpanel2
and the selected image size must be the size of jpanel2

Related

Save the background image of a rectangle using file dialog in javafx

I have two rectangles in my scene .Then I set a background in those rectangles using setFill() and make some changes.How do I save those images to disk using saveDialog ? Here is my save function -
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
fileChooser.setTitle("Save Image");
File file = fileChooser.showSaveDialog(window);
rect2.getFill();
SaveFile(file);
You can create a javafx.scene.image.Image from any node by calling snapshot(...) on the node:
Image img = rect2.snapshot(null, null);
The javax.imageio.ImageIO class has methods for saving images in standard formats, but only works with AWT images, so you need to convert it first:
BufferedImage bImg = SwingFXUtils.fromFXImage(img, null);
String format = file.getName().substring(file.getName().lastIndexOf(".")+1);
ImageIO.write(bImg, format, file);

How to save an image received through network in java

image is successfully received at the server side and I can display it on label
but my Problem is how to save that image
I have used
JFileChooser.showSaveDialog()
I tried printstream. I can save the file but whenever I opened the file in image viewer it is showing as this type of file is cant be opened
BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(sock.getInputStream()));
System.out.println("Image received!!!!");
JFileChooser fc = new JFileChooser();
int i=fc.showSaveDialog(null);
if( i == JFileChooser.APPROVE_OPTION ) {
PrintStream ps = new PrintStream(fc.getSelectedFile());
// ImageIO.write(bimg,"JPG",fc.getInputStream());
ps.print( img);
ps.close();
lblNewLabel.setIcon(new ImageIcon(img)); //image is successfully displaying on the label
}
You're writing the "object" representation of the image, only if your load it through PrintStream would you have a chance of seeing it again.
Try using something like...
ImageIO.write(img,"JPG",fc.getSelectedFile());
instead

How to show both text and image from the web in jlabel

I'm trying to display both image from the web and text in JLabel only the image will show.
jlabel.setText("Hello" + "http://");
Try:
URL url = new URL("http://www.url.com/image.jpg");
Image image = ImageIO.read(url);
jlabel.setIcon(new ImageIcon(image));
jlabel.setText("the text");
JLabel has a setIcon method which you can use to set the image, pass it an ImageIcon, which can be created from an URL:
jlabel.setIcon(new ImageIcon(new URL("http:/...")));
jlabel.setText("Hello");
You can also include an image through HTML directly:
jlabel.setText("<html><img src=\"http://...\"></html>");

Java: open image on a jlabel

I have seen tons of questions about this thing but i just cant completely understand why it doesnt work.
I want to open an image using JFileChooser and then show it on the jLabel on the other jFrame. So why it doesnt work? What is so wrong about it?
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
Icon icon = fileopen.getIcon(file);
origin.jLabel1.setIcon(icon);}
By the way will it work for .bmp files, not only .jpg, .png and .gif?
You need to use ImageIcon. References can be found here: ImageIcon java Docs and Swing tutorial.
Here is the updated source:
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
ImageIcon icon = new ImageIcon(file.getPath());
jLabel1.setIcon(icon);
}

Java Swing create a Text Area for the Output

Hello I am really new to Java programming and I have created a Java menu with some options as well as file chooser. Therefore in my IDE I print out the file name and the path of the file that the user chooses. Is there any way that I can create a text area on my frame so the user can see the actual output ?
This is how my file chooser looks like and how I output the results on my console.
JFileChooser chooser = new JFileChooser();
File F = new File("C:/");
File namedir;
File namepath;
chooser.setCurrentDirectory(F);
chooser.showOpenDialog(null);
chooser.setDialogTitle("Choose file to play");
chooser.setApproveButtonText("Play");
namedir = chooser.getCurrentDirectory();
namepath = chooser.getSelectedFile();
System.out.print("the name of the the directory is "+namedir.getName());
System.out.print("the name of the the path is "+namepath.getAbsolutePath());
And here is the code for my menu
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add the menu bar with the item browse
JMenuBar bar = new JMenuBar();
frame.setJMenuBar(bar);
JMenu search = new JMenu("Browse");
bar.add(search);
What I need is a text area so I can output the file name and the path on my frame.
See docs, this explains the use of JTextArea. It is very simple and you can do alot of things using it. It will surely work for you.

Categories

Resources