본문 바로가기
Language/JAVA

javafx TableView 테이블뷰 / listView 리스트뷰

by javapp 자바앱 2020. 5. 9.
728x90

TableView

 

 

fxml





main.java

 

public class Main extends Application {

            

             @Override

             public void start(Stage primaryStage) throws Exception {

                           Parent root = FXMLLoader.load(getClass().getResource("testTableVIew.fxml"));

                           Scene scene = new Scene(root, 600, 400);

                           scene.getStylesheets().add(getClass().getResource("application.css")

                                                     .toExternalForm());

                          

                          

                           primaryStage.setScene(scene);

                           primaryStage.show();

                          

             }

            

             public static void main(String[] args) {

                           launch(args);

             }

}


testController  .java

 

public class testController  implements Initializable{

             @FXML

             private TableView<Person> tableView;

             @FXML

             private TableColumn<Person, String> nameCol;

             @FXML

             private TableColumn<Person, Integer> ageCol;

            

              ObservableList<Person> data = FXCollections.observableArrayList();

              

              public testController() {

                            data.add(new Person("이름1", 24));

                            data.add(new Person("이름",25));

              }

             @SuppressWarnings("unchecked")

             @Override

             public void initialize(URL arg0, ResourceBundle arg1) {

 

           nameCol.setCellValueFactory(cellData -> cellData.getValue().getName());

           ageCol.setCellValueFactory(cellData -> cellData.getValue().getAge().asObject());

          

                           tableView.setItems(data);

                          

             }

 

             public void cancel() {

                           Alert alert = new Alert(AlertType.CONFIRMATION);

                           alert.setTitle("Confirmation Dialog");

                           alert.setHeaderText("Look, a Confirmation Dialog");

                           alert.setContentText("Are you ok with this?");

 

                           Optional<ButtonType> result = alert.showAndWait();

                           if (result.get() == ButtonType.OK){

                               // ... user chose OK

                                        System.out.println("OK");

                           } else {

                               // ... user chose CANCEL or closed the dialog

                                        System.out.println("취소");

                           }

 

 

             }

}

 

 


public class Person {

             private StringProperty name;

             private IntegerProperty age;

            

             public Person(String name, int age) {

                           super();

                           this.name = new SimpleStringProperty(name);

                           this.age = new SimpleIntegerProperty(age);

             }

 

             public StringProperty getName() {

                           return name;

             }

             public void setName(StringProperty name) {

                           this.name =name;

             }

 

             public IntegerProperty getAge() {

                           return age;

             }

             public void setAge(IntegerProperty age) {

                           this.age =age;

             }

            

}

 


module Project_fx {
 exports application;
 requires javafx.base;
 requires javafx.graphics;
 requires javafx.controls;
 requires javafx.fxml;
 requires javafx.media;
 requires javafx.swing;
 requires javafx.web;
 requires javafx.swt;  
 opens application to javafx.fxml;  //특히 이거 설정 안하면 에러남

 

https://stackoverflow.com/questions/52984791/fxmlloader-load-works-fine-in-pre-java-9-style-but-fails-in-modules

 

FXMLLoader.load() works fine in pre-java 9 style, but fails in modules

I successfully made a simple JavaFX FXML program (in pre-Java 9 style, not module) I used 3 files and the main class as below: └───javafxfxmldemo FXMLDocument.fxml FXMLDocumentCont...

stackoverflow.com

 

 

 

 


리스트뷰

 

 

            

public void setItem() {

                           //임시로 값 넣기

                           AttractionReview AR1 = new AttractionReview("r1", "20200525","리뷰제목1","20140299","관광지명","좋다",5);

                           AttractionReview AR2 = new AttractionReview("r2", "20200525","리뷰제목2","20140299","관광지명","좋다",5);

                           AttractionReview AR3 = new AttractionReview("r3", "20200525","리뷰제목3","20140299","관광지명","좋다",5);

                          

                           //

                           ObservableList<String> list = FXCollections.observableArrayList();

                           list.add(AR1.getReviewId() + "\n" + AR1.getUserId()+"\n"+AR1.getReviewTitle());

                           list.add(AR2.getReviewId());

                           list.add(AR3.getReviewId());

                           reviewListView.setItems(list);

             }

 

             //값 얻기

             public String getListValue() {

                          

                           return reviewListView.getSelectionModel().getSelectedItem();

             }

 

getSelectionModel().getSelectedItem()
getSelectedIndex()

getSelectedIndex() 할 경우

인덱스의 값이 나온다.

 

 


참고사이트

 

[Java FX] ListView

리스트뷰와 콤보박스를 만들어 선택한 내용을 label 에 띄우는 내용이다. 아래와 같은 화면이 나와야 한다....

blog.naver.com

 

댓글