Jump to content

Make button direct to link java

Go to solution Solved by Jefroy,

You have to implement ActionListener to your class than add it your button like this:

		button.addActionListener(this); 

than in the actionPerformed you check if the source is your button than open the website:
 

@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button){
			Desktop.getDesktop().browse(new URL("https://google.com").toURI());
		}
		
	}

 

Hi everyone!

 

I have been researching for the past 2 hours on how to open links with java using buttons with the following packages:

import java.awt.*;

import java.net.*;

import javax.swing.*;

import java.awt.event.*;

I have not had any luck!

 

What I have tried so far is this:

		JButton button = new JButton("Push me");

		
		panel.add(button);
		button.addActionListener(openWebPage("https://www.google.com")); 

 

Could someone kindly help me with this? I am not allowed to use constructors, or the private/public stuff (i forget what they are called) and i am not allowed to use the try and catch stuff, as he did not teach these to me yet )

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to comment
https://linustechtips.com/topic/804185-make-button-direct-to-link-java/
Share on other sites

Link to post
Share on other sites

1 minute ago, Jefroy said:

        Desktop.getDesktop().browse(new URL("https://google.de").toURI());
 

Yes, this does open a link, but my goal is to have the button when clicked open the link :(

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

4 minutes ago, manikyath said:

then put it where the button executes said code? ;)

button.addActionListener(Desktop.getDesktop().browse(new URL("https://google.com").toURI()));

 

Does not work dude

 

JButton button = new JButton("Push me");
        
		panel.add(button);
		button.addActionListener(Desktop.getDesktop().browse(new URL("https://google.com").toURI())); 
		JButton button1 = new JButton("Push me");
		panel.add(button1);
		

Throws 12 "quickfix errors", none of which have the solution

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

Just now, babadoctor said:

button.addActionListener(Desktop.getDesktop().browse(new URL("https://google.com").toURI()));

 

Does not work dude

 


JButton button = new JButton("Push me");
        
		panel.add(button);
		button.addActionListener(Desktop.getDesktop().browse(new URL("https://google.com").toURI())); 
		JButton button1 = new JButton("Push me");
		panel.add(button1);
		

 

because thats not how you do things.

 

i suggest you download eclipse and the swt plugin nonsense and look at the code it generates for a button.

Link to post
Share on other sites

1 minute ago, manikyath said:

because thats not how you do things.

 

i suggest you download eclipse and the swt plugin nonsense and look at the code it generates for a button.

>I suggest you download eclipse

T16PjLD.png The main point is I am learning and I have no idea how this works, which is why I asked you guys to kindly explain to me how it works

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

You have to implement ActionListener to your class than add it your button like this:

		button.addActionListener(this); 

than in the actionPerformed you check if the source is your button than open the website:
 

@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button){
			Desktop.getDesktop().browse(new URL("https://google.com").toURI());
		}
		
	}

 

Link to post
Share on other sites

1 minute ago, Jefroy said:

You have to implement ActionListener to your class than add it your button like this:


		button.addActionListener(this); 

than in the actionPerformed you check if the source is your button than open the website:
 


@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button){
			Desktop.getDesktop().browse(new URL("https://google.com").toURI());
		}
		
	}

 

OH SNAP! I just figured this out! I totally forgot we had to check if the button was being pressed with an action listener!!! I was doing this in my calculator project in class too!!!

 

This makes so much sense now! Thank you!

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

51 minutes ago, babadoctor said:

IntelliJ is not working at all

Right...

 

Care to elaborate? Bear in mind that it's widely regarded as the defacto standard in industry for Java development.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

16 hours ago, Nuluvius said:

Right...

 

Care to elaborate? Bear in mind that it's widely regarded as the defacto standard in industry for Java development.

well its too complicated for me someone who picked java up a couple weeks ago

 

 

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

4 hours ago, babadoctor said:

well its too complicated for me someone who picked java up a couple weeks ago

Really? I find that surprising, 'a couple of weeks' is a long time. It was one of the first tools that I reached for and found it to be quite intuitive indeed. It certainly gave me a significant edge over my competition while at uni.

 

Though I suppose everyone's reasons and drive for learning how to write software is different. Some may just need to get through it so that they can forget about it and go after what they really enjoy - 'means to an end'. You must be one such individual no?

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

13 hours ago, Nuluvius said:

Really? I find that surprising, 'a couple of weeks' is a long time. It was one of the first tools that I reached for and found it to be quite intuitive indeed. It certainly gave me a significant edge over my competition while at uni.

 

Though I suppose everyone's reasons and drive for learning how to write software is different. Some may just need to get through it so that they can forget about it and go after what they really enjoy - 'means to an end'. You must be one such individual no?

I don't know any other programming languages, and so it is too difficult for me to understand how this program works. I can not just copy and paste the eclipse code into a new project; it throws weird errors that did not previously exist.

 

>2 weeks is a long time

 

So you are telling me that I should start by using this tool first? My teacher has a strict "Only use eclipse" policy too, so...

 

OFF TOPIC: I suggest every poll from now on to have "**CK EA" option instead of "Other"

Link to post
Share on other sites

9 hours ago, babadoctor said:

I can not just copy and paste the eclipse code into a new project; it throws weird errors that did not previously exist.

That's fine, we can help you through those. In any event the IDEs are just going to operate upon the project. No duplication should be necessary.

9 hours ago, babadoctor said:

My teacher has a strict "Only use eclipse" policy too, so...

Indeed... and if he told you to go and jump off of a bridge or put your hand in a fire? Do you say 'Baaaahhhh'?

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

12 hours ago, babadoctor said:

So you are telling me that I should start by using this tool first? My teacher has a strict "Only use eclipse" policy too, so...

my college had a strict "visual basic" rule which, as a student rep took up the chain to fight my case to allow students to use what ever language they liked so long as i allowed the completion of the work and that it was able to be marked.

 

when your lecturer is wrong point it out, if they get annoyed they are just bad at their job, i had a month long discussion with managers and a lecture about the use of flash in my human interface unit, showing that flash was basically a dead language and that we should be learning in the direction of the industry not against.

 

The lecture argued that although true, system still use flash and that some will need to maintain or update these systems.

 

agreed  to disagree and I finished my unit with no hard feelings, i hated every second of flash lol.

 

That rule is in place because that's all he knows, the choice of IDE shouldn't be an issue the outcome is the same. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×