Hi, I am new to Java and just learning GUI's. I was wondering if someone could help me with a project? I am trying to create a questionnaire. It will was the user a question and have radio button answers. After the answer is selected, they will hit the "next" button and depending on the answer selected will direct them to a related follow up question with it's own answers. For example: Prompted a question - user selects answer A and hits "next" - because he selected A it would go to class2 with another question - answer. If the customer selected answer B instead - it would of gone to class3...and so fourth. I am familar with making radio buttons, I'm just not sure how to make it determine what class to go to based on the answer selected and hitting the "next" button.

Any help would greatly help me learn!

Thank you in advance!

Dani AI

Generated

A compact, maintainable pattern that fits the example given by is to treat the questionnaire as a small state machine (questions are nodes, answers are transitions) and swap visible screens with CardLayout rather than creating lots of JFrames. As pointed out, handle the decision on the "Next" click; 's ItemListener is useful when reacting immediately to selection changes, but for navigation the Next button keeps the flow simple.

Steps:

  • Give each JRadioButton an action command (like "A" or "B") and add them to a ButtonGroup.
  • Keep a lookup (Map) that says, for question "q1", choice "A" -> next card "q2", choice "B" -> "q3".
  • In the Next button ActionListener get the selected ButtonModel, read getActionCommand(), look up the next card and show it with CardLayout.
  • Store responses in a Map<String,String> if answers are needed later. Check for null selection and missing mappings.

Example snippet (minimal):

// radio buttons for q1
JRadioButton rA = new JRadioButton("Choice A");
rA.setActionCommand("A");
JRadioButton rB = new JRadioButton("Choice B");
rB.setActionCommand("B");
ButtonGroup group = new ButtonGroup();
group.add(rA); group.add(rB);

// flow map for q1 -> q2/q3
Map<String,Map<String,String>> flow = new HashMap<>();
Map<String,String> q1map = new HashMap<>();
q1map.put("A","q2");
q1map.put("B","q3");
flow.put("q1", q1map);

// cards is a JPanel using CardLayout with names "q1","q2","q3"
CardLayout cl = (CardLayout) cards.getLayout();

nextButton.addActionListener(e -> {
    ButtonModel sel = group.getSelection();
    if (sel == null) { JOptionPane.showMessageDialog(frame,"Select an answer"); return; }
    String choice = sel.getActionCommand();
    String next = flow.get("q1").get(choice);
    if (next != null) cl.show(cards, next);
});

Notes: implement each screen as a small JPanel class if it gets complex, register those panels with CardLayout, and avoid creating multiple top-level windows. This approach keeps the flow data-driven and easy to extend (add questions or change branches by editing the map).

Recommended Answers

All 4 Replies

Hi, I am new to Java and just learning GUI's. I was wondering if someone could help me with a project? I am trying to create a questionnaire. It will was the user a question and have radio button answers. After the answer is selected, they will hit the "next" button and depending on the answer selected will direct them to a related follow up question with it's own answers. For example: Prompted a question - user selects answer A and hits "next" - because he selected A it would go to class2 with another question - answer. If the customer selected answer B instead - it would of gone to class3...and so fourth. I am familar with making radio buttons, I'm just not sure how to make it determine what class to go to based on the answer selected and hitting the "next" button.

Any help would greatly help me learn!

Thank you in advance!

Did you attempt writing any code at all?

Add an ActionListener to the "next" button. In the listener you test each radio button to see if it's checked and, if it is, take the appropriate action (eg create a new instance of the appropriate class)
Now write some code...

Did you attempt writing any code at all?

I have only wrote simple radio button code to change a picture. I have began to write this out yet because I wasnt sure how to make it work the way I wanted it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.