javafx - Display label if TabPane has no tabs -


i want display label empty @ center:

private tabpane tabpane = new tabpane(); private text placeholder = new text("empty"); placeholder.setfont(font.font(null, fontweight.bold, 20)); placeholder.visibleproperty().bind(bindings.isempty(tabpane.getchildren())); 

what proper way implement this?

tabpane not have ready-to-use placeholder tableview does. may try 3 different approaches:

1) using custom skin, , set via -fx-skin.

package my.skin;  import com.sun.javafx.scene.control.skin.tabpaneskin;  import javafx.geometry.pos; import javafx.scene.node; import javafx.scene.control.label; import javafx.scene.control.tabpane; import javafx.scene.layout.pane; import javafx.scene.layout.vbox; import javafx.scene.text.font; import javafx.scene.text.fontweight;   public class customtabpaneskin extends tabpaneskin {      private final vbox placeholder;     private final label placeholdertext;       public customtabpaneskin( tabpane tabpane )     {         super( tabpane );          placeholdertext = new label( "empty" );         placeholdertext.setfont( font.font( null, fontweight.bold, 20 ) );         placeholdertext.setalignment( pos.center );          placeholdertext.minwidthproperty().bind( getskinnable().widthproperty() );         placeholdertext.minheightproperty().bind( getskinnable().heightproperty() );          placeholder = new vbox( placeholdertext );          ( node node : getchildren() )         {             if ( node.getstyleclass().contains( "tab-header-area" ) )             {                 pane headerarea = ( pane ) node;                 // header area hidden if there no tabs, when tabpane "empty"                 headerarea.visibleproperty().addlistener( ( observable, oldvalue, newvalue )                         ->                          {                             if ( newvalue )                             {                                 getchildren().remove( placeholder );                             }                             else                             {                                 getchildren().add( placeholder );                             }                         }                 );                  break;             }         }     }  } 

and in custom css file

.tab-pane {   -fx-skin: "my.skin.customtabpaneskin"; } 

that's all. use tabpanes in usual way.

2) using custom wrapper holds tabpane , placeholder

@override public void start( stage primarystage ) {     tabpaneholder tabpaneholder = new tabpaneholder( new tab( "tab 1" ) );     tabpaneholder.gettabs().add( new tab( "tab 2" ) );      scene scene = new scene( tabpaneholder, 350, 200 );     primarystage.setscene( scene );     primarystage.show(); }  public class tabpaneholder extends vbox {     private final tabpane tabpane;     private final text placeholder;      public tabpaneholder( tab... tabs )     {         this( "empty", tabs );     }      public tabpaneholder( string emptymessage, tab... tabs )     {         this.tabpane = new tabpane( tabs );         placeholder = new text( emptymessage );         placeholder.setfont( font.font( null, fontweight.bold, 20 ) );         booleanbinding bb = bindings.isempty( tabpane.gettabs() );         placeholder.visibleproperty().bind( bb );         placeholder.managedproperty().bind( bb );         getchildren().addall( placeholder, tabpane );          alignmentproperty().bind( bindings.when( bb ).then( pos.center ).otherwise( pos.top_left ) );     }      public observablelist<tab> gettabs()     {         return tabpane.gettabs();     } } 

3) simple, direct approach

@override public void start( stage primarystage ) {     tabpane tabpane = new tabpane();     tabpane.gettabs().add( new tab( "tab" ) );     text placeholder = new text( "empty" );     placeholder.setfont( font.font( null, fontweight.bold, 20 ) );     booleanbinding bb = bindings.isempty( tabpane.gettabs() );     placeholder.visibleproperty().bind( bb );     placeholder.managedproperty().bind( bb );      scene scene = new scene( new vbox( placeholder, tabpane ), 350, 200 );     primarystage.setscene( scene );     primarystage.show(); } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -