Вот (грубое) демонстрационное приложение, использующее FXML
.Ключ создает пользовательский узел для представления ListViewCell
.
Пользовательский узел для ListViewCell
.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<fx:root fx:id="hboxRoot" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" type="HBox" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" HBox.hgrow="ALWAYS">
<children>
<VBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label fx:id="lblName" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Label" VBox.vgrow="ALWAYS" />
<Label fx:id="lblAge" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Label" VBox.vgrow="ALWAYS" />
<Label fx:id="lblSex" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Label" VBox.vgrow="ALWAYS" />
</children>
<padding>
<Insets left="20.0" />
</padding>
</VBox>
</children>
</AnchorPane>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
<children>
<Button fx:id="btnAdd" mnemonicParsing="false" text="Button" />
<Button fx:id="btnRemove" mnemonicParsing="false" text="Button" />
</children>
</HBox>
</children>
</fx:root>
Пользовательский узел Controller
. При работе с пользовательскими узлами я не уверен, называется ли это Controller
.Он работает подобно `Контроллеру.
import java.io.IOException;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
/**
* FXML Controller class
*
* @author blj0011
*/
public class CustomCellView extends HBox
{
@FXML
HBox hboxRoot;
@FXML
Label lblName, lblAge, lblSex;
@FXML
Button btnAdd, btnRemove;
public CustomCellView()
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CellView.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
HBox getHBoxRoot()
{
return hboxRoot;
}
void setlblNameText(String text)
{
lblName.setText(text);
}
void setlblAgeText(String text)
{
lblAge.setText(text);
}
void setlblSexText(String text)
{
lblSex.setText(text);
}
void setBtnAddAction(EventHandler actionEvent)
{
btnAdd.setOnAction(actionEvent);
}
void setBtnRemoveAction(EventHandler actionEvent)
{
btnRemove.setOnAction(actionEvent);
}
}
ListViewCell
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.ListCell;
public class ListViewCell extends ListCell<Person>
{
@Override
public void updateItem(Person person, boolean empty)
{
super.updateItem(person, empty);
if (person != null) {
CustomCellView customCellView = new CustomCellView();
customCellView.setlblNameText(person.getName());
customCellView.setlblAgeText(person.getAge());
customCellView.setlblSexText(person.getSex());
EventHandler addHandler = (EventHandler) (Event event) -> {
System.out.println("Add " + person.getName());
};
customCellView.setBtnAddAction(addHandler);
EventHandler removeHandler = (EventHandler) (Event event) -> {
System.out.println("Remove " + person.getName());
};
customCellView.setBtnRemoveAction(removeHandler);
setGraphic(customCellView.getHBoxRoot());
}
}
}
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication311 extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Основной FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="573.0" prefWidth="726.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="javafxapplication311.FXMLDocumentController">
<children>
<ListView fx:id="lvMain" layoutX="102.0" layoutY="14.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
Главный FXMLController
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
/**
*
* @author blj0011
*/
public class FXMLDocumentController implements Initializable
{
@FXML
private ListView lvMain;
@Override
public void initialize(URL url, ResourceBundle rb)
{
lvMain.getItems().add(new Person("John Doe", "22", "Male"));
lvMain.getItems().add(new Person("Jane Doe", "21", "Female"));
lvMain.setCellFactory(new Callback<ListView<String>, ListCell<String>>()
{
@Override
public ListCell call(ListView param)
{
return new ListViewCell();
}
});
}
}
Человек
/**
*
* @author blj0011
*/
public class Person
{
private String name;
private String age;
private String sex;
public Person(String name, String age, String sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
}