2013-04-23 68 views
0

我有显示图像该JavaFX的手风琴:如何显示行到手风琴

公共类导航{

private static final Image BLUE_FISH = new Image("/Blue-Fish-icon.png"); 
private static final Image RED_FISH = new Image("/Red-Fish-icon.png"); 
private static final Image YELLOW_FISH = new Image("/Yellow-Fish-icon.png"); 
private static final Image GREEN_FISH = new Image("/Green-Fish-icon.png"); 

public void initNavigation(Stage primaryStage, Group root, Scene scene) { 

    VBox stackedTitledPanes = createStackedTitledPanes(); 

    ScrollPane scroll = makeScrollable(stackedTitledPanes); 
    scroll.getStyleClass().add("stacked-titled-panes-scroll-pane"); 
    scroll.setPrefSize(395, 580); 
    scroll.setLayoutX(5); 
    scroll.setLayoutY(32); 

    //scene = new Scene(scroll); 
    root.getChildren().add(scroll); 

} 

private VBox createStackedTitledPanes() { 
    final VBox stackedTitledPanes = new VBox(); 
    stackedTitledPanes.getChildren().setAll(
      createTitledPane("Connections", GREEN_FISH), 
      createTitledPane("Tables", YELLOW_FISH), 
      createTitledPane("Description", RED_FISH), 
      createTitledPane("Blue Fish", BLUE_FISH)); 
    ((TitledPane) stackedTitledPanes.getChildren().get(0)).setExpanded(true); 
    stackedTitledPanes.getStyleClass().add("stacked-titled-panes"); 

    return stackedTitledPanes; 
} 

public TitledPane createTitledPane(String title, Image... images) { 
    FlowPane content = new FlowPane(); 
    for (Image image : images) { 
     ImageView imageView = new ImageView(image); 
     content.getChildren().add(imageView); 

     FlowPane.setMargin(imageView, new Insets(10)); 
    } 
    content.setAlignment(Pos.TOP_CENTER); 

    TitledPane pane = new TitledPane(title, content); 
    pane.getStyleClass().add("stacked-titled-pane"); 
    pane.setExpanded(false); 

    return pane; 
} 

private ScrollPane makeScrollable(final VBox node) { 
    final ScrollPane scroll = new ScrollPane(); 
    scroll.setContent(node); 
    scroll.viewportBoundsProperty().addListener(new ChangeListener<Bounds>() { 
     @Override 
     public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds) { 
      node.setPrefWidth(bounds.getWidth()); 
     } 
    }); 
    return scroll; 
} 

}

我感兴趣的是它可以显示的数据行放置图像的位置。事情是这样的:

enter image description here

P.S案例。我有一个将被用作列表中的Java对象:

public List<dataObj> list = new ArrayList<>(); 

    public class dataObj { 

     private int connectionId; 
     private String conenctionname; 
     private String connectionDescription; 

     public dataObj() { 
     } 

     .................... 
    } 

当我插入一些数据到Java数组列表我想把它显示到基于上述要求的手风琴。

P.S 2在我的情况下,将文本插入FlowPane的正确方法是什么?我测试了这个:

public TitledPane createTitledPane(String title, Image... images) { 
     FlowPane content = new FlowPane(); 
     for (Image image : images) { 
      ImageView imageView = new ImageView(image); 
      content.getChildren().add(imageView); 

      FlowPane.setMargin(imageView, new Insets(10)); 
     } 
     content.setAlignment(Pos.TOP_CENTER); 
     content.setText("This part will be the first line.\n This part the second."); 

     TitledPane pane = new TitledPane(title, content); 
     pane.getStyleClass().add("stacked-titled-pane"); 
     pane.setExpanded(false); 

     return pane; 
    } 

我得到错误,使用setText插入文本是不正确的。什么是正确的方法?

+0

为什么不使用一个ListView? – Kalaschni 2013-04-23 13:53:17

+0

@Kalaschni请举例吗?我是JavaFX的新手。 – 2013-04-23 13:54:49

回答

1

如果使用"\n",输出字符串将被分隔成多行文本。 例如:

component.setText("This part will be the first line.\n This part the second."); 

从您的更新,假设你有getterssetters

component.setText(String.valueOf(dataObj.getConnectionId()) + "\n" + dataObj.getConnectionname() + "\n" + dataObj.getConnectionDescription()); 
+0

是的,但在我的情况下,我想显示数据的对象。 – 2013-04-23 13:25:13

+0

我不认为我明白你的意思,那有什么不同? – 2013-04-23 13:30:56

+0

我更新了帖子。 – 2013-04-23 13:38:01

1

您只需使用一个ListView:

private void hello() { 
    ListView<Object> lv = new ListView<>(); 
    // yourList is you List<Object> list 
    lv.itemsProperty().set(yourList); 
    lv.setCellFactory(new Callback<ListView<Object>, ListCell<Object>>() { 
     @Override 
     public ListCell<Object> call(ListView<Object> p) { 
      return new youCellFactory(); 
     } 
    }); 
    AnchorPane content = new AnchorPane(); 
    content.getChildren().add(lv); 
    // add to TitelPane 
    TitledPane pane = new TitledPane(title, content); 
} 
static class youCellFactory extends ListCell<Object> { 
    @Override 
    public void updateItem(Object item, boolean empty) { 
     super.updateItem(item, empty); 
     if (item != null) { 
      setText(item.getConenctionname()); 
     } 
    } 
} 

我没有测试代码,但它应该工作。

这里是一个很好的例子太多,但没有对象:
ListViewSample.java

+0

在我的情况下,我使用'TitledPane'作为对象返回。如何将我的代码与您的示例结合起来? – 2013-04-23 14:22:44

+0

我更新了我的代码。 – Kalaschni 2013-04-23 14:59:39

+0

我可以问你,我可以如何合适的对象'列表',并插入一些可以看到手风琴的数据。 – 2013-04-24 10:42:10