2012-08-30 24 views
27

¿Cómo puedo ocultar un elemento en HBox y hacer que el espacio utilizado por este artículo esté disponible para otros artículos?JavaFX HBox ocultar artículo

TitledPane legendPane = new TitledPane("Legend", _legend); 
legendPane.setVisible(false); 
LineChart chart = new LineChart<Number, Number>(_xAxis, _yAxis); 

HBox hbox = new HBox(5); 
hbox.getChildren().addAll(legendPane, chart); 

En el código anterior quiero que el nodo del gráfico use todo el espacio disponible cuando el panel de leyenda está oculto.

Respuesta

57

Antes de llamar legendPane.setVisible, llame a:

legendPane.managedProperty().bind(legendPane.visibleProperty()); 

La propiedad Node.managed impide que un nodo en una Escena afecte el diseño de otros nodos de escena.

+2

Parece más directo que agregar/eliminar el nodo. –

7

Puede eliminar temporalmente de la escena:

legendPane.visibleProperty().addListener(new ChangeListener<Boolean>() { 
    @Override 
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { 
     if (newValue) { 
      hbox.getChildren().add(legendPane); 
     } else { 
      hbox.getChildren().remove(legendPane); 
     } 
    } 
}); 

o manipular su tamaño:

legendPane.visibleProperty().addListener(new ChangeListener<Boolean>() { 
    @Override 
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { 
     if (newValue) { 
      legendPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
      legendPane.setPrefSize(prefWidth, prefHeight); 
     } else { 
      legendPane.setMaxSize(0, 0); 
      legendPane.setMinSize(0, 0); 

     } 
    } 
});