带预览的自定义对话框

时间:2015-10-08 16:19:26

标签: java swing user-interface dialog

对于我当前的项目,我需要一个自定义对话框,允许用户选择一个值并获取其详细信息的自定义预览。我尝试编写自己的窗口类,扩展JFrame,但到目前为止我一直困在最重要的部分:如何显示对话框的窗口,让用户输入他的输入,然后返回所选的值?< / p>

我尝试查看JOptionPane.showInputDialog的代码,但我感到困惑,而不是理解它是如何工作的。

这是我的问题的sscce

import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class MyCustomDialog extends JFrame {
    JComboBox<MyCustomObject> comboBox;
    JTextArea details;

    public MyCustomDialog(Vector<MyCustomObject> data) {
        comboBox = new JComboBox<>(data);
        comboBox.setSelectedIndex(-1);
        comboBox.setEditable(false);
        comboBox.addActionListener(ActionEvent -> updateDetails());
        getContentPane().add(comboBox);

        details = new JTextArea();
        details.setEditable(false);
        details.setOpaque(false);
        getContentPane().add(details);

        setVisible(true);
    }

    void updateDetails() {
        int selectedIndex = comboBox.getSelectedIndex();
        if(selectedIndex < 0) {
            details.setText("");
            return;
        }

        MyCustomObject selected = comboBox.getItemAt(selectedIndex);
        details.setText(selected.getDescription());
    }

    static MyCustomObject showDialog(Vector<MyCustomObject> vec) {
        // this is where I'm stuck

        // showDialog needs to:
        // 1. create the frame and show it to the user
        // 2. let the user choose a value and also change their selection multiple times
        // 3. let the user confirm the selection, for example with a button
        // 4. return the selected value

        return null;
    }

    public static void main(String... args) {
        Vector<MyCustomObject> vec = new Vector<>();
        vec.add(new MyCustomObject("Test Object 1", "Test Information 1"));
        vec.add(new MyCustomObject("Test Object 2", "Test Information 2"));
        System.out.println(showDialog(vec) + " was selected.");
    }

    public static class MyCustomObject {
        final String description;
        final String name;

        public MyCustomObject(String name, String description) {
            this.name = name;
            this.description = description;
        }

        String getDescription() { return description; }

        @Override
        public String toString() { return name; }
    }
}

感谢trashgod的回答更新了代码

import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class MyCustomDialog extends JPanel {
    JComboBox<MyCustomObject> comboBox;
    JTextArea details;

    MyCustomDialog(Vector<MyCustomObject> data) {
        comboBox = new JComboBox<>(data);
        comboBox.setSelectedIndex(-1);
        comboBox.setEditable(false);
        comboBox.addActionListener(ActionEvent -> updateDetails());
        add(comboBox);

        details = new JTextArea();
        details.setEditable(false);
        details.setOpaque(false);
        add(details);
    }

    void updateDetails() {
        int selectedIndex = comboBox.getSelectedIndex();
        if(selectedIndex < 0) {
            details.setText("");
            return;
        }

        MyCustomObject selected = comboBox.getItemAt(selectedIndex);
        details.setText(selected.getDescription());
    }

    static MyCustomObject showDialog(Vector<MyCustomObject> vec) {
        MyCustomDialog dialog = new MyCustomDialog(vec);

        int result = JOptionPane.showConfirmDialog(null, dialog, "Test Dialog", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            int selectedIndex = comboBox.getSelectedIndex();
            if (selectedIndex >= 0) return comboBox.getItemAt(selectedIndex);
        }

        return null;
    }

    public static void main(String... args) {
        Vector<MyCustomObject> vec = new Vector<>();
        vec.add(new MyCustomObject("Test Object 1", "Test Information 1"));
        vec.add(new MyCustomObject("Test Object 2", "Test Information 2"));
        System.out.println(showDialog(vec) + " was selected.");
    }

    public static class MyCustomObject {
        final String description;
        final String name;

        public MyCustomObject(String name, String description) {
            this.name = name;
            this.description = description;
        }

        String getDescription() { return description; }

        @Override
        public String toString() { return name; }
    }
}

1 个答案:

答案 0 :(得分:3)

  

如何显示对话框的窗口,让用户输入他的输入然后返回所选的值?

通常的方法是使用modal dialog;查看JOptionPanehere是处理此用法的便捷方式。可以找到更多示例here

  

除了使用JOptionPane之外,没有其他选择吗?

您可以使用JDialog,有或没有JOptionPane,分别显示herehere