CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2011
    Posts
    4

    Swing:Save results for checkbox

    Hi,

    I want to save the results user selected for checkboxes.
    I have a question which has 10 choices .User selects more than 1.All the choices are available as checkboxes.
    Do I have to hard code for each choice i.e have a class name choice.java and have set and get method for all 10 choice .or is there a way to make it dynamic.

    Please please help ,I am struggling with it since long.

    Thanks,

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Swing:Save results for checkbox

    If you're doing the same thing n times eg creating 10 Checkboxes then you can nearly always solve the repetition problem with a for-loop and an array or two.

    Not only does this save typing it's good programming practice as if you ever need to fix the code or change the code you only have to do it in 1 place.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2011
    Posts
    4

    Re: Swing:Save results for checkbox

    Could you please explain how to create checkboxes in a loop .I want to save the values in datbase and then fetch the value back.Do I need a variable for each checkbox or I should make a list?

    Please help.

    Thanks

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Swing:Save results for checkbox

    Could you please explain how to create checkboxes in a loop .
    There are various ways, a simple approach is:
    You create an ActionListener to handle the checkboxes changes of state.
    Then you have a loop and in the loop you create a new instance of a checkbox, add the actionListener to it, set it's actionCommand to the loop index, and add it to the display.
    Your ActionListener then needs to query the event source to get it's actionCommand so it knows which checkbox was checked/unchecked and it can then save the new state in a boolean array indexed on the actionCommand value.

    Do I need a variable for each checkbox or I should make a list?
    If you have a fixed number of items use an array, if it's dynamic use a list.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured