2014-07-24 2 views
1

tableview에 이미지를 추가하려고하는데 이미지를 추가 할 수 없습니다.javafx가 테이블 뷰에 이미지 추가

바이트로 이미지를 가져 오는 중입니다. 이미지 뷰에서이 이미지를 설정할 수 있지만 테이블 뷰에 추가 할 수있는 방법이 있습니다.

사람 클래스 :

public class person3 { 
    private final StringProperty firstName7 = new SimpleStringProperty(""); 
    private final StringProperty firstName8 = new SimpleStringProperty(""); 


    public person3(String firstName4,String firstName5) { 
     setFirstName7(firstName4); 
     setFirstName8(firstName5); 


     } 

    public String getFirstName7() { 
     return firstName7.get(); 
    } 

    public void setFirstName7(String name) { 
     this.firstName7.set(name); 
    } 

    public StringProperty firstNameProperty7() { 
     return firstName7; 
    } 
    public String getFirstName8() { 
     return firstName8.get(); 
    } 

    public void setFirstName8(String name) { 
     this.firstName8.set(name); 
    } 

    public StringProperty firstNameProperty8() { 
     return firstName8; 
    } 



} 

는 지금은
  f51=rs.getBytes(10); 
      System.out.println(f51); 
      ByteArrayInputStream bis = new ByteArrayInputStream(f51); 
      System.out.println(bis); 
      BufferedImage read = ImageIO.read(bis); 
      System.out.println(read); 
      Image image = SwingFXUtils.toFXImage(read, null); 
      table1.getItems().add(new person3("Data1","data2")); 
      // this add simple data but how can i add image 

도와의 tableview

에 이미지를 추가하려합니다.

감사합니다.

답변

9

이 문제에 대한 가능한 접근법 중 하나는 개인 클래스 ImageView과 setter 및 getter가있는 CustomImage이라는 간단한 클래스를 만드는 것입니다. 그런 다음이 클래스를 사용하여 TableView<T>TableColumn<T> 제네릭 유형을 지정하고, 열의 셀 값 팩토리를 설정하고, 이미지에 표를 채울 수 있습니다. 예를 CustomImage 클래스와 그 실용화의 구현은 다음과 같습니다

import javafx.scene.image.ImageView; 

public class CustomImage { 

    private ImageView image; 

    CustomImage(ImageView img) { 
     this.image = img; 
    } 

    public void setImage(ImageView value) { 
     image = value; 
    } 

    public ImageView getImage() { 
     return image; 
    } 
} 

실제 구현 :

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class ImageViewInTableView extends Application { 

    public Parent createContent() { 

     /* layout */ 
     BorderPane layout = new BorderPane(); 

     /* layout -> center */ 
     TableView<CustomImage> tableview = new TableView<CustomImage>(); 

     /* layout -> center -> tableview */ 

     /* initialize two CustomImage objects and add them to the observable list */ 
     ObservableList<CustomImage> imgList = FXCollections.observableArrayList(); 
     CustomImage item_1 = new CustomImage(new ImageView(new Image("Icon_AddNewPatient.png"))); 
     CustomImage item_2 = new CustomImage(new ImageView(new Image("Icon_EditPatient.png"))); 
     imgList.addAll(item_1, item_2); 

     /* initialize and specify table column */ 
     TableColumn<CustomImage, ImageView> firstColumn = new TableColumn<CustomImage, ImageView>("Images"); 
     firstColumn.setCellValueFactory(new PropertyValueFactory<CustomImage, ImageView>("image")); 
     firstColumn.setPrefWidth(60); 

     /* add column to the tableview and set its items */ 
     tableview.getColumns().add(firstColumn); 
     tableview.setItems(imgList); 

     /* add TableView to the layout */ 
     layout.setCenter(tableview); 
     return layout; 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     stage.setScene(new Scene(createContent())); 
     stage.setWidth(200); 
     stage.setHeight(200); 
     stage.show(); 
    } 

    public static void main(String args[]) { 
     launch(args); 
    } 
} 

그리고 그것은 어떻게 보이는지 그게 전부 : 나는 당신의 코드를 사용

enter image description here

+0

저는 FXML에 대해 설명하고 있습니다. – user3829658

+0

가장 좋은 설명 나는 건너왔다. 감사. – Sedrick

0

Netbeans의 최신 버전이 나와 있습니다. 또한 String 열을 추가했습니다.

import javafx.scene.image.ImageView; 

public class CustomImage 
{ 
    private ImageView image; 
    private String string; 

    CustomImage(ImageView img, String string) 
    { 
     this.image = img; 
     this.string = string; 
    } 

    public void setImage(ImageView value) 
    { 
     image = value; 
    } 

    public ImageView getImage() 
    { 
     return image; 
    } 

    public void setSring(String string) 
    { 
     this.string = string; 
    } 

    public String getString() 
    { 
     return this.string; 
    } 
} 

JavaFx 컨트롤러에서 구현.

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.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 

public class FXMLDocumentController implements Initializable 
{ 
    //Going to need access to the table view. I used JavaFx Scene Builder to create the tableview. 
    //You can use the .fxml document also. 
    @FXML private TableView tvMain;  

    @Override 
    public void initialize(URL url, ResourceBundle rb) 
    {   
     /* initialize two CustomImage objects and add them to the observable list */ 
     ObservableList<CustomImage> imgList = FXCollections.observableArrayList(); 
     CustomImage item_1 = new CustomImage(new ImageView(new Image("aulogo02.jpg")), "hello"); 
     CustomImage item_2 = new CustomImage(new ImageView(new Image("aulogo02.jpg")), "world"); 
     imgList.addAll(item_1, item_2); 

     /* initialize and specify table column */ 
     TableColumn tcC1 = new TableColumn<>("Picture"); 
     tcC1.setCellValueFactory(new PropertyValueFactory<>("image")); 
     tcC1.setPrefWidth(130);//set this to what you prefer. 

     TableColumn tcX = new TableColumn<>("Text"); 
     tcX.setCellValueFactory(new PropertyValueFactory<>("string")); 

     /* add column to the tableview and set its items */ 
     tvMain.getColumns().add(tcC1); 
     tvMain.getColumns().add(tcX); 
     tvMain.setItems(imgList);  
    }  

} 

FXML 파일로 구현.

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.TableView?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane id="AnchorPane" prefHeight="461.0" prefWidth="682.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="imageviewintableview.FXMLDocumentController"> 
    <children> 
     <TableView fx:id="tvMain" layoutY="-6.0" prefHeight="200.0" prefWidth="273.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
    </children> 
</AnchorPane> 
관련 문제