Dynamically new tabs, add a tab with a button pane

In most modern browsers, it is very easy and intuitive how to open a new tab in the same window.
Usually, there is a small button with a "+" to the right of the tabs. When you click this button, a new tab is created and it receives focus.

What is the easiest way to mimic this behavior in a JavaFX application?. I think trying to add a tab 'false' with a sign «+» Then I know when this tab is clicked, and on this event, I could dynamically create a new tab and give it focus.
What is the right way to do it? A small example of how best to implement it would be greatly appreciated.

This seems to work ok:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class TabPaneWithAddButton extends Application {

  @Override
  public void start(Stage primaryStage) {
    final AnchorPane root = new AnchorPane();
    final TabPane tabs = new TabPane();
    final Button addButton = new Button("+");

    AnchorPane.setTopAnchor(tabs, 5.0);
    AnchorPane.setLeftAnchor(tabs, 5.0);
    AnchorPane.setRightAnchor(tabs, 5.0);
    AnchorPane.setTopAnchor(addButton, 10.0);
    AnchorPane.setLeftAnchor(addButton, 10.0);

    addButton.setOnAction(new EventHandler() {
      @Override
      public void handle(ActionEvent event) {
        final Tab tab = new Tab("Tab " + (tabs.getTabs().size() + 1));
        tabs.getTabs().add(tab);
        tabs.getSelectionModel().select(tab);
      }
    });

    root.getChildren().addAll(tabs, addButton);

    final Scene scene = new Scene(root, 600, 400);
    scene.getStylesheets().add("tabs.css");
    primaryStage.setScene(scene);
    primaryStage.show();

  }

  public static void main(String[] args) {
    launch(args);
  }
}

with the css (tabs.css) file:

@CHARSET "US-ASCII";

.tab-header-area {
  -fx-padding: 0 0 0 50 ;
}

Tags: Java

Similar Questions

  • I don't want the new tab with apps opening when I open Firefox, how to fix this?

    I want to just the home page to open without the new tab with a search bar on this issue.

    Sometimes the home page changes are caused by a new extension or external software change your Firefox settings. These can sneak in when you install other software, as many free programs include a set of less useful things.

    Could check you your extensions for the actions essential or unrecognized? Open the page modules using either:

    • CTRL + SHIFT + a
    • "3-bar" menu button (or tools) > Add-ons

    In the left column, click Extensions. Then, when in doubt, turn off. Or if it's obviously nonsense, delete.

    If the button Delete is absent for an extension, you can usually find a way to remove it using the Control Panel, uninstall a program (or all what is it called now to the point Windows 8.1). By clicking on the column heading to sort by date, "installed on" is useful to find and remove unknown elements not trustworthy bundle or other additions.

    Once your system is clean, your preferred homepage appears, either immediately or after the update the setting in Options?

  • Every time Firefox opens, I get new tab, with my desired as a tab page I click on get to. How to get back to how it was in previous versions?

    After updating to the latest version of Firefox, when it opens I get two tabs, and one that appears on my screen is new tab with my homepage on the other, which requires that I click on it to go. Also, when I click on a link, the same thing happens, a new tab page comes up, with what I clicked on as another tab I have to click to get to. It's actually quite boring and inconvenient. How to get back to the way it was, so that when I run Firefox it opens on my homepage, and when a new page opens, I see the content of the link I clicked on? This new and improved IMO is really new and worse.

    A further extension could have snuck in. These can be bundled with other software you may have installed recently.

    Try turning off all non-essential or unrecognized extensions on page modules. Either:

    • CTRL + SHIFT + a
    • Firefox orange (or the Tools menu) button > Add ons

    In the left column, click Extensions. Then, when in doubt, turn off. (Or so completely junk, delete).

    Typically, a link will appear above at least an extension disabled to restart Firefox. You can complete your work on the tab and click one of the links in the last step.

    That get rid of the extra tab?

    Note: I also suggest to visit the Control Panel, uninstall a program and by clicking on the column heading "installed on". This should bring the most recent additions at the top and group by install date so that you can examine the software may have unexpected how have been installed.

  • I can not open a new tab (empty) I change all the settings. However as I said well can I open a new tab with a link without a link, I can't open a new (empty) tab.

    I can not open a new tab (empty) I change all the settings. However as I said well can I open a new tab with a link without a link, I can't open a new (empty) tab.

    Uninstall the toolbar Ask and it should work again. There is a compatibility issue with the Ask and Firefox toolbar that prevents the opening of new tabs.

    There are a few places to check for the Ask toolbar:

  • When I search something, firefox opens a new tab with the results. How can I change the settings to where it will stay all in the same tab?

    When I search something Firefox automatically opens a new tab with the results of this research. How can I change the settings to where the results will appear in the same tab where I made the research?

    If you want to open search results in a new window most often is a parameter of the search engine you use.

  • I can't stop the pop-up and adware ads in Safari. When I opened a new take, it is locked until I click a place twice, then two new tabs with advertising and virus detected by safari emerge

    I can't stop the pop-up and adware ads in Safari. When I opened a new take, it is locked until I click a place twice, then two new tabs with advertising and virus detected by safari emerge

    I tried following the instructions on the support page, in Apple, but it did not work.

    Force to leave Safari, then with the SHIFT key, restart Safari.

    Also use EtreCheck of www.etrecheck.com and see what else is running.

  • Last update adds a new tab with social media links to the exsisting homepages. How can I stop this?

    When I open Firefox, my home pages (4) shall, after a slight delay, a new tab appears with links to social media and the new tab is active. I want to stop the new tab to open!

    Uh-oh, you may have installed a non official package containing the update. If your download history indicates the source, can report you here?

    Your "more system details' shows the extension MySearchDial. I recommend you to remove that from the addons page (if possible) or from the control panel.

    Either:

    • CTRL + SHIFT + a
    • Firefox orange (or the Tools menu) button > Add ons

    In the left column, click Extensions. Then find MySearchDial and remove (or at least turn it off).

    If necessary, open the Control Panel, uninstall a program, then click on "installed on" heading of the column to bring recent infections, uh, additions, upward.

    Hopefully he cleaning?

  • The last version doesnot open new tab with google even after editing about: config

    Until two days ago, that the new tab opened with Google.com. After the introduction of tiles in a new tab where the tiles with suggested sites are displayed, even after editing with subject: config, there is no change. Set in a new tab button suggested only the sites or blank page that crushes everything: config it seems

    Firefox has tiles on the new tab from Firefox 13 page. But something has just changed: 41 Firefox no longer use the browser.newtab.url parameter in topic: config because he was constantly attacked by malware. From 41 of Firefox, you need an add-on to change the new tab page.

    Here are a few options (I'm learning more all the time):

    • If you are already using the "Restorer of classic theme" or "Tab Mix Plus" extension: There is an option somewhere (!) in his dialogues of the parameters to select another page in the new tab.

    Use only one of these methods at the same time, otherwise there may be conflicts and dysfunctions.

    Setting up substitute new tab and new tab custom

    After installing one of these extensions, you must use the Options page to set the new desired tab page (instead of use subject: config).

    Open the page modules using either:

    • CTRL + SHIFT + a (Mac: Cmd + shift + a)
    • "3-bar" menu button (or tools) > Add-ons

    In the left column, click Extensions. Then on the side right, find the new tab replace or custom new tab and click on the Options button. (See first screenshot attached).

    New tab Override

    Depending on the size of your screen, you may need to scroll down to enter the address in the form. (See second screenshot attached). For example:

    • (Default) page thumbnails = > subject: newtab
    • Blank tab = > subject: empty
    • Built-in Firefox homepage = > topic: welcome
    • Any other page = > full URL of the page

    Then tab or click this form field and you can test using Ctrl + t.

    Custom new tab

    It is easy to enter your favourite homepage address. If you check either of the boxes, the expansion will set the focus in the page as well, for example, the cursor will be in the Google search box. (See third screenshot attached). I had to leave Firefox and start it up again before new custom tab started working, but this may have been due to tests so many extensions back to back.

    Success?

  • Why can't I just open a new tab with the "Plus" sign or "command-T"?

    If I want to open a new tab, I either press 'Command T' or select 'New tab' in the file menu, or click on the little "plus" sign in my tab bar - right? None of them work. What Miss me?

    This problem is Firefox on your Mac?

    Perhaps users have discovered that this problem is caused by led modules, for example a plugin Conduit and/or an extension "Community Toolbar".

    You can check and disable the extensions on page modules. Either:

    • Command + shift + a
    • "3-bar" menu button (or tools) > Add-ons

    In the left column, click Extensions. Then, when in doubt, turn off.

    Often, a link will appear above at least an extension disabled to restart Firefox. You can complete your work on the tab and click one of the links in the last step.

    What helps with the button "+"? If so, you can name the specific extension that caused the problem?

    You can also see the Plugins page modules. You can never use 'activate' to disable plugins that you do not usually use.

  • How to get the new tab with the SAME window, by clicking on the + sign for new tab

    Firefox, windows 8, toshiba labtop

    • Hold CTRL and left click or middle - click the button reload on the address bar to open the current page in a new tab.
    • Hold Ctrl and left click or click middle points in the history of the tab to the buttons back and forward to open a page from this list in a new tab
      You can hold down the left button of the mouse on the back or next button to open the tab history.
    • Duplicate a tab with its history by pressing the CTRL key (Mac: Option) and dragging the tab in a new position on the tab bar.
  • I'm having a problem when I open a new tab with multiple images. If there is more than five or six images they all will not be open.

    I opened emails with many images. New version is having a problem opening a tab with images more than maybe 6 or 7. Of older versions sometimes had the same problem, but the images unopened is appeared on the page as a small box empty you can try to reload. No more. Now you must reload the tab all. Sometimes it works sometimes it doesn't.

    You seem to be using a beta version. Maybe go back to a regular version and wait for V. 30 becoming a regular version, and maybe some bug.

  • Everything by opening a new tab with the Alt + Enter command, is it possible to open next to the current tab and not in the end?

    No indication on how to achieve this will be very welcome.

    There is not an integrated framework for the opening of new tabs "unrelated" everywhere except at the end of the tab bar.

    Various add-on authors tackled this request over the years. It is not really clear to me that these extensions are still working, but you might check the reviews and see what you think. (I have not tried these myself)

  • Why did FF open image in a new tab with focus and black background?

    Whenever I used to open image in a new tab, it would always appear in the upper left corner, with a white background. Since my upgrade to 11.0, it is now focused on the force with a black background, I hate. How can I change this?

    This is a new feature in Firefox to enhance the visibility of the images open in a new tab. However, if you want to disable, you can install https://addons.mozilla.org/en-US/firefox/addon/old-default-image-style/, which will return the change for you.

  • I can't open new tabs with button more. Help?

    I used to be able to open a new tab empty with the + button next to my line of tabs, but this button is is more highlighted.

    This problem may be caused by the Ask.com toolbar (Tools > Modules > Extensions)

    The details of your system display this toolbar to the user as AskTbFF/3.12.2.16749 agent

  • I can't open a new tab with the keyboard shortcut or the sign thing more next to an already open tab to open another. Anyone know how to fix this?

    It was not until last night after I've updated for Firefox 3.6. So now I have use right clicking and then choosing to open a new tab from here.

    What can a problem with the Ask.com toolbar (Tools > Modules > Extensions)

    See:

Maybe you are looking for

  • Highlight the text and text blocks

    Is it just me, or is this common, or worse, the new 'normal' for the Pages app. All of a sudden I can't highlight a block of text. I can select 'ALL' the text 'A command' and delete everything, but I can't select (highlight) a character, a block of c

  • Satellite P100-222 - graphic card crashed - alternatives?

    Video card NVIDIA GeForce Go 7900 GS 512 MB broke down for my laptop Toshiba Satellite P100-222. I found the video card printed on this ID: DA0BD1U8AD9. I have searched to find this card on eBay or Google but could find based on reasonable prices.How

  • _ Cant find Lexar USB drive with password reset

    Can't get the usb key with the password to the computer on this subject. I did everything. How can we me and my husband reach the computer.  Any advice would be appreciated. Go on ASAP Thanks for your replies. BTW, we have windows 7 and Acer laptop f

  • is the GPU of NVIDIA chip on the motherboard for HP Pavilion dv9420us

    My HP Pavilion DV9420us just went 'all white' and now won't show anything.  It also keeps cycling on and outside. I just read that a class action has been filed last year (2010) against NVIDIA on GPU chipset.  It's time to add my complaint to the tri

  • download path

    When my daughter downloads photos they are available in the public record. Why?