2016-12-18 4 views
0

Kann ich die TextArea Steuerung auf automatisch die Höhe zu erweitern?JavaFX: TextArea automatische Höhe Erweiterung ohne Bildlaufleiste

Im folgenden Fall würde Ich mag die Bildlaufleiste am ScrollPane Kontrolle, um zu sehen, überhaupt nicht TextArea- Kontrolle.

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.geometry.Insets?> 
<?import javafx.scene.control.ScrollPane?> 
<?import javafx.scene.control.TextArea?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.VBox?> 

<ScrollPane fitToHeight="true" fitToWidth="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
      minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.91" xmlns:fx="http://javafx.com/fxml/1" 
      fx:controller="sample.Controller"> 
    <VBox style="-fx-background-color: bisque"> 
     <TextField/> 
     <TextArea VBox.vgrow="ALWAYS"> 
      <VBox.margin> 
       <Insets top="20.0"/> 
      </VBox.margin> 
     </TextArea> 
    </VBox> 
</ScrollPane> 

TextArea expanding the height

+0

Korrigieren Sie mich, wenn ich falsch liege, ist Ihre 'TextArea' in einer' ScrollPane'? Wenn das der Fall ist, sehe ich kein Interesse, 'TextArea' hat bereits eine eigene' ScrollBar'. Sie möchten immer noch die 'ScrollBar' davon verstecken und die' ScrollPane' anzeigen, wenn die 'TextArea' erweitert wird? –

+0

Ja, genau. Der Benutzer scrollt dann mit dem gesamten Inhalt ('ScrollPane') und nicht nur mit dem' TextArea'-Inhalt. – David

+0

@BoHalim Ich habe illustrierendes Bild hinzugefügt. – David

Antwort

1

Denn jetzt die einzige Lösung, um Ihr Problem zu schließen ist gegeben durch @Uluk Biy, dass ich here gefunden, was ich tat, nur ist seine Logik passen, und blenden Sie die ScrollBars . Das einzige Problem die Größe der TextArea ist, die zu dem den Text binded und so beginnt er mit einem minimum height am Anfang der Ausgabe, hier ist der komplette Code:

import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.geometry.Bounds; 
import javafx.scene.Scene; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.control.ScrollPane.ScrollBarPolicy; 
import javafx.scene.control.TextArea; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.Pane; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 

public class Launcher extends Application{ 

private Pane root = new Pane(); 
private Scene scene; 

private ScrollPane scroller; 
private Pane content = new Pane(); 

private TextField textF = new TextField(); 
private TextArea textA = new TextArea(); 

private Text textHolder = new Text(); 
private double oldHeight = 0; 


@Override 
public void start(Stage stage) throws Exception { 

    root.getChildren().addAll(yourSP()); 
    scene = new Scene(root,300,316); 
    stage.setScene(scene); 
    stage.show(); 

} 

private ScrollPane yourSP(){ 

    content.setMinSize(300, 300); 
    textF.setPrefSize(260, 40); 
    textF.setLayoutX(20); 
    textF.setLayoutY(20); 
    textA.setPrefSize(260, 200); 
    textA.setLayoutX(20); 
    textA.setLayoutY(80); 

    textA.getStylesheets().add(getClass().getResource("texta.css").toExternalForm()); 
    content.getChildren().addAll(textA,textF); 

    /*************************@Uluk Biy Code**************************/ 
    textA.setWrapText(true); 
    textHolder.textProperty().bind(textA.textProperty()); 
    textHolder.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() { 
     @Override 
     public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) { 
      if (oldHeight != newValue.getHeight()) { 
       oldHeight = newValue.getHeight(); 
       textA.setPrefHeight(textHolder.getLayoutBounds().getHeight() + 20); 
       System.out.println(textHolder.getLayoutBounds().getHeight()); 
      } 
     } 
    }); 
    /****************************************************************/ 

    scroller = new ScrollPane(content); 
    scroller.setHbarPolicy(ScrollBarPolicy.NEVER); 
    scroller.setPrefSize(300, 316); 


    return scroller; 
} 


public static void main(String[] args) { 

    launch(args); 

    } 

} 

Natürlich ist der Code

.text-area > .scroll-pane{ 

-fx-hbar-policy:never; 
-fx-vbar-policy:never; 

} 

viel Glück für die Fortsetzung: kann fxml Format angepasst werden, habe ich genug Zeit, es zu tun, und hier ist der Stil der TextArea einfach nicht hatte!