create project.

This commit is contained in:
2026-02-15 09:58:46 -05:00
commit 8c44225f3c
36 changed files with 864 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.bookocontacts;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class Person {
String id;
String name;
int age;
public Person() {
}
public Person(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" + "id=" + id + ", name=" + name + ", age=" + age + '}';
}
}
+38
View File
@@ -0,0 +1,38 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.bookocontacts;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class PersonFactory {
static int id=0;
static List getAllPersons() {
ArrayList<Person> list = new ArrayList<>();
list.add(createPerson("John", 40));
list.add(createPerson("Daisy", 21));
list.add(createPerson("Martha", 34));
list.add(createPerson("Frodo", 16));
list.add(createPerson("Mickey", 22));
list.add(createPerson("Minnie", 19));
list.add(createPerson("Donald", 30));
list.add(createPerson("Goofy", 32));
list.add(createPerson("Pokoyo", 12));
list.add(createPerson("Dora", 16));
list.add(createPerson("Tintin", 25));
list.add(createPerson("John", 52));
list.add(createPerson("Daisy", 29));
list.add(createPerson("Martha Has a very Long Name", 18));
System.out.println("Returning person size: " + list.size());
return list;
}
static Person createPerson(String _name, int _age) {
return new Person(Integer.toString(id++),_name, _age);
}
}
+170
View File
@@ -0,0 +1,170 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.bookocontacts;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.SortType;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
/**
* FXML Controller class
*
*/
public class TableViewController implements Initializable {
@FXML
private TableView<Person> personView = new TableView<>();
@FXML
private TableColumn<Person, String> personViewIdCol;
@FXML
private TableColumn<Person, String> personViewNameCol;
@FXML
private TableColumn<Person, Integer> personViewAgeCol;
@FXML
Pagination pagination;
@FXML
private Button save;
@FXML
private TextField idTxt;
@FXML
private TextField nameTxt;
@FXML
private TextField ageTxt;
private Button id = new Button("ID");
private ImageView upImg = new ImageView(new Image("/media/up.png"));
private ImageView downImg = new ImageView(new Image("/media/down.png"));
List<Person> rawPersons = PersonFactory.getAllPersons();
ObservableList<Person> persons = FXCollections.observableArrayList(rawPersons);
int pageCount = 5;
int itemsPerPage = 4;
int currentPageIndex = 0;
boolean order = true;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("initialize");
sort();
initializeTable();
pageCount = getPageCount(persons.size(), itemsPerPage);
System.out.println("pageCount=" + pageCount);
pagination.setPageCount(pageCount);
pagination.currentPageIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("Pagination Changed from " + oldValue + " , to " + newValue);
currentPageIndex = newValue.intValue();
updatePersonView();
}
});
save.setOnAction(new javafx.event.EventHandler<javafx.event.ActionEvent>() {
@Override
public void handle(javafx.event.ActionEvent t) {
persons.add(new Person(idTxt.getText(), nameTxt.getText(), Integer.valueOf(ageTxt.getText())));
sort();
updatePersonView();
idTxt.setText(null);
nameTxt.setText(null);
ageTxt.setText(null);
}
});
id.setOnAction(new javafx.event.EventHandler<javafx.event.ActionEvent>() {
@Override
public void handle(javafx.event.ActionEvent t) {
sort();
if (order) {
Collections.reverse(persons);
}
System.out.println(" order = " + order + "; data = " + persons);
order = !order;
id.setGraphic((order) ? upImg : downImg);
updatePersonView();
System.out.println(" comparator called");
}
});
}
private void sort() {
//persons.sort((Person p1, Person p2) -> p1.id.compareTo(p2.id));
Collections.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person t, Person t1) {
System.out.println(" comparator called");
return t.getId().compareTo(t1.getId());
}
});
}
public void updatePersonView() {
System.out.println("updatePersonView");
personView.getItems().setAll(persons.subList(currentPageIndex * itemsPerPage, ((currentPageIndex * itemsPerPage + itemsPerPage <= persons.size()) ? currentPageIndex * itemsPerPage + itemsPerPage : persons.size())));
}
private void initializeTable() {
personViewIdCol.setCellValueFactory(new PropertyValueFactory<Person, String>("Id"));
id.setGraphic(upImg);
personViewIdCol.setGraphic(id);
personViewIdCol.setSortable(false);
personViewNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("Name"));
personViewNameCol.sortTypeProperty().addListener(new ChangeListener<SortType>() {
@Override
public void changed(ObservableValue<? extends SortType> paramObservableValue, SortType paramT1, SortType paramT2) {
System.out.println("NAME Clicked -- sortType = " + paramT1 + ", SortType=" + paramT2);
id.setGraphic(null);
}
});
personViewAgeCol.setCellValueFactory(new PropertyValueFactory<Person, Integer>("Age"));
personViewAgeCol.setText("AGE");
personViewAgeCol.setSortable(false);
personView.getItems().setAll(persons.subList(0, itemsPerPage));
}
public int getItemsPerPage() {
return itemsPerPage;
}
public void setItemsPerPage(int itemsPerPage) {
this.itemsPerPage = itemsPerPage;
}
public int getCurrentPageIndex() {
return currentPageIndex;
}
public void setCurrentPageIndex(int currentPageIndex) {
this.currentPageIndex = currentPageIndex;
}
private int getPageCount(int totalCount, int itemsPerPage) {
float floatCount = Float.valueOf(totalCount) / Float.valueOf(itemsPerPage);
int intCount = totalCount / itemsPerPage;
System.out.println("floatCount=" + floatCount + ", intCount=" + intCount);
return ((floatCount > intCount) ? ++intCount : intCount);
}
}
+60
View File
@@ -0,0 +1,60 @@
package edu.bookocontacts;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
/**
*
*/
public class View extends Application {
private Stage stage;
@Override
public void start(Stage primaryStage) {
stage = primaryStage;
stage.setTitle("Contacts View");
Parent root = createView();
Scene scene = new Scene(root, 600, 400);
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
private Parent createView() {
AnchorPane pane = null;
try {
String fxml = "/media/TableView.fxml";
FXMLLoader loader = new FXMLLoader();
InputStream in = getClass().getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(getClass().getResource(fxml));
try {
pane = (AnchorPane) loader.load(in);
} finally {
if (null != in) {
in.close();
}
}
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
return pane;
}
public static void main(String[] args) {
launch(args);
}
}
+32
View File
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Pagination?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="edu.bookocontacts.TableViewController">
<children>
<TableView fx:id="personView" layoutX="1.0" layoutY="76.0" prefHeight="210.0" prefWidth="478.0">
<columns>
<TableColumn fx:id="personViewIdCol" maxWidth="5000.0" minWidth="10.0" prefWidth="116.0" resizable="true" />
<TableColumn fx:id="personViewNameCol" maxWidth="5000.0" minWidth="10.0" prefWidth="124.0" resizable="true" text="Name" />
<TableColumn fx:id="personViewAgeCol" maxWidth="5000.0" minWidth="10.0" prefWidth="93.0" resizable="true" text="Age" />
</columns>
</TableView>
<HBox layoutX="85.0" layoutY="283.0" prefHeight="64.0" prefWidth="377.0">
<children>
<Pagination fx:id="pagination" prefHeight="65.0" prefWidth="334.0" />
</children></HBox>
<Button fx:id="save" layoutX="181.0" layoutY="21.0" mnemonicParsing="false" text="Add" textFill="#0052cc" textOverrun="CLIP" />
<Text fill="#1400ff" layoutX="38.0" layoutY="39.0" scaleX="1.393175914330746" scaleY="1.5976504915765308" strokeType="OUTSIDE" strokeWidth="0.0" text="Contacts" wrappingWidth="119.82850646972656">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Text>
</children>
</AnchorPane>
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 236 B

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 725 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 225 B