2013-10-03 47 views
3

之前,我問過這個問題,我經歷了許多例子去了,他們中的一些是相關的,但沒有回答我的需求:獲得選擇`JCheckBox`相關`id`搖擺

我有一個列表,keyMessageList包括id和相關keyWord字符串

我顯示使用下面的代碼我的JPanel keyWord的名字:

for (KeyMessage obj : keyMessageList) { 
     checkBox = new JCheckBox(obj.getSmsKey()); 
     displayKeywordPanel.checkBooxPanel.add(checkBox); 
    } 

我得到了以下的輸出:

output image

現在,我的要求是:如果我選擇關鍵字,我需要得到id與選定的keyword相關聯。我在web applications這樣做過很多次類似的事情,但現在我需要在鞦韆上做同樣的事情。我在Web應用程序上使用了以下代碼來滿足要求。 適用網址爲:

<h:selectOneMenu id="branch" value="#{createUser.branchId}" required="true" requiredMessage="Please choose branch"> 
      <f:selectItems value="#{allBranch}"/> 
</h:selectOneMenu> 

任何鞦韆專家請幫助我。

注意我可以選擇多個複選框,並從JPA查詢keyMessageList回報。

回答

3

我有一個列表,keyWordsList其中包括id和相關的關鍵字。

請勿使用列表。請使用Map,其中「關鍵字」作爲關鍵字,「id」作爲值。然後,當您選擇複選框時,您將從地圖中獲取ID。

另一種選擇是創建你JCheckBox的如下:

JCheckBox checkBox = new JCheckBox(keyword); 
checkbox.setActionCommand(id); 

再後來你使用getActionCommand()方法來訪問所選擇的複選框的ID。

2

你可以得到displayKeywordPanel.checkBooxPanel.getComponents()數組。遍歷數組並存儲索引。然後使用索引來獲取元素keyMessageList

1

我認爲你可以擴展自己的JCheckBox(比如說說JCheckBoxWithID),它允許你保持id。然後用List一個複選框被選中/未選到每次存儲/刪除IDS使用ItemListener

誰選擇了這種方式,你會避免在你checkboxPanel問迭代,並保持分離職責:

  • JCheckBox只是示出了一個關鍵字和保持一個id
  • List只是選定的商店IDS
  • ItemListener處理該事件時一個JCheckBox變爲選中/未選中的一個和DDS /刪除其ID /從List

希望這個例子是有幫助的:

import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Tests { 

    private void initGUI(){ 

     /* This list will store selected ids */ 
     final List<Integer> selectedIds = new ArrayList<>(); 

     ItemListener itemListener = new ItemListener() { 
      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if(e.getSource() instanceof JCheckBoxWithID){ 
        JCheckBoxWithID checkBoxWithID = (JCheckBoxWithID) e.getSource(); 
        if(checkBoxWithID.isSelected()){ 
         selectedIds.add(checkBoxWithID.getId()); 
        } else { 
         selectedIds.remove(checkBoxWithID.getId()); 
        } 
       } 
      } 
     }; 

     String[] keyWords = new String[]{"Help 1", "Help 2", "Help 3", "Help 4", "Help 5", "Help 6"}; 
     Integer id = 0; 
     JPanel checkBoxesPanel = new JPanel(new GridLayout(3,3)); 

     /* Add many checkbox as you keywords you have */ 
     for(String keyWord : keyWords){ 
      JCheckBoxWithID checkBoxWithID = new JCheckBoxWithID(keyWord, id); 
      checkBoxWithID.addItemListener(itemListener); 
      checkBoxesPanel.add(checkBoxWithID); 
      id++; 
     } 

     JButton submit = new JButton("Submit"); 
     submit.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       JOptionPane.showMessageDialog(null, Arrays.toString(selectedIds.toArray()), "Selected IDs", JOptionPane.INFORMATION_MESSAGE); 
      } 
     }); 

     JPanel content = new JPanel(new FlowLayout(FlowLayout.LEADING)); 
     content.add(checkBoxesPanel); 
     content.add(submit); 

     JFrame frame = new JFrame("Demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setContentPane(content); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

    } 

    public static void main(String[] args) {   
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Tests().initGUI(); 
      } 
     });  
    } 

    class JCheckBoxWithID extends JCheckBox { 
     /* I use Integer but the id could be whatever you want, the concept is the same */ 
     private Integer _id; 

     public JCheckBoxWithID(String text, Integer id) { 
      super(text); 
      _id = id; 
     } 

     public void setId(Integer id){ 
      _id = id; 
     } 

     public Integer getId(){ 
      return _id; 
     } 
    } 
} 

這裏有一個打印屏幕:

enter image description here

+1

真的嗎?爲此擴展'JCheckBox'?您可以使用'putClientProperty'方法在'JComponent'上存儲額外的值。在這種情況下,擴展的附加價值是多少? – Robin

+0

爲什麼不呢?是的,你可以使用'putClientProperty',但它不那麼直觀的恕我直言。考慮閱讀別人的代碼,並試圖找出原始開發者以這種方式放置/閱讀房產的原因。我認爲,如果你讓自己的自定義類應該更容易理解它與超類相關的功能。 – dic19