I am using this code in a jbutton to open a webpage from my java application.

try {
         //Set your page url in this string. For eg, I m using URL for Google Search engine
         String url = "http://www.google.com";
         java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
       }
       catch (java.io.IOException e) {
           System.out.println(e.getMessage());
       }

Though, when i click the button twice, it opens a new tab.

It will eventually be used in a loop to open different websites upon click.

is there anyway to stop it from opening a new tab and just open the next webpage on the current tab?

or close out the last tab before opening a new tab?

Recommended Answers

All 4 Replies

You should create a boolean and set it true when you open a webpage. Then, when you try to open it again, test to see if it is true. If it is, don't open the page. If not, go ahead and run the code.

Is this a browser usage problem? Does it work the same way with different browsers?

Member Avatar for hfx642

This is a personal opinion!
I would prefer an application to open a new TAB instead of a new browser WINDOW.
Especially if your code is in a loop.
I absolutely HATE "web bombs"!!!

Though, when i click the button twice, it opens a new tab.

It will eventually be used in a loop to open different websites upon click.

is there anyway to stop it from opening a new tab and just open the next webpage on the current tab?

or close out the last tab before opening a new tab?

You would need to use library such as WebDriver or Selenium

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.