JOptionPane保持开放

JOptionPane保持开放

本文介绍了Force JOptionPane保持开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的构造如下:

My application is constructed as follows:


  • 主窗口允许用户选择CSV文件进行解析

  • 选择CSV文件后,JOptionPane将出现,JOptionPane包含一个带有多种选择的下拉菜单;每个都生成一个单独的窗口

  • 目前,JOptionPane在从菜单中进行选择后关闭,点击确定按钮

我正在寻找一种强制JOptionPane保持打开状态的方法,以便用户可以选择不同的东西。我想要通过点击右上角的X关闭JOptionPane。如果使用JOptionPane不是最好的方式,我也可以使用其他可能性来实现类似的结果。

I am looking for a way to force the JOptionPane to remain open so that the user can select something different if they want. I would like the JOptionPane to be closed only by clicking the "X" in the upper right corner. I am also open to other possibilities to achieve a similar result if using a JOptionPane isn't the best way to go on this.

这是相关的代码块I 'm work on:

Here is the relevant block of code I'm working on:

try
{
    CSVReader reader = new CSVReader(new FileReader(filePath), ',');

    // Reads the complete file into list of tokens.
    List<String[]> rowsAsTokens = null;

    try
    {
        rowsAsTokens = reader.readAll();
    }

    catch (IOException e1)
    {
        e1.printStackTrace();
    }

    String[] menuChoices = { "option 1", "option 2", "option 3" };

    String graphSelection = (String) JOptionPane.showInputDialog(null,
            "Choose from the following options...", "Choose From DropDown",
            JOptionPane.QUESTION_MESSAGE, null,
            menuChoices, // Array of menuChoices
            menuChoices[0]); // Initial choice

    String menuSelection = graphSelection;

    // Condition if first item in drop-down is selected
    if (menuSelection == menuChoices[0] && graphSelection != null)
    {
        log.append("Generating graph: " + graphSelection + newline);

        option1();
    }

    if (menuSelection == menuChoices[1] && graphSelection != null)
    {

        log.append("Generating graph: " + graphSelection + newline);

        option2();
    }

    if (menuSelection == menuChoices[2] && graphSelection != null)
    {
        log.append("Generating graph: " + graphSelection + newline);

        option3();
    }

    else if (graphSelection == null)
    {
        log.append("Cancelled." + newline);
    }
}


推荐答案

这些选项窗格中的任一个,我可以在关闭之前更改我的选择多次。第三个选项窗格将显示(默认)先前在第1个 - 当前值中选择的值。

In either of these option panes, I can change my choice as many times as I like before closing it. The 3rd option pane will show (default to) the value selected earlier in the 1st - the current value.

import java.awt.*;
import javax.swing.*;

class Options {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                Object[] options = {
                    "Option 1",
                    "Option 2",
                    "Option 3",
                    "None of the above"
                };
                JComboBox optionControl = new JComboBox(options);
                optionControl.setSelectedIndex(3);
                JOptionPane.showMessageDialog(null, optionControl, "Option",
                        JOptionPane.QUESTION_MESSAGE);
                System.out.println(optionControl.getSelectedItem());

                String graphSelection = (String) JOptionPane.showInputDialog(
                        null,
                        "Choose from the following options...",
                        "Choose From DropDown",
                        JOptionPane.QUESTION_MESSAGE, null,
                        options, // Array of menuChoices
                        options[3]); // Initial choice
                System.out.println(graphSelection);

                // show the combo with current value!
                JOptionPane.showMessageDialog(null, optionControl, "Option",
                        JOptionPane.QUESTION_MESSAGE);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

我觉得Michael猜到了一个 JList的。以下是& 。

I think Michael guessed right with a JList. Here is a comparison between list & combo.

请注意, JList & JComboBox 可以使用组合中看到的渲染器。重要的区别是列表是支持多重选择的嵌入式组件。

Note that both JList & JComboBox can use a renderer as seen in the combo. The important difference is that a list is an embedded component that supports multiple selection.

这篇关于Force JOptionPane保持开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:54