java swing 2 Answer(s) 3 years and 3 months ago
Posted in : Java Beginners
How to upload the image in using java swings. ex- we make a button to browsbutton and savebutton , when we click on the browsbutton , i can brows the image from the computer. plz help soon......... thanx.
public class Uploader extends JFrame { public static JFrame frame; public JList list; public DefaultListModel model; public File currentDir; public DropTarget target; JButton addButton, removeButton, uploadButton; public Uploader() { setSize(600,400); setResizable(true); setLocation(100,100);
Action uploadAction = new UploadAction("Upload"); Action browseAction = new BrowseAction("Browse"); JPanel cp = new JPanel(); cp.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); cp.setLayout(new BorderLayout());
setContentPane(cp); model = new DefaultListModel(); list = new JList(model); list.getInputMap().put(KeyStroke.getKeyStroke(8,0),"insertAction"); list.setVisibleRowCount(12); list.setCellRenderer(new ImageCellRenderer());
target = new DropTarget(list,new FileDropTargetListener()); cp.add(new JScrollPane(list),BorderLayout.CENTER); JPanel buttons = new JPanel(); buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,5,10)); JLabel uploadLabel = new JLabel(); uploadLabel.setLayout(new FlowLayout(FlowLayout.LEFT,5,10)); uploadButton = new JButton(uploadAction); uploadLabel.add(uploadButton); addButton = new JButton(browseAction); buttons.add(addButton); buttons.add(uploadButton); cp.add(buttons,BorderLayout.SOUTH); currentDir = new File(System.getProperty("user.dir")); } public int addToListModel(ArrayList filenames) { int count = 0; for ( Iterator i = filenames.iterator(); i.hasNext(); ) { String filename = (String)i.next(); if (!model.contains(filename)) { model.addElement(filename); count++; } } return count; } public static void main(String [] args) { frame = new Uploader(); frame.setVisible(true); } class BrowseAction extends AbstractAction { public BrowseAction(String text) { super(text); }
public void actionPerformed(ActionEvent evt) { JFileChooser chooser = new JFileChooser(currentDir); chooser.addChoosableFileFilter(new ImageFileFilter()); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); chooser.setMultiSelectionEnabled(true); chooser.setDragEnabled(true); chooser.showOpenDialog(frame); currentDir = chooser.getCurrentDirectory(); File [] files = chooser.getSelectedFiles(); ArrayList filenames = new ArrayList(files.length); for (int i = 0; i < files.length; i++) filenames.add(files[i].getName()); addToListModel(filenames); } } class ImageFileFilter extends javax.swing.filechooser.FileFilter { public boolean accept(File file) { if (file.isDirectory()) return false; String name = file.getName().toLowerCase(); return (name.endsWith(".jpg") || name.endsWith(".png")|| name.endsWith(".gif")); } public String getDescription() { return "Images ( *.jpg, *.png,*.gif)"; } }
March 3, 2010 at 4:00 PM
continue..
class UploadAction extends AbstractAction { public UploadAction(String text) { super(text); } public void actionPerformed(ActionEvent evt) { int selected = list.getSelectedIndex(); Object ob=null; ob=model.getElementAt(selected); String fileName=ob.toString(); File file=new File(fileName); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); PreparedStatement st = con.prepareStatement("insert into file(file,file_data) values(?,?)"); FileInputStream fis = new FileInputStream(file); st.setString(1,fileName); st.setBinaryStream(2, (InputStream)fis, (int)(file.length())); int s = st.executeUpdate(); JOptionPane.showMessageDialog(null,"image is successfully uploaded and inserted."); } catch(Exception ex){} } } class ImageCellRenderer extends JLabel implements ListCellRenderer { public ImageCellRenderer() { setOpaque(true); setIconTextGap(12); } public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus){ File f = new File(value.toString()); Border empty = BorderFactory.createEmptyBorder(3,3,3,3); Border matte = BorderFactory.createMatteBorder(0,0,1,0,Color.white); setBorder(BorderFactory.createCompoundBorder(matte,empty)); setText(f.getName()); Toolkit toolkit = Toolkit.getDefaultToolkit(); Image icon = toolkit.getImage(f.getPath()); Image scaledIcon = icon.getScaledInstance(40,40,Image.SCALE_FAST); setIcon(new ImageIcon(scaledIcon)); if (isSelected) { setBackground(new Color(61,128,223)); setForeground(Color.white); } else if (index % 2 == 0) { setBackground(new Color(237,243,254)); setForeground(Color.darkGray); } else { setBackground(Color.white); setForeground(Color.darkGray); } return this; } } class FileDropTargetListener extends DropTargetAdapter { public void dragEnter(DropTargetDragEvent evt) { frame.requestFocusInWindow(); } public void drop(DropTargetDropEvent evt) { try { Transferable tr = evt.getTransferable(); DataFlavor [] flavors = tr.getTransferDataFlavors(); for ( int i = 0; i < flavors.length; i++ ) { if ( flavors[i].isFlavorJavaFileListType() ) { evt.acceptDrop(DnDConstants.ACTION_COPY); java.util.List list2 = (java.util.List)tr.getTransferData(flavors[i]); ArrayList filenames = new ArrayList(); for ( int j = 0; j < list2.size(); j++ ) { String path = list2.get(j).toString(); if ( ((new ImageFileFilter()).accept(new File(path))) ) { filenames.add(path); } } if (filenames.size() > 0 ) { int numAdded = addToListModel(filenames); evt.dropComplete(true); return; } } JOptionPane.showMessageDialog(frame,"Error","Invalid File Type",JOptionPane.WARNING_MESSAGE); evt.rejectDrop(); }
swing
swing Write a javaswing program to delete a selected record from a table
Swing
Swing Write a javaswing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
Swing
Swing Write a javaswing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
java swing - Swing AWT java swing how i can insert multiple cive me exampleolumn and row in one JList in swing?plz g Hi Friend,
Please clarify your question.
Thanks
Java swing Java swing what are the root classes of all classes in swing
Java swing Java swing Does Swing contains any heavy weight component
java swing java swing view the book details using swing
java swing java swing what is javaswingSwing is a principal GUI toolkit for the Java programming language. It is a part of the JFC (Java Foundation Classes), which is an API for providing a graphical user interface for Java
java swing java swing how to connect database with in grid view in javaswing
Hi Friend,
Please visit the following link:
Grid view in javaswing
Thanks
java swing - Swing AWT java swing how to add image in JPanel in Swing? Hi Friend,
Try the following code:
import java.awt.*;
import java.awt.image....:
http://www.roseindia.net/java/example/java/swing/
Thanks
java swing java swing add two integer variables and display the sum of them using javaswing
Java swing Java swing Write a javaswing program to calculate the age from given date of birth
java swing - Swing AWT javaswing how i can insert in JFrame in swing? Hi Friend,
Try the following code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class FormDemo extends JFrame {
JButton ADD;
JPanel
java swing - Swing AWT javaswing Iam developing a java web browser.Actually my code works fine ie. i can load a web page without proxy.But in my place i have only proxy... a proxy or how to make my java web browser to listen to proxy setting??? please help
java swing. javaswing. Hi
How SetBounds is used in java programs.The values in the setBounds refer to what?
ie for example setBounds(30,30,30,30) and in that the four 30's refer to what
Java swing
are displayed in the table..I need the source code in javaswing...Java swing If i am login to open my account the textfield,textarea and button are displayed. if i am entering the time of the textfield
Java swing
to the database using javaswing...Java swing I create one table. That table contains task ID and Task Name. When I click the task ID one more table will be open and that table
java swing - Swing AWT java swing how to save data in sql 2005 while insert in textfield Hi Friend,
Try the following code:
import java.sql.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import
Java swing Java swing how to create simple addition program using javaswing?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SumOfNumbers extends JFrame
{
SumOfNumbers(){
JLabel lab1=new
swing - Java Beginners Swing application testing What are the conventions for testing a Javaswing application
Swing Program Swing Program Write a javaswing program to calculate the age from given date of birth
Swing paint - Swing AWT
the Swing Applet, use html file with the following code:
Java Applet Demo...Swing paint hi,
i want to print something on window using swing applet..
m doing dis..
protected void paintComponent(Graphics g
Java swing Java swing when i enter the time into the textbox and activities into the textarea the datas saved into the database.the javaswing code for the above item
import java.sql.*;
import java.awt.*;
import javax.swing.
Java swing Java swing How to combine two java files
java swing java swing what is code for diplay on javaswing internal frame form MYSQL DB pls send
Here is a code of creating form on jinternalframe and connect to mysql.
import java.io.*;
import java.sql.*;
import java.awt.
swing program swing program Write a javaswing program to getname and email id. Display the message if email id contains the name of theuser
Java swing Java swing What method is used to specify a container's layout
Java swing Java swing What methods are used to get and set the text label displayed by a Button object
Java Swing Java Swing I want scroll bar in a frame is it possible? if it is possible means i want coding for that please send me a reply
java swing java swing iam using my project text box, label , combobox and that the same time i want menubar and popmenu. plz give me code for this. i want immediately plz send me that code
java swing java swing iam using my project text box, label , combobox and that the same time i want menubar and popmenu. plz give me code for this. i want immediately plz send me that code
Java Swing Java Swing i have a Label more than that of a frame .... how to extend the frame to view the label which are hidden.. please send me the coding sir/madam
Java Swing Java Swing i have a Label more than that of a frame .... how to extend the frame to view the label which are hidden.. please send me the coding sir/madam
Java Swing Java Swing i have a Label more than that of a frame .... how to extend the frame to view the label which are hidden.. please send me the answer sir/madam
Java Swing Java Swing i have a Label more than that of a frame .... how to extend the frame to view the label which are hidden.. please send me the coding sir/madam
Java swing Java swing Design an appliaction for with details such as name,age,DOB,address,qualification and finaly when we click the view details button all types details should be displayed in another View in TextView's..I need the sample
Java swing Java swing I create 2 text field f1,f2 and 2 button b1,b2.If i enter some text in the text field and click the button b1 the text field f1 text will be shown in the Message dialog box and if i click the button b2 the text field
Java Swing Java Swing Write an applet program to transfer the content of the text field into the listbox component on clicking a button code project
 ...++");
model.addElement("Java");
model.addElement("Perl");
model.addElement
java swing java swing what is code for dislay image on java swinginternalframe form MYSQL DB pls send
Here is a code that displays an image on internal frame.
import java.awt.*;
import java.io.*;
import javax.imageio.
Java swing Java swing how to create the login form?
1)LoginDemo.java:
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class LoginDemo extends JFrame
{
JButton SUBMIT;
JLabel
Swing Program Swing Program write a javaswing program to get user name and password. The name contains only characters while password is masked with the character "*". Apply checks for all validation
Swing Program Swing Program Write a javaswing program that takes name and marks as input with all validation. The name and marks will be displayed in message box when user clicks on a button
Java swing code Javaswing code can any one send me the javaswing code for the following:
"A confirmation message after the successful registration of login form
Java swing code Javaswing code can any one send me the javaswing code for the following:
"A confirmation message after the successful registration of login form
java swing and CSS javaswing and CSS can css be used in javaswing desktop application in different forms for better styles?plzz help
Java swing code Javaswing code can any one send me the javaswing code for the following:
"A confirmation message after the successful registration of login form
Java swing code Javaswing code can any one send me the javaswing code for the following:
"A confirmation message after the successful registration of login form