Foros del Web » Programación para mayores de 30 ;) » Java »

[SOLUCIONADO] Acceder a URL URI

Estas en el tema de Acceder a URL URI en el foro de Java en Foros del Web. Buenas tardes, Estoy iniciandome en programación Java, y me surge un problema con mi primer codigo. Quiero realizar una ventana donde muestre un diagalo para ...
  #1 (permalink)  
Antiguo 16/03/2016, 12:48
 
Fecha de Ingreso: febrero-2015
Mensajes: 55
Antigüedad: 9 años, 2 meses
Puntos: 7
Acceder a URL URI

Buenas tardes,

Estoy iniciandome en programación Java, y me surge un problema con mi primer codigo.

Quiero realizar una ventana donde muestre un diagalo para insertar una direccion URL y al pulsar el boton "Acceder" que entre en la pagina web con el navegador.


Código Java:
Ver original
  1. import java.awt.Desktop;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.IOException;
  6. import java.net.URI;
  7. import java.net.URISyntaxException;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JTextField;
  11. import javax.swing.WindowConstants;
  12.  
  13. public class nav {
  14.    
  15.     private static JFrame ven;
  16.     private static JButton but;
  17.     private static JTextField text1;
  18.  
  19.      public static void main(String[] args) throws Exception {
  20.          
  21.          
  22.             ven = new JFrame ("Acceder URL");          
  23.             ven.getContentPane().setLayout(new FlowLayout());  
  24.             but = new JButton("Acceder");
  25.             ven.getContentPane().add(but);
  26.             text1 = new JTextField(20);
  27.             ven.getContentPane().add(text1);
  28.             text1.setText("http://");
  29.            
  30.             String direccion = text1.getText();
  31.            
  32.             but.addActionListener(new ActionListener(){
  33.                
  34.                 public void actionPerformed(ActionEvent e) {
  35.  
  36.  
  37.                     if (Desktop.isDesktopSupported()) {
  38.                         // Windows
  39.                         try {
  40.                             Desktop.getDesktop().browse(new URI(direccion));
  41.                         } catch (IOException e1) {
  42.                             // TODO Auto-generated catch block
  43.                             e1.printStackTrace();
  44.                         } catch (URISyntaxException e1) {
  45.                             // TODO Auto-generated catch block
  46.                             e1.printStackTrace();
  47.                         }
  48.                     } else {
  49.                         // Ubuntu
  50.                         Runtime runtime = Runtime.getRuntime();
  51.                         try {
  52.                             runtime.exec("/usr/bin/firefox -new-window " + direccion);
  53.                         } catch (IOException e1) {
  54.                             // TODO Auto-generated catch block
  55.                             e1.printStackTrace();
  56.                         }
  57.                     }
  58.                    
  59.                 }
  60.             }
  61.            
  62.             );
  63.        
  64.         ven.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  65.        
  66.         ven.pack();
  67.        
  68.         ven.setVisible(true);
  69.     }
  70.   }

El problema creo que lo tengo en la linea
Código Java:
Ver original
  1. text1.setText("http://");

Ya que si indico directamente la url en text1, si que accede a la pagina web.
Es decir, no está retornando el valor de text1 que es donde indico la pagina web.

Espero me puedan ayudar.

Muchas gracias!
  #2 (permalink)  
Antiguo 17/03/2016, 01:28
Avatar de Profesor_Falken  
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: Acceder a URL URI

Buenas,

Prueba a asignar la direccion desde dentro del evento:

Código Java:
Ver original
  1. public void actionPerformed(ActionEvent e) {
  2.  
  3. String direccion = text1.getText();
  4. .......

Un saludo
__________________
If to err is human, then programmers are the most human of us
  #3 (permalink)  
Antiguo 17/03/2016, 04:40
 
Fecha de Ingreso: febrero-2015
Mensajes: 55
Antigüedad: 9 años, 2 meses
Puntos: 7
Respuesta: Acceder a URL URI

Muchísimas gracias Profesor_Falken !!

Era un problema de estructura del código... Cambiando la variable dirección dentro del evento, si que selecciona correctamente la variable.


Muchas gracias de nuevo.

Un saludo :)

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:09.