JavaFX Text and Scroll Pane

 

Text Controls and Scrollpane

 

 

Text Controls

§  JavaFX provides controls that let you enter text.

§  Such controls are important to a GUI because they let the user enter a string of his or her own choosing, rather than selecting a predefined option.

§  text controls provide a solution to a wide array of input situations.

 

§  JavaFX supports three forms of text controls.

o   TextField, which allows one line of text to be entered;

o   TextArea, which supports multiline text;

o   PasswordField, which can be used to input passwords because what is typed is not shown.

§  All three controls inherit TextInputControl, which defines much of the text control’s functionality.

§  supports several capabilities including cut, copy, and paste and deleting and inserting text.

 

§  TextField defines two constructors.

o   default constructor – creates an empty text field that has the default size.

o   specify the initial contents of the field.

 

§  Methods

§  to specify a TextField’s size – setPrefColumnCount( )

     final void setPrefColumnCount(int columns)

§  The columns value is used by TextField to determine its size.

§  to set the text in a text field – setText( )

§  to obtain the current text – getText( )

     final void setText(String str)

     final string getText( )

§  To obtain the anchor (the starting point of the selection) when text is selected – getAnchor( ).

§  clear the content of a text field – clear( ).

§  One especially useful TextField option is the ability to set a prompting message that is displayed inside the text field when the user attempts to use a blank field.

final void setPromptText(String str)

§  str – string displayed in text field when no text has been entered.

§  When the user presses ENTER while inside a TextField, an action event is generated.

§  The PasswordField control works like TextField, except that the characters are not shown when they are typed by the user.

 

§  TextArea is similar to TextField but it also supports multiline editing.

§  ENTER is used to advance the caret to the next line – so it does not generate an action event when ENTER is pressed.

§  TextArea will have a default size, but user can set the size explicitly by using the setPrefColumnCount( ) and setPrefRowCount( ) methods.

§  If the text inside a TextArea exceeds its width or height, scroll bars are added automatically.

 

// JavaFX Controls

// Text controls

 

import javafx.application.*;

import javafx.scene.*;

import javafx.stage.*;

import javafx.event.*;

import javafx.scene.layout.*;

import javafx.scene.control.*;

import javafx.scene.input.*;

import javafx.scene.text.*;

import javafx.geometry.*;

 

public class JfxChk extends Application

{

   public static void main(String []args)

   {

        launch(args);    

   }

  

   public void init(){}

   public void stop(){}

  

  


 

   public void start(Stage myStage)

   {

        myStage.setTitle("JavaFX text controls");

  

        GridPane gp = new GridPane();

        gp.setHgap(50);

        gp.setVgap(50);

        gp.setPadding(new Insets(50,50,50,50));

       

        Label L1 = new Label("Comment : ");

        Label L2 = new Label("Password : ");

        Label L3 = new Label("Text Area :");

       

        TextField tf1 = new TextField();

        tf1.setPrefColumnCount(15);

        tf1.setPromptText("Enter comment...");

       

        PasswordField tf2 = new PasswordField();

        tf2.setPrefColumnCount(15);

        tf2.setPromptText("******");

       

        TextArea tf3 = new TextArea();

        tf3.setPrefColumnCount(15);

        tf3.setPrefRowCount(5);

       

        Button B1 = new Button("Clear");

        Button B2 = new Button("Submit Comment.");

                

        gp.add(L1,0,0);

        gp.add(L2,0,1);

        gp.add(L3,0,2);

        gp.add(B1,0,3);

       

        gp.add(tf1,1,0);

        gp.add(tf2,1,1);

        gp.add(tf3,1,2);

        gp.add(B2,1,3);

       

        StringBuffer str = new StringBuffer();

       

        B1.setOnAction(new EventHandler<ActionEvent>()

        {

             public void handle(ActionEvent ae)

             {

                 tf1.clear();

                 tf2.clear();

                 tf3.clear();

                 str.delete(0,str.length());

             }            

        });

       

        B2.setOnAction(new EventHandler<ActionEvent>()

        {

             public void handle(ActionEvent ae)

             {

                 str.append(tf1.getText()+"\n");

                 tf3.setText(str.toString());

             }            

        });

       

        Scene myScene = new Scene(gp, 450, 450);

       

        myStage.setScene(myScene);

        myStage.show();

   }

}

 

Sample output

    

// JavaFX Controls

// Text controls

 

import javafx.application.*;

import javafx.scene.*;

import javafx.stage.*;

import javafx.event.*;

import javafx.scene.layout.*;

import javafx.scene.control.*;

import javafx.scene.input.*;

import javafx.scene.text.*;

import javafx.geometry.*;

 

public class JfxChk extends Application

{

   public static void main(String []args)

   {

        launch(args);    

   }

  

   public void init(){}

   public void stop(){}

  

   public void start(Stage myStage)

   {

        myStage.setTitle("JavaFX text controls");

  

        FlowPane fp = new FlowPane(Orientation.VERTICAL, 50, 25);

        fp.setAlignment(Pos.CENTER);

        fp.setPadding(new Insets(10));

        Label L1 = new Label("Select Text ... ");

        Label L2 = new Label();

        Label L3 = new Label();

       

        TextField tf1 = new TextField();

        tf1.setPrefColumnCount(15);

        tf1.setPromptText("Enter Text...");

       


 

 

        tf1.setOnMouseReleased(new EventHandler<MouseEvent>()

        {

            public void handle(MouseEvent event)

             {

                String selectedText = tf1.getSelectedText();

                L1.setText("Selected Text: " + selectedText);

                

                 int anchorPosition = tf1.getAnchor();

                L2.setText("Anchor Position: " + anchorPosition);

                

                 int caretPosition = tf1.getCaretPosition();

                L3.setText("caret Position: " + caretPosition);

            }

        });

       

        fp.getChildren().addAll(tf1,L1,L2,L3);

       

        Scene myScene = new Scene(fp, 300, 200);

       

        myStage.setScene(myScene);

        myStage.show();

   }

}

 

Sample output

 


ScrollPane

§  scroll bars are used in situations when the content of a control exceed their dimensions.

§  examples: a large graphics image, such as a photo, may not fit within reasonable boundaries, or the text in a label is longer than will fit within the size allotted for it.

§  JavaFX makes it easy to provide scrolling capabilities to any node in a scene graph.

§  This is accomplished by wrapping the node in a ScrollPane.

§  When a ScrollPane is used, scroll bars are automatically implemented that scroll the contents of the wrapped node.

 

§  ScrollPane defines two constructors.

o   The first is the default constructor. And add the node to be scrolled by calling setContent( ).

              final void setContent(Node content)

 

o   The second lets you specify a node that you want to scroll.

              ScrollPane(Node content)

§  Here, content specifies the information to be scrolled.

 

§  After you have set the content, add the scroll pane to the scene graph. When displayed, the content can be scrolled.

 

§  ScrollPane has a default size is provided

§  The viewable area of a scroll pane is the viewport.

§  It is the area in which the content being scrolled is displayed.

§  Thus, the viewport displays the visible portion of the content.

§  The scroll bars scroll the content through the viewport.

§  Thus, by moving a scroll bar, you change what part of the content is visible.

§  To set the viewport dimensions – two methods:

     final void setPrefViewportHeight(double height)

     final void setPrefViewportWidth(double width)

§  Here, width specifies the width of the viewport and height specifies the height.

§  To obtain the current viewport height and width – getPrefViewportHeight( ) and getPrefViewportWidth( ).

§  In its default behavior, a ScrollPane will dynamically add or remove a scroll bar as needed.


 

§  ScrollPane offers the ability to pan its contents by dragging the mouse.

§  By default, this feature is off.

§  To turn it on, use setPannable( ):

     final void setPannable(boolean enable)

 

§  To set the value of a scroll bar under program control – setHvalue( ) or setVvalue( ). The value determines the position of the scroll bar’s thumb.

     final void setHvalue(double newHval)

     final void setVvalue(double newVval)

 

§  The new horizontal position is specified by newHval, and the new vertical position is specified by newVval. By default, scroll bar positions start at zero.

§  other options.

o   to set the minimum and maximum scroll bar positions.

o   specify when and if the scroll bars are shown by setting a scroll bar policy.

o   Get current position of the scroll bars – getHvalue( ) and getVvalue( ).

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu