Good day! I can not break the cells in the ListView, that would display the picture, the button and the text on one line. I tried, but I could not decide the whole evening I can not decide. Thank you
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import sample.objects.CollectionContactForList; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("views/sample.fxml")); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root, 500, 300)); primaryStage.show(); } private void testData(){ CollectionContactForList contactForList= new CollectionContactForList(); contactForList.fieldData(); } public static void main(String[] args) { launch(args); } } Class Controller for processing
import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.util.Callback; import sample.objects.CollectionContactForList; import sample.objects.Contact; public class Controller { //Коллекции CollectionContactForList contactForList = new CollectionContactForList(); @FXML HBox hbox; @FXML ListView<Contact> listContact; @FXML Label name; public void setListView(){ listContact.setCellFactory(new Callback<ListView<Contact>, ListCell<Contact>>() { @Override public ListCell<Contact> call(ListView<Contact> param) { //тут нужно собрать объект ListCell, и вернуть его ListCell<Contact> listCell = new ListCell<Contact>(){ //обрисовка происходит здесь @Override public void updateItem(Contact item , boolean empty) { super.updateItem(item, empty); if (empty || item == null) { } else { HBox hBox = new HBox(); name.setText(""); ImageView.class.getResourceAsStream("/resurse/logo.png"); // Тут нужно загрузить xml FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("boxInContact.fxml")); //устанавливаем графику // setGraphic(); } } }; return listCell; } }); } @FXML private void initialize() { /* listContact.setCellFactory(new Callback<ListView<Contact>, ListCell<Contact>>() { @Override public ListCell<Contact> call(ListView<Contact> param) { //тут нужно собрать объект ListCell, и вернуть его ListCell<Contact> listCell = new ListCell<Contact>(){ //отрисовка происходит здеся @Override public void updateItem(Contact item , boolean empty) { super.updateItem(item, empty); if (empty || item == null) { } else { HBox hBox = new HBox(); name.setText(""); ImageView.class.getResourceAsStream("/resurse/logo.png"); // Тут нужно загрузить xml FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("boxInContact.fxml")); //устанавливаем графику // setGraphic(); } } }; return listCell; } }); */ contactForList.fieldData(); listContact.setItems((ObservableList<Contact>) contactForList.getContactList()); //setListView(); } }//Закрытие класса Contact class, that's what you need to display.
public class Contact { ImageView imageView; private String avatar = "/logo.png"; private String name; Button button; public Contact(String avatar, String name, Button button) { this.avatar = avatar; this.name = name; this.button = button; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Button getButton() { return button; } public void setButton(Button button) { this.button = button; } } Class containing the Contacts collection
public class CollectionContactForList implements ForContact { public ObservableList<Contact> contactList= FXCollections.observableArrayList(); //Set<Contact>setContactList = new HashSet<>(); public ListView<Contact> listView = new ListView<Contact> ((ObservableList<Contact>) contactList); // Методы интерфейса @Override public void add(Contact contact) { contactList.add(contact); }// Закрытие метода добавления @Override public void update(Contact contact) { }// метод ,пока не используется , вскорем будущем применим его 15 04 @Override public void delete(Contact contact) { contactList.remove(contact); }// Закрытие метода удаления public List<Contact> getContactList() { return contactList; } public void fieldData(){ contactList.add( new Contact("","ada",new Button("st"))); } }// Закрытие класса GUI itself
?import javafx.scene.layout.GridPane?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-I nfinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controllers.Controller"> <children> <BorderPane layoutX="164.0" layoutY="79.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <left> <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER"> <tabs> <Tab fx:id="tabContact" text="Untitled Tab 1"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <ListView fx:id="listContact" layoutX="-31.0" layoutY="43.0" prefHeight="371.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> </children> </AnchorPane> </content> </Tab> <Tab text="Untitled Tab 2"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <ListView layoutX="14.0" layoutY="75.0" p refHeight="371.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> </children> </AnchorPane> </content> </Tab> </tabs> </TabPane> </left> <top> <ListView fx:id="listUser" prefHeight="58.0" prefWidth="600.0" BorderPane.alignment="CENTER" /> </top> </BorderPane> </children> </AnchorPane> And this fxml file is needed to load it into the ListView.
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.shape.*?> <?import javafx.scene.control.*?> <?import javafx.scene.image.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <HBox fx:id="hbox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="99.0" prefWidth="139.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controllers.Controller"> <children> <ImageView fitHeight="98.0" fitWidth="67.0" pickOnBounds="true" preserveRatio="true" fx:id="image"/> <Label prefHeight="98.0" prefWidth="45.0" text="Label" fx:id="name"/> <Button fx:id="btnStatus" mnemonicParsing="false" text="Button" /> </children> </HBox>