quarta-feira, 14 de fevereiro de 2018

Javafx - exemplo de ListView

Um objeto ListView apresenta ao usuário
uma lista de rolagem de itens de texto, 
estas rolagens podem ser orientadas horizontalmente
ou verticalmente como é nosso caso.
"Confira o funcionamento no vídeo do programa"
Apresento aqui um excelente exemplo de como
usar o ListView do javafx.
De propósito usei muitas personalizações em
alguns componentes por isto veio este acréscimo

todo em linhas de código.

Veja abaixo uma imagem do programa em execução:



Veja abaixo o vídeo do programa:

 
Veja abaixo o código do programa:


import javafx.application.Application;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;

public class PROJETO extends Application {
     static BorderPane root = new BorderPane ( );
     static Canvas canvas = new Canvas ( 620, 350 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static Scene scene = new Scene ( root, 620, 350, Color.WHITE );
     static GridPane grid = new GridPane ( );
     static Button Esq_Button = new Button ( "←" );
     static Button Dir_Button = new Button ( "→" );
     static VBox vbox = new VBox ( 5 );  
     static int i, j = 0;
     static String arr [ ] = {
         "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
         "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
         "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
         "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
         "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"};
     static final ObservableList < String > pares = FXCollections
              .observableArrayList ( arr );
     static final ListView < String > paresListView = new ListView <> ( pares );
     static final ObservableList < String > impares = FXCollections
              .observableArrayList ( );
     static final ListView < String > imparesListView = new ListView <> ( impares );
     // /////////////////////////////////////////////////////////////////////////
     public static void Informe ( ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "Por: ", 250, 75 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Samuel Lima", 280, 75 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "sa_sp10@hotmail.com", 250, 90 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void Button_View_1 ( ) {
         Dir_Button.setStyle ( "-fx-font: 15 arial;"
                   + "-fx-background-color: blue;" );
         Dir_Button.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   Dir_Button.setStyle ( "-fx-font: 15 arial;"
                            + "-fx-background-color: pink;" );
              }
         } );
         Dir_Button.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   Dir_Button.setStyle ( "-fx-font: 15 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         Dir_Button.setPrefWidth ( 50 );// Largura do botão
         Dir_Button.setPrefHeight ( 20 );// altura do botão
         Dir_Button.setOnAction ( ( ActionEvent event ) -> {
              String force = paresListView.getSelectionModel ( )
                        .getSelectedItem ( );
              if ( force != null ) {
                   paresListView.getSelectionModel ( ).clearSelection ( );
                   pares.remove ( force );
                   impares.add ( force );
              }
              i++;
              System.out.print ( i );
         } );    
         Dir_Button.setStyle ( "-fx-padding: 3;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 2;" + "-fx-border-insets: 3;"
                   + "-fx-border-radius: 3;" + "-fx-border-color: red;" );
         grid.add ( paresListView, 0, 1 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void Button_View_2 ( ) {
         Esq_Button.setStyle ( "-fx-font: 15 arial;"
                   + "-fx-background-color: blue;" );
         Esq_Button.addEventHandler ( MouseEvent.MOUSE_ENTERED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   Esq_Button.setStyle ( "-fx-font: 15 arial;"
                            + "-fx-background-color: pink;" );
              }
         } );
         Esq_Button.addEventHandler ( MouseEvent.MOUSE_EXITED,
                   new EventHandler < MouseEvent > ( ) {
              @Override
              public void handle ( MouseEvent e ) {
                   Esq_Button.setStyle ( "-fx-font: 15 arial;"
                            + "-fx-background-color: blue;" );
              }
         } );
         Esq_Button.setPrefWidth ( 50 );// Largura do botão
         Esq_Button.setPrefHeight ( 20 );// altura do botão
         Esq_Button.setOnAction ( ( ActionEvent event ) -> {
              String imp = imparesListView.getSelectionModel ( )
                        .getSelectedItem ( );
              if ( imp != null ) {
                   imparesListView.getSelectionModel ( ).clearSelection ( );
                   impares.remove ( imp );
                   pares.add ( imp );
              }
         } );
         Esq_Button.setStyle ( "-fx-padding: 3;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 2;" + "-fx-border-insets: 3;"
                   + "-fx-border-radius: 3;" + "-fx-border-color: red;" );
         grid.add ( imparesListView, 2, 1 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void List_View ( ) {
         ctx.setFont ( Font.font ( "Tahoma", FontWeight.NORMAL, 12 ) );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "Pares", 140, 90 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "Ímpares", 440, 90 );
         ColumnConstraints col = new ColumnConstraints ( 150, 150,
                   Double.MAX_VALUE );
         ColumnConstraints col_1 = new ColumnConstraints ( 50 );
         ColumnConstraints col_2 = new ColumnConstraints ( 150, 150,
                   Double.MAX_VALUE );
         col.setHgrow ( Priority.ALWAYS );
         col_2.setHgrow ( Priority.ALWAYS );
         grid.getColumnConstraints ( ).addAll ( col, col_1, col_2 );
     }
     // /////////////////////////////////////////////////////////////////////////
     @Override
     public void start ( Stage stage ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         stage.setTitle ( "JAVAFX - EXEMPLO DE LISTVIEW" );
         //Criando moldura e dando efeitos
         root.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 12;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: red;" );
         vbox.setStyle ( "-fx-padding: 2;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 3;" + "-fx-border-insets: 2;"
                   + "-fx-border-radius: 2;" + "-fx-border-color: black;" );
         grid.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 2;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: blue;" );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - EXEMPLO DE LISTVIEW", 180, 50 );
         grid.setHgap ( 10 );
         grid.setVgap ( 80 );
         root.getChildren ( ).addAll ( canvas );
         vbox.setAlignment ( Pos.CENTER );
         vbox.getChildren ( ).addAll ( Dir_Button, Esq_Button );
         GridPane.setHalignment ( vbox, HPos.CENTER );
         grid.add ( vbox, 1, 1 );
         root.setCenter ( grid );
         GridPane.setVgrow ( root, Priority.ALWAYS );
         List_View ( );        
         Button_View_1 ( );
         Button_View_2 ( );
         Informe ( );
         stage.setScene ( scene );
         stage.show ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void main ( String [ ] args ) {
         launch ( args );
     }
}
 

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.