Click here to Skip to main content
15,038,451 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made this script with gui to open url. Here is the script -

Java
import javax.swing.*; 
import java.io.*;
import java.awt.*; 
import java.awt.event.*;  
import java.awt.Desktop;
import java.net.URI;
import java.awt.Desktop;
import java.net.URL;
class TextFieldExample
{  
public static void main(String args[])  
    {  
    JFrame f= new JFrame("TextField Example");  
    JTextField turl;  
    turl=new JTextField();  
    turl.setBounds(100,50, 300,50);  

 Font font = new Font("Georgia", Font.BOLD,20);
 turl.setFont(font);
    
    
String url=turl.getText(); 


JButton b1=new JButton(new ImageIcon("button.png"));  
b1.setBounds(138,100,230,60); 

b1.addActionListener(new ActionListener() {

    
    public void actionPerformed(ActionEvent ae) {

try {
  Desktop desktop = java.awt.Desktop.getDesktop();
  URI oURL = new URI(url);
  desktop.browse(oURL);
} catch (Exception e) {
  e.printStackTrace();
}



}});

    
    


f.add(b1);
f.add(turl); 
f.setSize(500,300);  
f.setLayout(null);  
f.setVisible(true);//56 

 



    }  
    }


But when I run this and enter a url, It is opening the file explorer. Can Someone find a solution. Please help me. I am new to java. But I know python. But I am Studying java because its good for internet. If you find a solution please say.

What I have tried:

I have tried many. But not working.
Posted
Updated 3-Jan-21 23:43pm
v2
Comments
Richard MacCutchan 4-Jan-21 3:56am
   
What is the content of the url string?
Mr.Corona 4-Jan-21 4:03am
   
https://meet.google.com
Richard MacCutchan 4-Jan-21 5:26am
   
I just tried that and it worked correctly, but only when I run it from a console app. More investigating to be done. BTW if you want to reply to my messages then please use the Reply button above the message.
Richard MacCutchan 4-Jan-21 5:44am
   
See Solution below.
Mr.Corona 7-Jan-21 0:40am
   
Sir I am new to java and I don't have an IDE. My Parents are not allowing to install an IDE. I am writing my code always in notepad. ­čś¬­čś░

1 solution

The problem is simply that you do not have a URL in your call to desktop.browse(oURL);.

You try to capture the contents of turl at the time that you are creating the frame window, but there is no text there at that point. You should change your code to call String url=turl.getText(); inside the button handler, thus:
Java
b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        try {
            String url = turl.getText(); // capture the URL when the user presses the button.
            Desktop desktop = java.awt.Desktop.getDesktop();
            URI oURL = new URI(url);
            desktop.browse(oURL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
   
Comments
Mr.Corona 7-Jan-21 0:41am
   
Thanks Sir helped me a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900