Zer0
22
2019-05-17 22:11:04
6
806

JavaFX) 테이블 마우스 클릭이 안되네요 .. 오류도 뜨질 않구요 ㅠㅠ 도와주세요!!


아래 코드와 같이 코드를 짰습니다. 다른 커스텀 다이얼로그는 창이 뜨는데 ... 테이블 뷰만 클릭을 하면 뜨지를 않네요 ..

컴파일러에서 오류도 안나고 아무래도 클릭 자체가 안 먹히는거 같은데 ..

혹시 어디가 문제인지 알려주실 수 있을까요 ㅠㅠ?

package application;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class RootController implements Initializable {
    @FXML private TableView<Student> tableView;
    private ArrayList<Student> stuArr = new ArrayList<>();
    private Stage primaryStage;
    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        TableColumn tcName = tableView.getColumns().get(0);
        tcName.setCellValueFactory(new PropertyValueFactory("name"));
        tcName.setStyle("-fx-alignment: CENTER;");
        TableColumn tcKorean = tableView.getColumns().get(1);
        tcKorean.setCellValueFactory(new PropertyValueFactory("korean"));
        tcKorean.setStyle("-fx-alignment: CENTER;");
        TableColumn tcMath = tableView.getColumns().get(2);
        tcMath.setCellValueFactory(new PropertyValueFactory("math"));
        tcMath.setStyle("-fx-alignment: CENTER;");
        TableColumn tcEnglish = tableView.getColumns().get(3);
        tcEnglish.setCellValueFactory(new PropertyValueFactory("English"));
        tcEnglish.setStyle("-fx-alignment: CENTER;");
      //테이블 뷰 클릭시 마우스이벤트
        tableView.setOnMouseClicked(event -> handleTableViewMouseCliked(event));
    }

    public void handleBtnBarchartAction(ActionEvent event) throws Exception{
        Stage dialog = new Stage(StageStyle.UTILITY);
        dialog.initModality(Modality.WINDOW_MODAL);
        dialog.initOwner(primaryStage);
        dialog.setTitle("막대 그래프");
        Parent parent = FXMLLoader.load(getClass().getResource("barchart.fxml"));
        BarChart barChart = (BarChart) parent.lookup("#barChart");
        XYChart.Series koreanP = new XYChart.Series();
        koreanP.setName("국어");
        ObservableList koreanList = FXCollections.observableArrayList();
        XYChart.Series mathP = new XYChart.Series();
        mathP.setName("수학");
        ObservableList mathList = FXCollections.observableArrayList();
        XYChart.Series englishP = new XYChart.Series();
        englishP.setName("영어");
        ObservableList englishList = FXCollections.observableArrayList();
        for(int i = 0; i < stuArr.size(); i++){
            koreanList.add(new XYChart.Data(stuArr.get(i).getName(),stuArr.get(i).getKorean()));
            mathList.add(new XYChart.Data(stuArr.get(i).getName(),stuArr.get(i).getMath()));
            englishList.add(new XYChart.Data(stuArr.get(i).getName(),stuArr.get(i).getEnglish()));
        }
        koreanP.setData(koreanList);
        mathP.setData(mathList);
        englishP.setData(englishList);
        barChart.getData().add(koreanP);
        barChart.getData().add(mathP);
        barChart.getData().add(englishP);

        Button btnOut = (Button) parent.lookup("#btnBarOut");
        btnOut.setOnAction(event1 -> dialog.close());
        Scene scene = new Scene(parent);
        dialog.setScene(scene);
        dialog.setResizable(false);
        dialog.show();
    }
    public void handleBtnAddAction(ActionEvent event) throws Exception{
            Stage dialog = new Stage(StageStyle.UTILITY);
            dialog.initModality(Modality.WINDOW_MODAL);
            dialog.initOwner(primaryStage);
            dialog.setTitle("추가");
            Parent parent = FXMLLoader.load(getClass().getResource("form.fxml"));
            Button btnSave = (Button) parent.lookup("#btnSave");
            btnSave.setOnAction(e -> {
                TextField name_txt = (TextField) parent.lookup("#name_txt");
                TextField korean_txt = (TextField) parent.lookup("#korean_txt");
                TextField math_txt = (TextField) parent.lookup("#math_txt");
                TextField english_txt = (TextField) parent.lookup("#english_txt");
                stuArr.add(new Student(name_txt.getText(),Integer.parseInt(korean_txt.getText()),Integer.parseInt(math_txt.getText()),Integer.parseInt(english_txt.getText())));
                tableView.setItems(FXCollections.observableArrayList());
                for (int i = 0; i < stuArr.size(); i++) {
                    tableView.getItems().add(stuArr.get(i));
                }
                dialog.close();
            });
            Button btnCancel = (Button) parent.lookup("#btnCancel");
            btnCancel.setOnAction(e -> dialog.close());
            Scene scene = new Scene(parent);
            dialog.setScene(scene);
            dialog.setResizable(false);
            dialog.show();
    }
// 마우스 이벤트 !!
    public void handleTableViewMouseCliked(MouseEvent event){
        if(event.getClickCount() != 2) return;
        try {
            Stage dialog = new Stage(StageStyle.UTILITY);
            dialog.initModality(Modality.WINDOW_MODAL);
            dialog.initOwner(primaryStage);
            dialog.setTitle("파이 그래프");
            Parent parent = FXMLLoader.load(getClass().getResource("piechart.fxml"));
            PieChart pieChart = (PieChart) parent.lookup("#pieChart");
            Student student = tableView.getSelectionModel().getSelectedItem();
            pieChart.setData(FXCollections.observableArrayList(
                    new PieChart.Data("국어", student.getKorean()),
                    new PieChart.Data("수학", student.getMath()),
                    new PieChart.Data("영어", student.getEnglish())
            ));
            Button btnPieOut = (Button) parent.lookup("#pieBtnOut");
            btnPieOut.setOnAction(e -> dialog.close());
            Scene scene = new Scene(parent);
            dialog.setScene(scene);
            dialog.show();
        }catch (Exception e){}
    }
}
0
  • 답변 6

  • k20081001
    252
    2019-05-18 06:27:43

    클릭 이벤트가 !=2 인데 먹을까요?

  • Zer0
    22
    2019-05-18 10:55:00
    public void handleTableViewMouseCliked(MouseEvent event){
    if(event.getClickCount() == 2) { // 이부분을 2로 바꿔도 실행이 안되어요 ...
    try {
    Stage dialog = new Stage(StageStyle.UTILITY);
    dialog.initModality(Modality.WINDOW_MODAL);
    dialog.initOwner(primaryStage);
    dialog.setTitle("파이 그래프");
    Parent parent = FXMLLoader.load(getClass().getResource("piechart.fxml"));
    PieChart pieChart = (PieChart) parent.lookup("#pieChart");
    Student student = tableView.getSelectionModel().getSelectedItem();
    pieChart.setData(FXCollections.observableArrayList(
    new PieChart.Data("국어", student.getKorean()),
    new PieChart.Data("수학", student.getMath()),
    new PieChart.Data("영어", student.getEnglish())
    ));
    Button btnPieOut = (Button) parent.lookup("#pieBtnOut");
    btnPieOut.setOnAction(e -> dialog.close());
    Scene scene = new Scene(parent);
    dialog.setScene(scene);
    dialog.show();
    }catch (Exception e){}
    }}
  • Zer0
    22
    2019-05-18 10:57:22

    아무래도 마우스이벤트 자체가 발생을 안하는거 같은데 .. if구문을 지우고 클릭을 해봐도 클릭이 안 먹히는거 같아요 .. 혹시 뭔가 다른 부분이 잘 못 되었을까요 ?? 

    혹시나 해서 scean 자체가 문제 일까봐 메인 부분도 올려볼게요!! 그 primaryStage로 받아옵니다!!

    package application;

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

    public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("root.fxml"));
    Parent root = loader.load();
    RootController controller = loader.getController();
    controller.setPrimaryStage(primaryStage);

    primaryStage.setTitle("AppMain");
    primaryStage.setScene(new Scene(root, 800, 500));
    primaryStage.show();
    }


    public static void main(String[] args) {
    launch(args);
    }
    }
  • Zer0
    22
    2019-05-18 11:03:12

    아 버튼이벤트 자체를 잘 못 받아왔었던거 같아요 ... 나오네요 .. ㅠㅠ 죄송합니다 조금 더 고민해보고 말씀 드렸어야 했는데 감사합니다 .. ㅠㅠ

  • k20081001
    252
    2019-05-18 19:54:21

    전 자바fx가 뭔지 들어서 존재만 알지 해본적은 없습니다.

    스스로 찾으셨다니 대단하십니다.

    그리고 다행이예요.

  • Zer0
    22
    2019-05-18 23:13:47

    그래도 답변 달아주셔서 정말 감사합니다~!

  • 로그인을 하시면 답변 을 등록할 수 있습니다.