terça-feira, 15 de novembro de 2016

Array list - TableView

Usando a classe arraylist que faz parte de um conjunto de classe
que forma um flamework chamado collection.
Este conjunto suporta mais recursos em comparação a um array comum
sendo ideal na criação de listas.
Um arraylist não aceita nenhum tipo primitivo da linguagem java
vindo somente aceitar objetos, como String.
Neste exemplo mostro especificamente o método chamado asList que
nos permitiu criar este array que serviu para preenchimento
de uma TableView, aliás esta TableView é chamado por um item de menu,

Veja abaixo algumas imagens do programa em execução:








Veja abaixo o código do programa:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyCode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.scene.control.cell.PropertyValueFactory;
import java.util.Arrays;
import java.util.List;
import javafx.collections.FXCollections;
////////////////////////////////////////////////////////////////////////////////
public class PROJETO extends Application {
public void start ( Stage primaryStage ) {
List < Times > times = Arrays.asList (
                     new Times ( " Palmeiras  ", 70, 34, 21, 7, 6 , 57, 30, 27, 68 ),
                     new Times ( " Santos     ", 64, 34, 20, 4, 10, 53, 29, 24, 62 ),
                     new Times ( " Flamengo   ", 63, 34, 18, 9, 7 , 47, 33, 14, 61 ),
                     new Times ( " Atlético-MG", 60, 34, 17, 9, 8 , 56, 44, 12, 58 ),
                     new Times ( " Botafogo   ", 55, 34, 16, 7, 11, 41, 35, 6 , 53 ),
                     new Times ( " Atlético-PR", 51, 34, 16, 3, 15, 35, 31, 4 , 50 ));
          Canvas canvas = new Canvas ( 490, 300 );
          GraphicsContext ctx = canvas.getGraphicsContext2D ( );
          ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
          ctx.setFill ( Color.RED );
          ctx.fillText ( "TABELA DO BRASILEIRÃO 2016", 120, 80 );
          ctx.setLineWidth ( 22.0 );
          ctx.setStroke ( Color.GREEN );
          ctx.strokeRect ( 10, 35, 440, 220 );
          BorderPane root = new BorderPane ( );
          root.getChildren ( ).add ( canvas );
          Scene scene = new Scene ( root, 460, 265 );
          primaryStage.setScene ( scene );
          primaryStage.setTitle ( "Tabela do Brasileirão 2016" );
          primaryStage.show ( );
          ////////////////////////////////////////////////////////////////////////
          MenuBar m_nu_1 = new MenuBar ( );
          Menu m_nu = new Menu ( "Arq" );
          MenuItem exd = new MenuItem ( "Sair" );
          MenuItem te_d = new MenuItem ( "Tabela" );
          m_nu.getItems ( ).addAll ( te_d, exd );
          ///////////////////////////////////////////////////
          m_nu_1.getMenus ( ).add ( m_nu );
          root.setTop ( m_nu_1 );
          //////////////////////////////////////////////////////////////////////
          exd.setOnAction ( new EventHandler < ActionEvent > ( ) {
                @Override
                public void handle ( ActionEvent e ) {
                     primaryStage.close ( );
                }
          } );
          ////////////////////////////////////////////////////////////////////////
          te_d.setOnAction ( new EventHandler < ActionEvent > ( ) {
                @SuppressWarnings ( "unchecked" )
                @Override
                public void handle ( ActionEvent e ) {
                     TableView < Object > tabela = new TableView < > ( );
                     TableColumn < Object, Object > time = new TableColumn < > (
                               "Times" );
                     TableColumn < Object, Object > ponto = new TableColumn < > (
                               "P" );
                     TableColumn < Object, Object > jogo = new TableColumn < > (
                               "J" );
                     TableColumn < Object, Object > vitoria = new TableColumn < > (
                               "V" );
                     TableColumn < Object, Object > empate = new TableColumn < > (
                               "E" );
                     TableColumn < Object, Object > derrota = new TableColumn < > (
                               "D" );
                     TableColumn < Object, Object > goolspro = new TableColumn < > (
                               "GP" );
                     TableColumn < Object, Object > goolscontra = new TableColumn < > (
                               "GC" );
                     TableColumn < Object, Object > saldodegool = new TableColumn < > (
                               "SG" );
                     TableColumn < Object, Object > porcentagem = new TableColumn < > (
                               "%" );
                     time.setCellValueFactory (
                               new PropertyValueFactory < > ( "time" ) );
                     ponto.setCellValueFactory (
                               new PropertyValueFactory < > ( "ponto" ) );
                     jogo.setCellValueFactory (
                               new PropertyValueFactory < > ( "jogo" ) );
                     vitoria.setCellValueFactory (
                               new PropertyValueFactory < > ( "vitoria" ) );
                     empate.setCellValueFactory (
                               new PropertyValueFactory < > ( "empate" ) );
                     derrota.setCellValueFactory (
                               new PropertyValueFactory < > ( "derrota" ) );
                     goolspro.setCellValueFactory (
                               new PropertyValueFactory < > ( "goolpro" ) );
                     goolscontra.setCellValueFactory (
                               new PropertyValueFactory < > ( "goolcontra" ) );
                    saldodegool.setCellValueFactory (
                               new PropertyValueFactory < > ( "saldodegool" ) );
                     porcentagem.setCellValueFactory (
                               new PropertyValueFactory < > ( "porcentagem" ) );
                     tabela.setItems ( FXCollections.observableArrayList ( times ) );
                     tabela.getColumns ( ).addAll ( time, ponto, jogo, vitoria,
                               empate, derrota, goolspro, goolscontra, saldodegool,
                               porcentagem );
                     Stage subStage = new Stage ( );
                     subStage.setTitle ( "Tabela do brasileirão 2016" );
                     StackPane root = new StackPane ( );
                     root.getChildren ( ).add ( tabela );
                     subStage.setScene ( new Scene ( root, 380, 180 ) );
                     subStage.show ( );
                }
          } );
          exd.setAccelerator ( new KeyCodeCombination ( KeyCode.E,
                     KeyCombination.CONTROL_DOWN ) );
          te_d.setAccelerator ( new KeyCodeCombination ( KeyCode.C,
                     KeyCombination.CONTROL_DOWN ) );
     }
    ////////////////////////////////////////////////////////////////////////////
     public static class Times {
          private String  Times;
          private int          Pontos;
          private int          Jogos;
          private int          Vitorias;
          private int          Empates;
          private int          Derrotas;
          private int          Goolspro;
          private int          Goolscontra;
          private int          Saldodegools;
          private int          Porcentagem;
       /////////////////////////////////////////////////////////////////////////
          public Times ( String time, int ponto, int jogo, int vitoria,
                     int empate, int derrota, int goolpro, int goolscontra,
                     int saldodegool, int porcentagem ) {
                this.Times = time;
                this.Pontos = ponto;
                this.Jogos = jogo;
                this.Vitorias = vitoria;
                this.Empates = empate;
                this.Derrotas = derrota;
                this.Goolspro = goolpro;
                this.Goolscontra = goolscontra;
                this.Saldodegools = saldodegool;
                this.Porcentagem = porcentagem;
          }
        ////////////////////////////////////////////////////////////////////////
          public String getTime ( ) {
                return Times;
          }

          public void setTime ( String time ) {
                this.Times = time;
          }

          public int getPonto ( ) {
                return Pontos;
          }

          public void setPonto ( int ponto ) {
                this.Pontos = ponto;
          }

          public int getJogo ( ) {
                return Jogos;
          }

          public void setJogo ( int jogo ) {
                this.Jogos = jogo;
          }

          public int getVitoria ( ) {
                return Vitorias;
          }

          public void setVitoria ( int vitoria ) {
                this.Vitorias = vitoria;
          }

          public int getEmpate ( ) {
                return Empates;
          }

          public void setEmpate ( int empate ) {
                this.Empates = empate;
          }

          public int getDerrota ( ) {
                return Derrotas;
          }

          public void setDerrota ( int derrota ) {
                this.Derrotas = derrota;
          }

          public int getGoolpro ( ) {
                return Goolspro;
          }

          public void setGoolpro ( int goolpro ) {
                this.Goolspro = goolpro;
          }

          public int getGoolcontra ( ) {
                return Goolscontra;
          }

          public void setGoolcontra ( int goolcontra ) {
                this.Goolscontra = goolcontra;
          }

          public int getSaldodegool ( ) {
                return Saldodegools;
          }

          public void setSaldodegool ( int saldodegool ) {
                this.Saldodegools = saldodegool;
          }

          public int getPorcentagem ( ) {
                return Porcentagem;
          }

          public void setPorcentagem ( int porcentagem ) {
                this.Porcentagem = porcentagem;
          }
     }
    ////////////////////////////////////////////////////////////////////////////
     public static void main ( String [ ] args ) {
          Application.launch ( args );
     }
}

Nenhum comentário:

Postar um comentário

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