如何从组按钮中检索jbuttons文本

时间:2013-04-03 15:04:13

标签: java swing prepared-statement jbutton buttongroup

我正在尝试获取所选按钮的文本名称并将其发送到我的数据库。 buttonGroup是否有方法传递所选按钮名称的文本?因为我想将所选按钮名称插入交易表

 try {
      conn = getConnection(); 
      String Transaction = "INSERT  INTO Transaction"
                + "(Currency_type, currency_Amount,Local_Amount,Rate,Tran_type) VALUES"
                + "(?,?,?,?)";

      pst = conn.prepareStatement(Transaction); // create a statement
      pst.setObject(1,DisplayL.getText()); // set input parameter 1
      pst.setObject(2,txt_select.getText()); // set input parameter 2
      pst.setObject(3,txt_amount.getText()); // set input parameter 1
      pst.setObject(4,txt_rate.getText()); // set input parameter 2
      pst.setObject(5,buttonGroup2); // set input parameter 2

      pst.execute();// execute update statement
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
            try {
                pst.close();
            } catch (SQLException ex) {
                Logger.getLogger(connectnm.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(connectnm.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

预期输出

交易

Tran_ID  |Currency_type | currency_Amount|  Local_Amount|   Rate  |Tran_type|
     4   | Euro         |  1200.00       |    1000.00   |   1.120 |**Sell** |

1 个答案:

答案 0 :(得分:1)

 ButtonModel btnModel = buttonGroup2.getSelection();
if(btnModel !=null)
{
 btnModel.getActionCommand()
}