3

So basically, I'm trying to make a simple program here with GUI that let's you make a choice given two jRadioButtons as choices of which either of the two leads to a corresponding storyline in similar fashion to visual novels. The problem however is that I'm having trouble connecting the idea of a jButton to a choice from a jRadioButton.

The concept is like this: In a frame, there's a jTextArea which displays strings of texts forming a storyline which gives two options to choose from here and there as represented by two jRadioButtons which then must be executed by a jButton.

So far, the only logic I've placed inside the ActionListener for both jRadioButtons is having to disable the other once a jRadioButton is clicked while the jButton is intentionally blank. Anyone got any similar program or module he / she would like to share, at least, the logic on this one programatically speaking?

Robin
  • 36,233
  • 5
  • 47
  • 99
dothackjhe
  • 29
  • 1
  • 2
  • 5
  • 1
    See the [radiobutton tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton). Put them in a `ButtonGroup` and you will have the selection by default – Robin Sep 30 '12 at 12:17
  • How should the jButton behave by then as a button that OK's the action? – dothackjhe Sep 30 '12 at 12:27
  • You can see which radio button is selected through the radiobutton API. Not sure I fully understand the problem here – Robin Sep 30 '12 at 12:29
  • I need a sample program that shows: Given two choices, choose one. Choose one by using a jRadioButton given two of those which will be executed once a jRadioButton is triggered as well as the jButton clicked. – dothackjhe Sep 30 '12 at 12:49
  • _need a sample program that shows_ read the corresponding chapter in the tutorial referenced in the swing tag wiki - it has sample code for all the basic use cases for all Swing components – kleopatra Sep 30 '12 at 13:41

1 Answers1

5

We usually use RadioButtons to choose between one of a few options and only one button can be selected at a time. To get this functionality, you need to add your buttons to a javax.swing.ButtonGroup . Here is a quick example:

 //Fields declared in your main GUI class
 JRadioButton option1,option2;
 JButton goButton; //executes the "Story"

 //Constructor code or place in init() method that constructor calls:
 {
    //initialize buttons and place them in your frame.
    ButtonGroup buttonGroup = new javax.swing.ButtonGroup();
    buttonGroup.add(option1);
    buttonGroup.add(option2);
    buttonGroup1.setSelected(option1.getModel(), true);
    goButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            goButtonActionPerformed(evt);
        }
    });

}

private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
     if(option1.isSelected()) {                
         //place text in your text area
     }
     if(option2.isSelected()) {                
         //place different text in your text area
     }
}                                            
Thorn
  • 4,015
  • 4
  • 23
  • 42