JavaFX列表与SceneBuilder中的库类似

时间:2017-04-12 12:14:04

标签: java user-interface javafx

我试图在SceneBuilder中重新创建Library-List的外观,但我不知道,我需要采取哪个元素。

如何重新创建此列表?

Picture of the Library-List

1 个答案:

答案 0 :(得分:0)

这是使用ControlsFx Awesome Fonts的粗略草稿。

  

主:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication73 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.Accordion?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication73.FXMLDocumentController">
    <children>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <Accordion prefHeight="200.0" prefWidth="320.0">
        <panes>
          <TitledPane animated="false" text="untitled 1">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <ListView fx:id="lvOne" layoutX="-19.0" layoutY="-50.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>
            </content>
          </TitledPane>
          <TitledPane animated="false" text="untitled 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </TitledPane>
          <TitledPane animated="false" text="untitled 3">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </TitledPane>
        </panes>
      </Accordion>
    </children>
</AnchorPane>
  

控制器:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
import org.controlsfx.glyphfont.FontAwesome;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @FXML ListView lvOne;

    ObservableList<CustomItem> listViewData = FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
        lvOne.setItems(listViewData);
        lvOne.setCellFactory(new Callback<ListView<CustomItem>, ListCell<CustomItem>>() {
            @Override
            public ListCell<CustomItem> call(ListView<CustomItem> listView)
            {
                return new ListViewCell();
            }
        });

        CustomItem ci = new CustomItem();        
        ci.setLabelGlyph(FontAwesome.Glyph.FLASH);
        ci.setString("entry one");
        listViewData.add(ci);

        CustomItem ci2 = new CustomItem();        
        ci2.setLabelGlyph(FontAwesome.Glyph.AMBULANCE);
        ci2.setString("entry two");
        listViewData.add(ci2);
    }    

}
  

ListView单元格:

import javafx.scene.control.Label;
import javafx.scene.control.ListCell;

/**
 *
 * @author blj0011
 */
public class ListViewCell extends ListCell<CustomItem>
{
    @Override
    public void updateItem(CustomItem item, boolean empty)
    {         
       super.updateItem(item, empty);

        if (empty || item == null) 
        {
            setGraphic(null);
            setText(null);
        } 
        else 
        {
            Label label = item.getLabel();
            setGraphic(label);
        }       
    }
}
  

CustomItem的:

import javafx.scene.control.Label;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.FontAwesome.Glyph;

/**
 *
 * @author blj0011
 */
public class CustomItem
{
    private final Label label = new Label();

    public void setLabelGlyph(Glyph glyph)
    {
        FontAwesome fa = new FontAwesome();
        label.setGraphic(fa.create(glyph));
    }

    public void setString(String string)
    {
        label.setText(string);
    }

    public Label getLabel()
    {
        return label;
    }
}

enter image description here