2013-02-11 42 views
0

我的应用程序的结构如下:力的JOptionPane保持开放

  • 主窗口允许用户选择CSV文件进行解析
  • 选择CSV文件后的JOptionPane出现,JOptionPane的含有滴具有各种选择的下拉菜单;其中每产生一个单独的窗口
  • 目前,选择从菜单进行和“OK”按钮被点击
后的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); 
    } 
} 
+0

请发布您的代码。 – Aubin 2013-02-11 22:03:45

+2

可能会更好地把你的下拉菜单放在不同的选框中,这会给你更多的行为选项 – 2013-02-11 22:05:51

+0

我也注意到你正在使用'=='比较'字符串'。这不是'Java'中的方法。你应该使用'equals()'方法来代替:'menuSelection.equals(menuChoice [0])' – Michael 2013-02-11 23:29:26

回答

2

我想对于与选择的窗口,甚至 后保持开放用户选择的选项,让他们如果他们愿意,可以选择另一个选项 。我如何让JOptionPane保持打开状态而不是 其默认行为,在选择下拉值为 后关闭?

2

在这两种选项窗格中,我可以改变我的选择,因为很多次,因为我就像在结束之前一样。第三个选项窗格将显示(默认为)第1个中较早选定的值 - 当前值。

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。这里是list & combo之间的比较。

注意两个JList & JComboBox可以使用渲染为组合看出。重要的区别是列表是支持多选的嵌入式组件。

+0

*“我希望窗户的选择保持开放”*如果通过'窗口'表示选项窗格 - 它保持打开状态。如果你的意思是组合下拉,它是一个下拉菜单,**不是一个窗口!** – 2013-02-11 23:15:22

1

下面的解决方案不会给你一个下拉菜单,但它可以让你选择多个值。

可以使用JList来存储您的选择和使用JOptionPane.showInputMessage这样的:在listOfChoices

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

使用方法getSelectedIndices()JOptionPane.showInputDialog()后,将返回一个整数数组,其中包含从被选中的指标JList,您可以使用ListModel来获得它们的值:

int[] ans = listOfChoices.getSelectedIndices(); 
ListModel listOfChoicesModel = listOfChoices.getModel(); 
for (int i : ans) { 
    System.out.println(listOfChoicesModel.getElementAt(i)); 
} 
+0

只要我看到你的答案,我听到一分钱的下降。我认为你已经明白OP的实际需要。 – 2013-02-11 23:16:57

+0

@AndrewThompson谢谢:)他想要一个下拉列表...这不一样,但如果他没有很多价值,这可以做到这一点。 – Michael 2013-02-11 23:21:54

+0

尽管这允许用户选择多个值,但点击“确定”按钮后,JOptionPane仍会关闭。我希望用户能够在初始选择后继续选择项目。 – 2013-02-12 21:23:57