FileChooser

package GYM; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; import java.io.*; public class Clientes extends JDialog implements ActionListener{ public void actionPerformed(ActionEvent evt) { if(cap2==evt.getSource()) { //JFILECHOOSER // // FileFilter filter1 = new ExtensionFileFilter("JPG , GIF, JPEG and PNG, ", new String[] { "JPG", "JPEG" , "PNG","GIF" }); // fc.setFileFilter(filter1); int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //This is where a real application would open the file. System.out.println("Opening: " + file.getName() + "."+file.getPath()); ruta=file.getPath(); sizeL(foto,file.getPath()); } else { System.out.println("Operacion cancelada."); } } } /** * Creates a new instance of <code>Clientes</code>. */ JPanel panel=new JPanel(); JPanel panel2=new JPanel(); JLabel eti; JTextField txt[]=new JTextField[6]; JTextArea area=new JTextArea(); JTabbedPane pes=new JTabbedPane(); JTabbedPane pes2=new JTabbedPane(); String tex[]={"GUARDAR","LIMPIAR","SALIR"}; JButton bt[]=new JButton[3]; JPanel datos=new JPanel(); JPanel datos2=new JPanel(); //// Metodos m=new Metodos(); MySQL sql=new MySQL(); // Validar_Campos vc=new Validar_Campos(); JLabel foto=new JLabel(); JFileChooser fc; String ruta=""; //variable de la foto Image img = null; File nueva_foto=null; // Capturar_Foto dialog=null; JButton cap=new JButton("Capturar"); JButton cap2=new JButton("Seleccionar"); public Clientes() { super(new Frame(), "NUEVO CLIENTE", true); setLayout(null); panel.setLayout(null); pes.addTab("DATOS DEL CLIENTE",panel); pes.setBounds(10,50,530,330); add(pes); foto.setText("Foto"); foto.setBounds(pes.getX()+pes.getWidth()+10,90,120,120); foto.setBorder(BorderFactory.createLoweredBevelBorder()); add(foto); // cap=new JButton("Capturar"); cap.setBounds(foto.getX(),foto.getY()+foto.getHeight()+10,110,30); cap.addActionListener(this); add(cap); // cap2=new JButton("Capturar"); cap2.setBounds(cap.getX(),cap.getY()+cap.getHeight()+10,110,30); cap2.addActionListener(this); add(cap2); ruta="Fotos/ultimaFoto.jpg"; sizeL(foto,ruta+"1"); fc = new JFileChooser(); panel2.setLayout(null); pes2.addTab("CONTROLES",panel2); pes2.setBounds(650,50,150,330); // add(pes2); datos.setLayout(null); datos.setBorder(new TitledBorder("DATOS GENERALES")); datos.setBounds(5,10,520,160); panel.add(datos); datos2.setLayout(null); datos2.setBorder(new TitledBorder("DIRECCION")); datos2.setBounds(5,170,520,120); panel.add(datos2); eti=new JLabel("ID_CLIENTE"); eti.setBounds(10,30,100,25); datos.add(eti); txt[0]=new JTextField(); txt[0].setBounds(eti.getX()+90,eti.getY(),120,25); // txt[0].setText(""+(m.contar("select * from clientes")+1)); txt[0].addActionListener(this); datos.add(txt[0]); eti=new JLabel("N_IDENTIDAD"); eti.setBounds(txt[0].getX()+130,txt[0].getY(),100,25); datos.add(eti); txt[1]=new JTextField(); txt[1].setBounds(eti.getX()+90,eti.getY(),170,25); txt[1].addActionListener(this); datos.add(txt[1]); eti=new JLabel("NOMBRE COMPLETO"); eti.setBounds(10,txt[0].getY()+35,130,25); datos.add(eti); txt[2]=new JTextField(); txt[2].setBounds(eti.getX()+130,eti.getY(),340,25); txt[2].addActionListener(this); datos.add(txt[2]); txt[2].addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent e) { String cadena= (txt[2].getText()).toUpperCase(); txt[2].setText(cadena); } }); eti=new JLabel("TELEFONO"); eti.setBounds(10,txt[2].getY()+35,130,25); datos.add(eti); /* agregar[0]=new JButton("AGREGAR"); agregar[0].setBounds(eti.getX()+90,eti.getY(),100,30); agregar[0].addActionListener(this); datos.add(agregar[0]);*/ txt[4]=new JTextField(); txt[4].setBounds(eti.getX()+90,eti.getY(),170,25); txt[4].addActionListener(this); datos.add(txt[4]); JScrollPane des1= new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); des1.setBounds(10,20,500,80); datos2.add(des1); /* vc.Validar_Numeros(txt[1],13); vc.Validar_Letras(txt[2],45); vc.Validar_LetrasCorreo(txt[4],45); vc.Validar_LetrasNumeros(area,500);//*/ // txt[2].addActionListener(this); int bajar=60; for (int i = 0; i<bt.length; i++) { bt[i]=new JButton(tex[i]); bt[i].setBounds(20,bajar,100,40); bt[i].addActionListener(this); panel2.add(bt[i]); bajar+=60; } setSize(750,520); setLocationRelativeTo(null); setVisible(true); } public void guardar_Foto() { // sql.ingreso("insert into fotos values('1',?,'"+txt[0].getText()+"')",ruta,1); } public void sizeL(JLabel l,String p ) { ImageIcon icon =new ImageIcon(p); Image img = icon.getImage(); Image newimg = img.getScaledInstance(l.getWidth(),l.getHeight(), java.awt.Image.SCALE_SMOOTH); ImageIcon newIcon = new ImageIcon(newimg); l.setIcon(newIcon); l.setSize(l.getWidth(),l.getHeight()); //return l; } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here new Clientes(); } }
Ejemplo de selector de archivos en java!!

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.