2015-04-06 79 views
1

我一直在试图弄清楚如何制作一个基于数组内容动态创建的弹出窗口,并且我几乎可以确定我错过了一些至关重要的东西帮助我解决并充分理解这个问题。用JRadioButtons使用HashMap填充JPanel

我究竟在做什么?

我有一个程序,通过某些目录循环,收集所有的文件夹名称,并将其存储在ArrayList。当我尝试使用该动态创建窗口时出现问题ArrayList。我不确定如何解决这个问题。

什么是通过流程

我目前我有3个班。视图,模型和控制类。包含文件夹的数组存储在模型类中。我通过控制类检索它。我在ActionListener以及HashMap内创建了一个新的JPanel。我循环通过HashMap 添加String名称和JRadioButton我试图填充窗口,但我真的不知道如何。

这里是我的工作的一段代码:

public void actionPerformed(ActionEvent e) { 

     if (e.getSource() == theView.viewButton) { 

      System.out.println("View Button clicked"); 
      theView.setBotTextArea(""); 
      theView.setBotTextArea("Viewing..."); 

      JPanel radioPanel = new JPanel(); 

      // Method that gather folder names and stores it in an array 
      theModel.listAllFolders(); 
      // Make the categories array and store the names 
      ArrayList<String> categories = theModel.getListOfCategories(); 

      // Create a hashmap with names and JRadioButtons 
      HashMap<String, JRadioButton> buttonMap = new HashMap<String, JRadioButton>(); 

      // loop to fill up the HashMap 
      for (int i = 0; i < categories.size(); i++) { 

       buttonMap.put(categories.get(i), new JRadioButton(categories.get(i))); 

      } 

      for (Entry<String, JRadioButton> entry : buttonMap.entrySet()) { 

       // Not sure how to retrieve the hashmap data to create and 
        fill up the window 

      } 
} 

我新的极端,以包含HashMap(我努力学习的话),所以我甚至不知道这是否是一个好主意开始。我一直坚持这项任务近3天。在过去,我试图使用数组来完成类似的任务,但我几乎可以肯定,这是我的一个巨大的逻辑错误,阻止我完成它。

我很感谢在这个问题上的任何新的见解。

+0

为什么'ArrayList - > HashMap - > JRadioButton'?为什么不是更简单的'ArrayList - > JRadioButton'?或者,如果你需要创建一个HashMap,那么为什么不用一个for循环来创建JRaidoButton,它填充HashMap,将JRradioButton放到一个ButtonGroup中,并将JRadioButton放到一个JPanel上你将JRadioButtons添加到JPanel?)? –

+0

我的逻辑是我将来可能会在右侧'JRadioButton'中分配正确的名称。 'HashMap'似乎是防止这种情况发生的最佳选择。尽管如此,我可能会过度复杂化。编辑:你介意在你的编辑中显示一段代码吗?这几乎是我一直想要做的。 – Lotix

+0

您可能会为我们过度复杂化它。再次,请给我们更多的细节。我敢肯定,你知道如何将JRadioButtons添加到JPanel,对吗?只需调用'myPanel.add(myRadioButton)',所以我不确定将您的JRadioButton放到JPanel中有什么困惑。 –

回答

2

如果您在JRadioButton中显示的文本与放置在HashMap中的文本相同,那么我不会看到需要使用HashMap。只要确保您使用正确的文本设置了您的JRadioButton的actionCommand字符串,将所有的JRadioButtons添加到同一个ButtonGroup中,并且当您需要选择时,从ButtonGroup的getSelection()方法返回的ButtonModel中获取actionCommand。

例如

for (String text : fileList) { 
    JRadioButton btn = new JRadioButton(text); 
    btn.setActionCommand(text); // radiobuttons don't do this by default 
    buttonGroup.add(btn); // ButtonGroup to allow single selection only 
    myRadioPanel.add(btn); // JPanel usually uses a GridLayout 
} 
// if myRadioPanel is already in the GUI, then revalidate and repaint it 

后获得的选择(如果通过其他按钮的ActionListener的实现:

ButtonModel model = buttonGroup.getSelection(); 
if (model != null) { 
    selectedText = model.getActionCommand(); 
} 

或者,如果使用一个ActionListener添加到单选按钮,然后只需拿到动作事件的actionCommand属性

由于为了在JPanel中添加JRadioButtons,我确信你已经知道如何做到这一点,如果某个步骤让你感到困惑,那么你还没有告诉我们。

+0

你和@ControlAltDel的答案都对我有很大的帮助。我得到了它最重要的部分,它工作得很好。谢谢。 – Lotix

+0

@Lotix:很高兴它有帮助,但我仍然对你被卡住的东西感到困惑。 –

+0

它归结于我不知道如何使用数组,并使用正确的名称和变量填充JRadioButtons的窗口。我意识到我正在使它变得比实际复杂得多。 – Lotix

2
ArrayList<String> categories = theModel.getListOfCategories(); 
for (String val : categories) { 
    JRadioButton jb = new JRadioButton(val); 
    radioPanel.add(jb); 
} 
//now you need to add the radioPanel JPanel to an existing Container, or call setContentPane(radioPanel);