2013-02-11 40 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