java - 带预览的自定义对话框

标签 java swing user-interface dialog

对于我当前的项目,我需要一个自定义对话框,允许用户选择一个值并获得其详细信息的自定义预览。我尝试编写自己的扩展 JFrame 的窗口类,但直到现在我还停留在最重要的部分:如何显示对话框的窗口,让用户进行输入,然后返回选定的值(value)?

我尝试查看 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; }
    }
}

最佳答案

How do I show the dialog's window, let the user do his input and then return the selected value?

通常的方法是使用 modal dialog ; JOptionPane , 见过 here , 是处理这种用法的便捷方式。可以找到更多示例 here .

Is there's no option other than using JOptionPane?

您可以使用 JDialog,有或没有 JOptionPane,如图所示 herehere , 分别。

关于java - 带预览的自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33021055/

相关文章:

java - HWPFDocument 位于哪里?我在 POI 项目中找不到它

java - 图像未加载到 JTable 上

java - JDialog关闭按钮事件

ios - 尝试设置 UIButton 的约束时出错

java - 如何通过单击按钮在面板的同一区域显示不同的 JPanel

java - 使用单选按钮更改 GUI 的颜色

java - 如何在 Java 中解析 HTTP 请求?

java - 多线程中的 SNMP Walk

java - Files.walk 似乎没有进入子文件夹 Java

java - 使用可观察对象或事件或接口(interface)传递值