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?

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.

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

This article has been dead for over six months. Start a new discussion instead.