RSS
Facebook
G+

Buscador de IntercambioSOS:


Retroceder   Foro de Ayuda IntercambiosvirtualeS > Soporte Sobre: Educación & Entretenimiento > Zona Académica > Programación
Comunidad

Respuesta
 
Herramientas Desplegado
Antiguo 12-oct-2010     #1
Predeterminado [JAVA] Abrir Archivos con la Clase Desktop

Hola amigos aquí les presento un pequeño programa que muestra como abrir distintos tipos de archivos con la clase Desktop. El programa fué creado por mí!!! Espero que sea de ayuda.







Código PHP:
import java.awt.Color;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Edgar
 */
public class Ventana extends JFrame{

    private 
Desktop dtop;
    private 
JTextField url null;
    private 
JLabel label nulllabel2 null;
    private 
JMenuBar barraMenu null;
    private 
JMenu archivo null;
    private 
JMenuItem abrir null;
    private 
JFileChooser jfc null;
    private 
JPanel panel null;
    private 
String ruta null;

    public 
Ventana(){
        
barraMenu = new JMenuBar();
        
archivo = new JMenu("Archivo");
        
abrir = new JMenuItem("Abrir");
        
        
archivo.add(abrir);
        
barraMenu.add(archivo);
        
this.setJMenuBar(barraMenu);

        
jfc = new JFileChooser();
        
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
url = new JTextField(67);
        
abrir.addActionListener(new manejadorEventos());
        
label = new JLabel("Url:");
        
label.setFont(new Font("Arial",1,12));

        
label2 = new JLabel("http://www.intercambiosos.org/      By RagdE");
        
label2.setFont(new Font("Arial"225));
        
label2.setForeground(Color.red);

        
panel = new JPanel();
        
panel.setLayout(new FlowLayout());
        
panel.add(label);
        
panel.add(url);
        
panel.add(label2);
        
this.setContentPane(panel);

        
this.setSize(800150);
        
this.setDefaultCloseOperation(Ventana.DISPOSE_ON_CLOSE);
        
this.setVisible(true);
    }

    class 
manejadorEventos implements ActionListener {

        public 
void actionPerformed(ActionEvent e) {
            if(
e.getSource() == abrir){
                
AbrirFileChooser();
                if(!
ruta.isEmpty()){
                    
abrirArchivo(ruta);
                }else
                    
JOptionPane.showMessageDialog(null"No se ha elegido ningún archivo!!!""Información"JOptionPane.INFORMATION_MESSAGE);
            }

        }

    }

    public 
void AbrirFileChooser(){
        
ruta null;
        
int opcion jfc.showOpenDialog(Ventana.this);
            if (
opcion == JFileChooser.APPROVE_OPTION) {
                
File file jfc.getSelectedFile();
                
ruta file.getAbsolutePath();
                
url.setText(ruta);
            }
    }

// Este es el Metodo que donde se implementa la clase Desktop

    
public void abrirArchivo(String ruta){
         try {
              
dtop Desktop.getDesktop();
              
File file = new File(ruta);
              
dtop.open(file);
        } catch (
IOException ex) {
              
JOptionPane.showMessageDialog(null"No se pudo abrir el archivo!!!""Información"JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public static 
void main(String[] args){
        
Ventana ventana = new Ventana();
    }



La clase Desktop permite que una aplicacion Java pueda ejecutar cualquier tipo de archivo siempre y cuando esten registrados en el sistema, esta clase tambien ejecuta accesos directos a Urls utilizando el navegador predeterminado de nuestro sistema.

Hasta Pronto!!!
Responder Citando
Los siguientes 3 usuarios agradecen a RagdE por este mensaje:
baduser (12-oct-2010), jchierro (12-oct-2010), wpiaw (13-oct-2010)
Antiguo 12-oct-2010     #2
Predeterminado




Excelente post compañero, muchas gracias... ya me quiero meter a esto del java, es lo que mas $$$$$ deja....jejejeje
Responder Citando
Antiguo 12-oct-2010     #3
Caluroso

Muy interesante....

Gracias por el Tutorial.

Responder Citando
Antiguo 13-oct-2010     #4
Predeterminado

Muy buen Tema! y muy buen trabajo todos los meritos. Saludos



intercambiosvirtuales.org
Enviar un mensaje por MSN a wpiaw Responder Citando
Respuesta

Etiquetas
archivos, desktop, java


(0 miembros y 1 visitantes)
 

Normas de Publicación
No puedes crear nuevos temas
No puedes responder mensajes
No puedes subir archivos adjuntos
No puedes editar tus mensajes

Los Códigos BB están Activado
Las Caritas están Activado
[IMG] está Activado
El Código HTML está Desactivado

Ir al Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Todas las extensiones de los archivos jchierro Tutoriales & Videos Tutoriales 16 06-sep-2011 18:37


Desarrollado por: vBulletin® Versión 3.8.1
Derechos de Autor ©2000 - 2024, Jelsoft Enterprises Ltd.
Ad Management by RedTyger