Java Noob
Java Noob

Reputation: 371

Javafx Text area disable scroll bar

At the moment, I'm making a lw interpreter for a programming language. http://www.muppetlabs.com/~breadbox/bf/ Anyways, I'm trying to implement a text area, but I want the scroll bars to be hidden. I've looked at multiple tutorials to do this, but none of them seemed to work. Here is the code for my fxml window

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

<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<SplitPane dividerPositions="0.6231155778894473" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="interpreter.Window">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0">
         <children>
            <HBox layoutX="188.0" layoutY="81.0" prefHeight="262.0" prefWidth="599.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="-1.0" AnchorPane.topAnchor="0.0">
               <children>
                  <Pane prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <Button fx:id="executor" layoutX="64.0" layoutY="64.0" mnemonicParsing="false" onAction="#onExecute" text="Execute" />
                        <TextField fx:id="input" layoutX="15.0" layoutY="105.0" promptText="input" />
                        <ComboBox fx:id="byteSize" layoutX="23.0" layoutY="153.0" prefWidth="150.0" promptText="Byte Size" />
                        <TextField fx:id="bytes" alignment="CENTER_RIGHT" layoutX="25.0" layoutY="205.0" prefHeight="27.0" prefWidth="91.0" promptText="Bytes" text="30000" />
                        <Text layoutX="39.0" layoutY="41.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Control Center" textAlignment="CENTER">
                           <font>
                              <Font size="17.0" />
                           </font>
                        </Text>
                        <Text layoutX="122.0" layoutY="222.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Bytes" />
                        <Line endX="100.0" layoutX="98.0" layoutY="261.0" startX="-100.0" />
                     </children>
                  </Pane>



                    <TextArea fx:id="errorLog" disable="true" editable="false" prefHeight="262.0" prefWidth="38.0" style="-fx-text-fill: #ff0000" />
                     //this is the one where I want the scroll bars hidden



                  <TextArea fx:id="code" prefHeight="262.0" prefWidth="361.0" promptText="Code" HBox.hgrow="ALWAYS" />
               </children>
            </HBox>
         </children>
      </AnchorPane>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
             <TextArea fx:id="output" disable="true" editable="false" layoutX="76.0" layoutY="-60.0" prefHeight="166.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
         </children>
      </AnchorPane>
  </items>
</SplitPane>

I have it specified which one I want to hide the scroll bar's of. Does anybody have any insight to this? Thanks.

Upvotes: 3

Views: 3971

Answers (3)

Alastair Cooper
Alastair Cooper

Reputation: 82

To prevent repeating links to the css all over my project, I extended the TextArea to create a very simple 'vertical-scroll' only TextArea object. I attached the css to the object itself. The vertical scrollbar is visible even when unnecessary because I like it that way. To only show when needed, you can change 'always' to 'as-needed' in the css call to vbar-policy.

I also added a bit of padding to centre the text vertically, although this will obviously need to change with a different font size


package sample.controls;

import javafx.scene.control.TextArea;

public class VertOnlyTextArea extends TextArea {

    public VertOnlyTextArea(){
        setWrapText(true);
        getStylesheets().add("/css/vertOnlyTextArea.css");
    }
}

in the css directory... vertOnlyTextArea.css


.text-area .scroll-pane {
    -fx-hbar-policy: never;
    -fx-vbar-policy: always;
}
.text-area .content{
    -fx-padding: 8 0 0 8;
}

hope this helps

Upvotes: 0

Ala Eddine Menai
Ala Eddine Menai

Reputation: 2880

There are many ways to keep your need.

By using Css : in this CSS i suppose that error is id of TextArea

Fxml :

<TextArea id="error" fx:id="errorLog" disable="true" editable="false" prefHeight="262.0" prefWidth="38.0" style="-fx-text-fill: #ff0000" />

CSS :

#error .scroll-pane {
    -fx-hbar-policy: never;
    -fx-vbar-policy: never;
}

or

#error .scroll-pane{
    -fx-padding:0

}

These styles give you the same result.

By using FXML : I added wrapText="true"

  <TextArea id="error" fx:id="errorLog" disable="true" editable="false" prefHeight="262.0" prefWidth="38.0" style="-fx-text-fill: #ff0000" wrapText="true"/>

Upvotes: 0

Adam
Adam

Reputation: 36743

This can be done by setting the horizontal+vertical scrollbar policies on the inner ScrollPane. I'm currently unsure if this can be done via FXML or Java API, however it can be achieved via CSS.

See CSS guide on ScrollPane

#errorLog .scroll-pane {
    -fx-hbar-policy: never;
    -fx-vbar-policy: never;
}

This assumes the CSS sub-structure of TextArea, but this is well-defined in the official CSS gudie for TextArea

scroll-pane — ScrollPane
    content — Region

If you're not using CSS already, you'll need to define a CSS file and add it to the Scene or FXML. Information on how to do this can be found in Styling UI Controls with CSS.

Upvotes: 4

Related Questions