0

我的应用程序构造如下:

  • 主窗口允许用户选择要解析的 CSV 文件
  • JOptionPane 在选择 CSV 文件后出现,并且 JOptionPane 包含一个带有各种选项的下拉菜单;每个都生成一个单独的窗口
  • 目前,在从菜单中进行选择并单击“确定”按钮后,JOptionPane 将关闭

我正在寻找一种方法来强制 JOptionPane 保持打开状态,以便用户可以根据需要选择不同的东西。我希望仅通过单击右上角的“X”来关闭 JOptionPane。如果使用 JOptionPane 不是最好的方法,我也愿意接受其他可能性来实现类似的结果。

这是我正在处理的相关代码块:

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);
    }
}
4

3 回答 3

2

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

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);
    }
}

我认为迈克尔猜对了JList这是listcombo之间的比较。

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

于 2013-02-11T23:13:40.650 回答
2

即使在用户选择了一个选项后,我希望带有选项的窗口保持打开状态,以便他们可以根据需要选择另一个选项。如何让 JOptionPane 保持打开状态,而不是在选择下拉值后关闭它的默认行为?

  • 这是基本属性,默认情况下JOptionPane已处理,如果没有肮脏的黑客,这是不可能的,不要那样做

  • 使用JDialog(可能,可能未装饰)具有适当的ModalityType值

  • 你可以使用一些变体Java & Ribbon

  • 您可以使用 JMenuItems(非常好的方式)将所需的选择放入JComboBox 或 JMenu 到 JLayer 或 GlassPane

  • 我认为这是JToolBarJMenu的标准工作

于 2013-02-11T22:55:05.623 回答
1

以下解决方案不会为您提供下拉菜单,但它允许您选择多个值。

您可以使用 aJList来存储您的选择并JOptionPane.showInputMessage像这样使用:

JList listOfChoices = new JList(new String[] {"First", "Second", "Third"});
JOptionPane.showInputDialog(null, listOfChoices, "Select Multiple Values...", JOptionPane.QUESTION_MESSAGE);

使用getSelectedIndices()onlistOfChoices之后的方法JOptionPane.showInputDialog()将返回一个整数数组,其中包含从 中选择的索引,JList您可以使用 aListModel来获取它们的值:

int[] ans = listOfChoices.getSelectedIndices();
ListModel listOfChoicesModel = listOfChoices.getModel();
for (int i : ans) {
    System.out.println(listOfChoicesModel.getElementAt(i));
}
于 2013-02-11T23:14:26.033 回答