-
-
Save alvareztech/56a2fdc4ff3b0421471bdcd6004be906 to your computer and use it in GitHub Desktop.
JavaFX & TreeSet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Contacto { | |
private String nombre; | |
private int telefono; | |
public Contacto() { | |
this.nombre = ""; | |
this.telefono = 0; | |
} | |
public Contacto(String nombre, int telefono) { | |
this.nombre = nombre; | |
this.telefono = telefono; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public int getTelefono() { | |
return telefono; | |
} | |
public void setTelefono(int telefono) { | |
this.telefono = telefono; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
import java.util.Comparator; | |
import java.util.Iterator; | |
import java.util.TreeSet; | |
public class Main extends Application { | |
// Variables globales, así se pueden acceder desde cualquier método en la clase | |
TreeSet<Contacto> contactos = new TreeSet<Contacto>(new Comparator<Contacto>() { | |
@Override | |
public int compare(Contacto o1, Contacto o2) { | |
String nombre1 = o1.getNombre().toLowerCase(); | |
String nombre2 = o2.getNombre().toLowerCase(); | |
return nombre1.compareTo(nombre2); | |
} | |
}); | |
TextArea textArea = new TextArea(); | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
TextField nombreTextField = new TextField(); | |
nombreTextField.setPromptText("Nombre"); | |
TextField telefonoTextField = new TextField(); | |
telefonoTextField.setPromptText("Teléfono"); | |
Button adicionarButton = new Button("Adicionar"); | |
adicionarButton.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
// Recogemos la información | |
String nombre = nombreTextField.getText(); | |
int telefono = Integer.parseInt(telefonoTextField.getText()); | |
// Creamos el contacto con los datos recogidos | |
Contacto c = new Contacto(nombre, telefono); | |
// Adicionamos al árbol | |
contactos.add(c); | |
// Método que se encarga de mostrar en el TextArea | |
mostrar(); | |
// limpiamos | |
nombreTextField.setText(null); | |
telefonoTextField.setText(null); | |
} | |
}); | |
VBox vBox = new VBox(); | |
vBox.getChildren().add(nombreTextField); | |
vBox.getChildren().add(telefonoTextField); | |
vBox.getChildren().add(adicionarButton); | |
vBox.getChildren().add(textArea); | |
vBox.setSpacing(20); | |
vBox.setPadding(new Insets(20)); | |
Scene scene = new Scene(vBox, 300, 300); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
private void mostrar() { | |
String x = ""; | |
// Una forma de recorrer el árbol | |
Iterator<Contacto> iterador = contactos.iterator(); | |
while (iterador.hasNext()) { | |
Contacto c = iterador.next(); | |
x += c.getNombre() + "\t" + c.getTelefono() + "\n"; // Así armamos línea por línea lo que se mostrará | |
} | |
textArea.setText(x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment