o combox do javafx.
Neste exemplo estamos comparando os valores
declarados no combox com os valores carregados
no array bidimensional de inteiros,
que foi preenchido com os 100 primeiros
números naturais.
Este array foi passado como parâmetros para
o método Combo_box ( int Vet [ ] [ ] );
Na posição 2 temos o número 1, e assim é em
todas as posições, sucessivamente.
O que chama a atenção, é que os números são
imprimidos aleatoriamente na posição que
coresponde ao seu valor e isto é perfeito.
Veja abaixo uma imagem do programa em execução:
Veja abaixo o código do programa:
//COMBOX MUITO BOM
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
public class PROJETO extends Application {
public static int TAM = 10;
static Canvas canvas = new Canvas ( 600, 340 );
static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
static Pane root = new Pane ( );
static Scene scene = new Scene ( root,
600, 340 );
static GridPane grid = new GridPane ( );
static Button btn_1 = new Button ( "FECHAR" );
@SuppressWarnings ( "rawtypes" )
static final ComboBox cb = new ComboBox ( );
//
/////////////////////////////////////////////////////////////////////////
public static Node Sair ( ) {
btn_1.setPrefWidth ( 100 );// Largura do botão
btn_1.setLayoutX ( 267 );// Posição do botão
coluna
btn_1.setPrefHeight ( 20 );// altura do botão
btn_1.setLayoutY ( 300 );// //Posição do botão
linha
btn_1.setStyle ( "-fx-padding:
1;" + "-fx-border-style: solid
inside;"
+ "-fx-border-width: 2;" + "-fx-border-insets:
1;"
+ "-fx-border-radius: 1;" + "-fx-border-color:
red;"
+ "-fx-font-style: italic;" + "-fx-font-family:
Consolas;" );
btn_1.setOnKeyPressed ( e -> {
if ( e.getCode ( ) == KeyCode.ENTER ) {
System.exit ( 0 );
}
} );
return btn_1;
}
//
/////////////////////////////////////////////////////////////////////////
@SuppressWarnings ( "unchecked" )
public static void Combo_box ( int Vet [ ] [ ] ) {
cb.setStyle ( "-fx-padding:
5;" + "-fx-border-style: solid
inside;"
+ "-fx-border-width: 2;" + "-fx-border-insets:
5;"
+ "-fx-border-radius: 5;" + "-fx-border-color:
black;" );
//total de linhas visíveis
cb.setVisibleRowCount ( 10 );
cb.getItems ( ).addAll (
"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",
"51", "52", "53", "54", "55", "56", "57", "58","59", "60",
"61", "62", "63", "64", "65", "66", "67", "68","69", "70",
"71", "72", "73", "74", "75", "76", "77", "78","79", "80",
"81", "82", "83", "84", "85", "86", "87", "88","89", "90",
"91", "92", "93", "94", "95", "96", "97", "98","99", "100", "101");
cb.getSelectionModel ( ).selectedIndexProperty ( )
.addListener ( new ChangeListener < Number > ( ) {
@SuppressWarnings ( "rawtypes" )
public void changed ( ObservableValue ov, Number value,
Number new_value ) {
System.out.println ( new_value.intValue ( ) );
int i, j = 0;
StringBuilder str = new StringBuilder ( );
ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 14 ) );
for ( i = 0; i < TAM; i++ ) {
str.append ( "\n" );
for ( j = 0; j < TAM; j++ ) {
str.append ( "\t" );
if ( Vet [ i ] [ j ] == new_value.intValue ( ) ) {
if ( Vet [ i ] [ j ] >= 0 && Vet [ i ] [ j ] <= 9 )
str.append ( "0" );
str.append ( Vet [ i ] [ j ] );
}
}
}
ctx.setFill ( Color.BLACK );
ctx.fillText ( " " + str, 138, 100 );
}
} );
grid.add ( cb, 1, 1 );
}
//
/////////////////////////////////////////////////////////////////////////
public void start ( Stage stage ) {
stage.setTitle ( "JAVAFX -
COMBOX EXEMPLO" );
// Criando moldura e dando efeitos
root.setStyle ( "-fx-padding:
5;" + "-fx-border-style: solid inside;"
+ "-fx-border-width:
10;" + "-fx-border-insets: 5;"
+ "-fx-border-radius:
5;" + "-fx-border-color: green;" );
ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
ctx.setFill ( Color.RED );
ctx.fillText ( "JAVAFX -
COMBOX EXEMPLO", 200, 50 );
ctx.setFill ( Color.BLUE );
ctx.fillText ( "Array sendo
preenchido pelo combox", 190, 80 );
int a = 0, b = 0, i, j = 0, n = 100;
int [ ] [ ] Vet = new int [ TAM ] [ TAM ];
int [ ] V = new int [ n ];
for ( i = 1; i <= n; i++ ) {
V [ a ] = i;
a++;
}
ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 14 ) );
for ( i = 0; i < TAM; i++ ) {
for ( j = 0; j < TAM; j++ ) {
Vet [ i ] [ j ] = V [ b ];
b++;
}
}
Combo_box ( Vet );
Sair ( );
grid.setPadding ( new Insets ( 15, 15, 60, 15 ) );
root.getChildren ( ).addAll ( canvas, btn_1 );
root.getChildren ( ).addAll ( grid );
stage.setScene ( scene );
stage.show ( );
}
//
/////////////////////////////////////////////////////////////////////////
public static void main ( String [ ] args ) {
launch ( args );
}
}
//COMBOX COM INICIALIZAÇÃO
//ATRAVÉS DE UM CONTADOR
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
public class PROJETO extends Application {
public static int TAM = 10;
static Canvas canvas = new Canvas ( 600, 340 );
static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
static Pane root = new Pane ( );
static Scene scene = new Scene ( root,
600, 340 );
static GridPane grid = new GridPane ( );
static Button btn_1 = new Button ( "FECHAR" );
@SuppressWarnings ( "rawtypes" )
static final ComboBox cb = new ComboBox ( );
//
/////////////////////////////////////////////////////////////////////////
public static Node Sair ( ) {
btn_1.setPrefWidth ( 100 );// Largura do botão
btn_1.setLayoutX ( 267 );// Posição do botão
coluna
btn_1.setPrefHeight ( 20 );// altura do botão
btn_1.setLayoutY ( 300 );// //Posição do botão
linha
btn_1.setStyle ( "-fx-padding:
1;" + "-fx-border-style: solid
inside;"
+ "-fx-border-width: 2;" + "-fx-border-insets:
1;"
+ "-fx-border-radius: 1;" + "-fx-border-color:
red;"
+ "-fx-font-style: italic;" + "-fx-font-family:
Consolas;" );
btn_1.setOnKeyPressed ( e -> {
if ( e.getCode ( ) == KeyCode.ENTER ) {
System.exit ( 0 );
}
} );
return btn_1;
}
//
/////////////////////////////////////////////////////////////////////////
@SuppressWarnings ( "unchecked" )
public static void Combo_box ( int Vet [ ] [ ] ) {
cb.setStyle ( "-fx-padding:
5;" + "-fx-border-style: solid
inside;"
+ "-fx-border-width: 2;" + "-fx-border-insets:
5;"
+ "-fx-border-radius: 5;" + "-fx-border-color:
black;" );
int i = 0;
do {
i++;
//total de linhas visíveis
cb.setVisibleRowCount ( 10 );
cb.getItems ( ).addAll ( i );
} while ( i <= 100 );
cb.getSelectionModel ( ).selectedIndexProperty ( )
.addListener ( new ChangeListener < Number > ( ) {
@SuppressWarnings ( "rawtypes" )
public void changed ( ObservableValue ov, Number value,
Number new_value ) {
System.out.println ( new_value.intValue ( ) );
ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 14 ) );
int i = 0, j = 0;
StringBuilder str = new StringBuilder ( );
for ( i = 0; i < TAM; i++ ) {
str.append ( "\n" );
for ( j = 0; j < TAM; j++ ) {
str.append ( "\t" );
if ( Vet [ i ] [ j ] == new_value.intValue ( ) ) {
if ( Vet [ i ] [ j ] >= 0 && Vet [ i ] [ j ] <= 9 )
str.append ( "0" );
str.append ( Vet [ i ] [ j ] );
}
}
}
ctx.setFill ( Color.BLACK );
ctx.fillText ( " " + str, 138, 100 );
}
} );
grid.add ( cb, 1, 1 );
}
//
/////////////////////////////////////////////////////////////////////////
public void start ( Stage stage ) {
stage.setTitle ( "JAVAFX -
COMBOX EXEMPLO" );
// Criando moldura e
dando efeitos
root.setStyle ( "-fx-padding:
5;" + "-fx-border-style: solid inside;"
+ "-fx-border-width: 10;" + "-fx-border-insets:
5;"
+ "-fx-border-radius: 5;" + "-fx-border-color:
green;" );
ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
ctx.setFill ( Color.RED );
ctx.fillText ( "JAVAFX -
COMBOX EXEMPLO", 200, 50 );
ctx.setFill ( Color.BLUE );
ctx.fillText ( "Array sendo
preenchido pelo combox", 190, 80 );
int a = 0, b = 0, i, j = 0, n = 100;
int [ ] [ ] Vet = new int [ TAM ] [ TAM ];
int [ ] V = new int [ n ];
for ( i = 1; i <= n; i++ ) {
V [ a ] = i;
a++;
}
ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 14 ) );
for ( i = 0; i < TAM; i++ ) {
for ( j = 0; j < TAM; j++ ) {
Vet [ i ] [ j ] = V [ b ];
b++;
}
}
Combo_box ( Vet );
Sair ( );
grid.setPadding ( new Insets ( 15, 15, 60, 15 ) );
root.getChildren ( ).addAll ( canvas, btn_1 );
root.getChildren ( ).addAll ( grid );
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.