Skip to content

#08.5 RadioButton

Valkryst edited this page Jul 18, 2018 · 1 revision
import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.builder.RadioButtonBuilder;
import com.valkryst.VTerminal.component.RadioButtonGroup;

import java.io.IOException;

public class Driver {
    public static void main(final String[] args) throws IOException {
        final Screen screen = new Screen();
        screen.addCanvasToFrame();

        final RadioButtonBuilder radioButtonBuilder = new RadioButtonBuilder();

        // Create the first group of RadioButtons:
        final RadioButtonGroup groupA = new RadioButtonGroup();

        radioButtonBuilder.setPosition(10, 10);
        radioButtonBuilder.setGroup(groupA);
        radioButtonBuilder.setText("Group A, Option #1");
        screen.addComponent(radioButtonBuilder.build());

        radioButtonBuilder.setPosition(10, 11);
        radioButtonBuilder.setText("Group A, Option #2");
        screen.addComponent(radioButtonBuilder.build());


        // Create the second group of Radio Buttons:
        final RadioButtonGroup groupB = new RadioButtonGroup();

        radioButtonBuilder.setPosition(10, 13);
        radioButtonBuilder.setGroup(groupB);
        radioButtonBuilder.setText("Group B, Option #1");
        screen.addComponent(radioButtonBuilder.build());

        radioButtonBuilder.setPosition(10, 14);
        radioButtonBuilder.setText("Group B, Option #2");
        screen.addComponent(radioButtonBuilder.build());

        screen.draw();
    }
}

Code Explanation

final RadioButtonBuilder radioButtonBuilder = new RadioButtonBuilder();

Constructs a new RadioButtonBuilder. You can view the documentation here.

You can reuse the builder, so you won't need to create a new RadioButtonBuilder every time you want to create a new progress bar.


final RadioButtonGroup groupA = new RadioButtonGroup();

Constructs a new RadioButtonGroup. You can view the documentation here.

Each RadioButtonGroup contains one or more RadioButtons. Only one RadioButton, in a specific group, can be selected at any one time.


radioButtonBuilder.setPosition(10, 10);

This tells the builder to place the first radio button at position (10x, 10y).


radioButtonBuilder.setGroup(groupA);

This tells the builder to add the radio button into the first RadioButtonGroup.


radioButtonBuilder.setText("Group A, Option #1");

This tells the builder to set the text of the radio button to "Group A, Option #1".


panel.addComponent(radioButtonBuilder.build());

This builds the radio button and adds it to the panel.

Result