2015-07-22 91 views
0

我有一個JTable填充了關於students (student id, name...)的數據,當我從表中選擇一行時,表單打開並且其字段需要填充相同的值(例如,如果英勇強尼從表中選擇。如何根據從數據庫中提取的值設置單選按鈕

然後他的名字應該在文本提交的名稱顯示在表格上,我不喜歡這樣txtfieldName.setText(student.getName)

我的問題是我怎麼自動設置爲我Radio button (我的單選按鈕是男性或女性),當我點擊該字段時

enter code here 

tableGuest.addMouseListener(new MouseAdapter() { 
public void mouseClicked(MouseEvent e) { 
    try{ 
     int row = tableGuest.getSelectedRow(); 
     String guestEdit=(tableGuest.getModel().getValueAt(row,  0).toString()); 


     String query = "Select guest_id,guest_fname,guest_lname,guest_contact,guest_age,guest_gender,guest_address,guest_email from guest_tbl where guest_id= '"+guestEdit+"'"; 

    PreparedStatement pst = con.prepareStatement(query); 
    ResultSet rs = pst.executeQuery(); 

    buttonGroupEdit.add(rdbtnMaleEdit); 
    buttonGroupEdit.add(rdbtnFemaleEdit); 


       while(rs.next()) 
       { 
        String genderEdit=rs.getString("guest_gender"); 

        if(genderEdit.equals("Male")) 
        { 
         rdbtnMaleEdit.setSelected(true); 
        } 
        else if(genderEdit.equals("Female")) 
        { 
         rdbtnFemaleEdit.setSelected(true); 
        } 
        else 
        { 
        JOptionPane.showMessageDialog(null, "error !"); 
        } 


        tfEditFname.setText(rs.getString("guest_fname")); 
        tfEditLname.setText(rs.getString("guest_lname")); 
        tfEditEmail.setText(rs.getString("guest_email")); 

        tfEditContact.setText(rs.getString("guest_contact")) 
       } 
       pst.close(); 


      }catch(Exception ex){ 
       ex.printStackTrace(); 
      } 

     } 
    }); 
+0

到目前爲止您嘗試過的是什麼? – Burusothman

+0

您需要評估'sex'到'boolean'('true' /'false')的值,該值可以通過'isSelected'設置爲'JRadioButton'。您可能還想看看[如何使用ButtonGroup組件](https://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html) – MadProgrammer

+0

關於學生結構的一些想法'類將有幫助 – MadProgrammer

回答

0
String gender = "male" 
    // this comes from db - since we don't know the structure this is a plain guess. 

    if (gender.equals("male") { 
     rbtMale.setSelected(true); 
    } else { 
     rbtFemale.setSelected(true); 
    } 

而像MadProgrammer說,你將需要一個ButtonGroupadd所有相關的按鈕它。

private final ButtonGroup genderButtons = new ButtonGroup(); 
genderButtons.add(rbtMale); 
genderButtons.add(rbtFemale); 
+0

謝謝你的幫助!但我需要根據數據庫 –

+0

自動設置我的單選按鈕,謝謝你,先生!我明白了,感謝你的代碼:D –

0

我已經同我的工作還挺解決方案我生成與單選框數據庫值並顯示在對話框中的java他們一起工作。

我們必須從存儲在列表類似下面數據庫中的值:

列表標題; //這是一個包含數據庫的列表值

第一計數列表中的元素的值:

INT list_count = Titles.size();

現在用收音機功能繼續進行,首先我們需要列表元素轉換成數組象下面這樣:

String[] col = new String[list_count]; //created an array with limit of list count values 
for(int i=0; i < list_count; i++){ 
col[i]=Titles.get(i).toString(); // add values of list into array with loop 
} 

下面是我們在上面創建了創建與數據庫陣列單選按鈕的功能:

public String get_key(int list_count, String[] col){ 

    JRadioButton jb[] = new JRadioButton[col.length]; //Create Radion button array 

    ButtonGroup rb = new ButtonGroup(); //Group Radio Button 

    JPanel panel = new JPanel(new GridLayout(0, 1)); //Set layout of radion button to display each after other 

    JScrollPane sp = new JScrollPane(panel); // Create a scrollpane to put all these radio button on that 

    GridBagLayout gridbag = new GridBagLayout(); //Layout for scrollpane 

sp.setViewportBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); //bordre for scrollpane 

    List<Component> q = new ArrayList<>(); // q is a component to store and display each radio button 

    JLabel h1=new JLabel("Select a value"); //put a heading on top of jpanel before radio button 

    h1.setFont(new Font("Serif", Font.BOLD, 18)); // set heading text 

    panel.add(h1); //add heading on top of jpanel 

    panel.setBorder(new EmptyBorder(10, 10, 10, 10)); //set panel border to padding each radio button 

    for(int i=0; i < list_count; i++){ 

    jb[i]=new JRadioButton(col[i]); //create radion button dynamacially "col[i]" is the value of each radio 

    rb.add(jb[i]); //it is important also to put all radio in a group we created so only one element should be selected 

    panel.add(jb[i]); // add all radio on jpanel 

     } 

sp.setPreferredSize(new Dimension(350, 300)); //set size of scrollpane 

int act=JOptionPane.showConfirmDialog(null, sp, "Select Primary Key",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE); //add and show scrollpane in dialog 

} 

調用帶有參數值這個功能,我們創建了第一個「list_count」 &「關口」:

get_key(list_count, col); 
相關問題