Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Using a JFileChooser

Have Programming Question? Ask it here!
 
 
 


Melvin Cai
Using a JFileChooser
2 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners



Advertisement
View Answers

August 28, 2009 at 10:38 AM


Hi Friend,

Try the following code:

import java.sql.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.ImageIO;

public class InsertImage {
static JTextField text=new JTextField(20);
static JButton button=new JButton("Save");
static JPanel panel=new JPanel();

public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
final File file ;
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
final String name=file.getName();
System.out.println(name);
text.setText(name);
panel.add(text);
panel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
FileInputStream fis;
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st = con.prepareStatement("insert into file(file_name,file_data) values(?,?)");
fis = new FileInputStream(file);
st.setString(1,name);
st.setBinaryStream(2, (InputStream)fis, (int)(file.length()));
int s = st.executeUpdate();
JOptionPane.showMessageDialog(null,"image is successfully inserted.");
}
catch(Exception ex){}
}
});
JFrame f=new JFrame("Insert Image");
f.add(panel);
f.setVisible(true);
f.setSize(300,100);
}
}
}

Thanks


August 28, 2009 at 12:46 PM


Thank you Sir / Ma'am for your valuble tutorial.

Thanks in a million.


Related Tutorials/Questions & Answers:
Using a JFileChooser - Java Beginners
Using a JFileChooser  Dear Editor, How to select an image from JFileChooser & add the image in mySQL5 database? Thanks in advanced. Regards... void main(String[] args) { JFileChooser chooser = new JFileChooser(); int
How to open a file in java using JFileChooser
How to open a file in java using JFileChooser  How to open a file in java using JFileChooser
Advertisements
wait until a file is opened using JFileChooser
wait until a file is opened using JFileChooser   I created a java... JFileChooser to open a file. b. call a funtion that takes in the filename as argument...){ JFileChooser chooser=new JFileChooser(); int ret = chooser.showDialog
JFileChooser - Swing AWT
JFileChooser  Hi,iam using jfilechooser to select a directory....now... ActionListener { JButton button; JFileChooser chooser; String... JFileChooser(); chooser.setCurrentDirectory(new java.io.File
JFileChooser - Java Beginners
JFileChooser  How to create a GUI application that allows a user to select a text file (using the JFileChooser). The program will then count...(ActionEvent e){ JFileChooser chooser = new JFileChooser
How to select default FileFilter when creating a JFileChooser dialog?
How to select default FileFilter when creating a JFileChooser dialog? ... default FileFilter when creating a JFileChooser dialog? Thanks   Hi, Following code can be used: JFileChooser c = new JFileChooser
JFileChooser
Java: JFileChooser The javax.swing.JFileChooser class is used to create... an Open File Chooser The sample code below uses JFileChooser in the Open button.... // Illustrates menus, JFileChooser, Scanner.. // Author : Fred Swartz // Date : 2005-02-23
java in using brausing
java in using brausing  sir, i am beginner java developer ,sir i am creat's the image viewer Jframe but not a created frame plz. halpe me give...) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File
generate columns in file using tab delimeter - Swing AWT
generate columns in file using tab delimeter   Hi, I have problem ........i have imported all the files in a directory using JFIleChooser... one please help me how to generate the columns and rows using tab delimiter.(I
using array
using array  transpose a matrix
using array
using array  transpose a matrix
java chat using java media
java chat using java media   Remove the error of this code plz reply... ActionListener, ControllerListener { JButton fileButton; JFileChooser file = new JFileChooser(); JLabel urlLabel; JTextField urlField; String mediaFile
using array
using array  column wise total of a matrix
using prompt
using prompt  how to use prompt
using function
using function   using a function to find the value of v from the foiiowing expression 1v=1u+1*f
using array
using array  Circular left shift array element by one position
using array
using array  Circular left shift array element by one position
using array
using array  read 10 digit number and display (star
using array
using array  display 10 digit number using array and print pyramid. 1 1 1 1 1 1 1 1 1 1 1 1
Using Ajax
Using Ajax  Hi, How I can use Ajax in web programming? Tell me any example which using Ajax to call server side script? Thanks   Hi, Here are examples: PHP Ajax and Database First Ajax Example Getting Started
using for loop
using for loop  what will be the source code of the program that the output will be printing all numbers that is divisible by 3 and 5 sample output: 3 is divisible by 3 5 is divisible by 5 6 is divisible by 3 9 is divisible by 3
using function
using function  write a program using function overloading to accept any sentence and print vowels and consonants sepratly   Here is an example that accepts a sentence from the user and display the vowels and consonants
using iReport
using iReport  Hello sir, I am using iReport3.0 and then i am calling the .jrxml file in jsp but i got the error.My JSP code is as below and after the jsp code i have placed the error code Thanks in advance: &lt
using constructor
using constructor  all constructor for matrix class?   The given code implements the Matrix class and show the addition of two matrices. public class Matrix{ int M; int N
How to link a excel file with a application software using java swing
How to link a excel file with a application software using java swing  I have to link an excel file with a application software which I am...){ JFileChooser fc = new JFileChooser
How to link a excel file with a application software using java swing
How to link a excel file with a application software using java swing  I have to link an excel file with a application software which I am...){ JFileChooser fc = new JFileChooser
How to link an excel file with the application software using java swing
How to link an excel file with the application software using java swing  I have to link an excel file with a application software which I am...){ JFileChooser fc = new JFileChooser
using eval in Java
using eval in Java  using eval in Java
Is JSF using JSP?
Is JSF using JSP?  Is JSF using JSP
string palindrom using vbscript?
string palindrom using vbscript?  string palindrom using vbscript
write a programm using java
write a programm using java  print the following using java programming
Using throw in java method
Using throw in java method  using throw with method implies what
why using static keyword
why using static keyword  why using static keyword
Image Movement using Swings
Image Movement using Swings  How to move image using Swings
Date object using JavaScript
Date object using JavaScript  What's the Date object using JavaScript
delete an entry using JavaScript
delete an entry using JavaScript  How to delete an entry using JavaScript
report generation using jsp
report generation using jsp  report generation coding using jsp
survey poll using java
survey poll using java  how to make a survey poll using java? i am using netbeans and glassfish
A Java Program by using JSP
A Java Program by using JSP  how to draw lines by using JSP plz show me the solution by using program
java code using swings
java code using swings  code that should be able to enter data of student details using all swings into the access database using jdbc connectivity
tree using jsp code
tree using jsp code  i want to draw a tree structure of a family hierarchy using jsp code
create a form using struts
create a form using struts  How can I create a form for inputting text and uploading image using struts
using random number
using random number  generate a 10 digit number using random number and display the length of longest increasing series
timer using javascript
timer using javascript  How to display timer on HTML page using JavaScript
stack using linked list
stack using linked list  how to implement stack using linked list
Android using database
Android using database  How to Add,delete,modify the contact name,address,phone no into the database using android coding
validate parameter before using it
validate parameter before using it   How to validate the parameter before using it in JSP
AutoCompleteField using JTextArea
AutoCompleteField using JTextArea  How to create an AutoCompleteField in java such as username field using JTextArea
ScatterPlot using jsp
ScatterPlot using jsp  hi, can anybody provide me code for ScatterPlot using jsp. thanks
generate charts using JSP
generate charts using JSP  any one know coding for generate bar chart or pie chart using JSP
 

 

 

DMCA.com