2013-04-02 62 views
0

我有GUI屏幕,可让您设置一个选择联系人通过RadioButton正在取得的隐私。虽然我可以像这样将选择添加到数据库...编辑单选按钮在Java中

private void addContactButtonActionPerformed(java.awt.event.ActionEvent evt) {             
     try { 
      ContactDAO cDao = new ContactDAO(); 
      final ContactDTO cdto = new ContactDTO(); 


String privacy = ""; 
      String alumni = ""; 
      if (all.isSelected()) { 
       privacy = all.getText(); 
      } 
      if (bio.isSelected()) { 
       privacy = bio.getText(); 
      } 
      if (none.isSelected()) { 
       privacy = none.getText(); 
      } 
      if (yes.isSelected()) { 
       alumni = yes.getText(); 
      } 
      if (no.isSelected()) { 
       alumni = no.getText(); 
      } 
      cdto.setAlumni(alumni); 
      cdto.setStatus(privacy); 
      cDao.add(cdto); 

} 

我被困在检索编辑模式的以前选择的项目。每个单选按钮选项都属于一个按钮组。

private void editContact() { 
    txtID1.setText(String.valueOf(cDTO.getID())); 
    txtTitle1.setText(cDTO.getTitle()); 
    txtFn1.setText(cDTO.getForename()); 
    txtSn1.setText(cDTO.getSurname()); 
    //get status from cDTO.getStaus and adjust appropriately to the radio button 
} 

在上面的方法我想设置单选按钮的选定项目。就像为JComboBox做getSelectedItem()一样,我试图为单选按钮实现相同的功能。 note cDTO包含数据字符串cDTO.getStatus,它从数据库获取值。但我怎么把它设置为我有3个单选按钮,命名为allButtonbioButtonnoneButton

+1

更快地发布[SSCCE](http://sscce.org/),简短,可运行,可编译,只是关于问题 – mKorbel

回答

1

假设cDTO.getStatus()返回匹配的单选按钮的名称String:在ButtonGroupb对于每一个按钮,做这样的事情:

b.setSelected(cDTO.getStatus().equals(b.getText()));