自定义对话框与预览 [英] Custom dialog with preview

查看:165
本文介绍了自定义对话框与预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

For my current project I need a custom dialog that allows the user to select a value and get a custom preview of its details. I tried writing my own window class that extends JFrame but up until now I'm stuck at the most important part: how do I show the dialog's window, let the user do his input and then return the selected value?

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

I tried looking into the code of JOptionPane.showInputDialog but I got confused instead of understanding how that works.

以下是 sscce for我的问题:

Here's an sscce for my problem:

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?

通常的方法是使用 模态对话框 ; JOptionPane ,见此处,是处理此用法的便捷方式。更多的例子可以在这里找到。

The usual approach is to use a modal dialog; JOptionPane, seen here, is a convenient way to handle this usage. More examples may be found here.


除了使用 JOptionPane

您可以使用 JDialog ,带或不带 JOptionPane ,如此处 here

You can use a JDialog, with or without a JOptionPane, as shown here and here, respectively.

这篇关于自定义对话框与预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆