ComboBox崩溃javafx 8 JDK 51

时间:2015-08-18 21:59:59

标签: combobox javafx-8

所以我使用ComboBox的程序已经停止工作了。在我将jdk从8u20更新到8u50之前,现在当相同的程序运行而没有任何更改时,如果选择ComboBox而没有先选中它,它会导致程序冻结然后崩溃。

我只是在搜索后在网络上的另一个地方看到这个,防止崩溃的唯一方法是按Tab键直到选择了组合框并使用箭头键选择。

还有其他人听说过这个吗?

以下是一个示例,如果您复制粘贴这两个类并运行main,则可以轻松运行。

主要课程:

import javafx.application.Application;
import javafx.stage.Stage;

import java.util.ArrayList;

/**
 * Created by Josh on 19/8/2015.
 */
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

        ArrayList<String> options = new ArrayList<String>();
        options.add("Apple");
        options.add("Orange");
        options.add("Pear");

        String result = Selection_Dialog.show(options, "Orange");

    }

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

ComboBox类:

import com.sun.glass.ui.Screen;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.awt.*;

import java.util.ArrayList;

class Selection_Dialog {

    //Window Properties
    private static final String TITLE = "Selection Dialog";
    private static String DEFAULT_MESSAGE = "";
    private static int DEFAULT_WIDTH = 400;
    private static int DEFAULT_HEIGHT = 250;
    private static Point DEFAULT_POSITION = new Point();

    //Window Components
    private static Stage stage;
    private static javafx.scene.control.Label messageLabel;
    private static javafx.scene.control.Button okButton;
    private static ComboBox selector;

    //Show Window Popup
    public static String show(ArrayList<String> options, String initial) {
        return show(options, initial, DEFAULT_MESSAGE, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_POSITION);
    }
    public static String show(ArrayList<String> options, String initial, String message) {
        return show(options, initial, message, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_POSITION);
    }
    public static String show(ArrayList<String> options, String initial, String message, Point position) {
        return show(options, initial, message, DEFAULT_WIDTH, DEFAULT_HEIGHT, position);
    }
    public static String show(ArrayList<String> options, String initial, String message, int width, int height) {
        return show(options, initial, message, width, height, DEFAULT_POSITION);
    }
    public static String show(ArrayList<String> options, String initial, String message, int width, int height, Point position) {

        //Setup Properties
        stage = new Stage();
        BorderPane root = new BorderPane();
        stage.initStyle(StageStyle.UTILITY);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setScene(new Scene(root, width, height));
        stage.setTitle(TITLE);
        stage.setResizable(false);
        stage.setOnCloseRequest(new EventHandler() {
            @Override
            public void handle(javafx.event.Event event) {
                event.consume();
            }
        });
        if(position == DEFAULT_POSITION){
            int x, y;
            x = Screen.getMainScreen().getWidth()/2;
            y = Screen.getMainScreen().getHeight()/2;
            x = x - width/2;
            y = y - height/2;
            position = new Point(x, y);
        }
        stage.setX(position.getX());
        stage.setY(position.getY());


        //Create Components
        messageLabel = new javafx.scene.control.Label(message);
        okButton = new javafx.scene.control.Button("Ok");
        selector = new ComboBox();
        selector.getItems().addAll(options);
        if(options.size() > 0){
            if(initial == null){
                selector.getSelectionModel().select(0);
            }else if(options.contains(initial)){
                selector.getSelectionModel().select(initial);
            }else{
                selector.getSelectionModel().select(0);
            }

        }

        //Align Components
        root.setAlignment(messageLabel, Pos.CENTER);
        root.setAlignment(okButton, Pos.CENTER);
        messageLabel.setTranslateY(15);
        okButton.setTranslateY(-25);
        messageLabel.setTextAlignment(TextAlignment.CENTER);
        messageLabel.setWrapText(true);

        //Add Components
        root.setBottom(okButton);
        root.setTop(messageLabel);
        root.setCenter(selector);

        //Add Listeners

        okButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.close();
            }
        });

        //Show Window
        stage.showAndWait();

        return selector.getSelectionModel().getSelectedItem().toString();
    }

}

我创建了ComboBox类,因此我可以在将来的项目中轻松地从用户那里获得选择对话框。它以前工作得很好,可能有问题吗?

0 个答案:

没有答案