Table of contents
1.
Introduction
2.
Classification of Radio Buttons
3.
1️⃣ State
3.1.
📍Read Only State
3.2.
⭐ Implementation for Read Only State 
3.3.
Output
3.4.
📍 Disabled
3.5.
⭐ Implementation for Disabled State
3.6.
Output
4.
2️⃣ Orientation
4.1.
📍Horizontal
4.2.
⭐ Implementation of Horizontal Orientation
4.3.
Output
4.4.
📍Vertical
4.5.
⭐ Implementation of Vertical Orientation
4.6.
Output
5.
3️⃣ Types of Options
5.1.
📍Custom Options
5.2.
⭐ Implementation of Custom Options
5.3.
📍Default Option
6.
Some points to remember
7.
Frequently Asked Questions
7.1.
Is Vaadin utilized widely?
7.2.
Is Vaadin scaleable?
7.3.
Is Vaadin a good framework?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Vaadin-Radio Button

Author Neha Chauhan
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Vaadin}> is a web-development application, which allows users to build a consistently good UI/UX using Java. More than 150 000 developers around the world are using Vaadin. More than 40% of the Fortune 100 companies have been using Vaadin. It can be used for both client and server-side development. 

Radio Button

Vaadin Radio Button is a group component in which there is a list of related options mutually exclusive to each other and the user can select exactly one of the options. In this article, we will discuss the Radio Button group and its components. 

Classification of Radio Buttons

There are different types of Radio Button Groups in Vaadin. We can classify the Radio Buttons based on the-

1️⃣ State

2️⃣ Orientation

3️⃣ Type of Options

Let’s learn more about each option in detail. Let’s Start 🏁

1️⃣ State

In Vaadin, the Radio Button options are mutually exclusive and can take up two states - Read Only State and Disabled State. 

📍Read Only State

If we want to use Radio Button options that are not editable but are still accessible, then we set the Radio Button to Read Only state. 

To set the Radio Button as Read Only, keep the setReadOnly property as true. This will ensure that the buttons are still visible to the user, but they cannot change the state of the button. The options can be selected and copied. 

⭐ Implementation for Read Only State 

package com.vaadin.demo.component.radiobutton;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.router.Route;

@Route("radio-button-readonly")
public class RadioButtonReadonly extends Div {

  public RadioButtonReadonly() {
  
    RadioButtonGroup<String> radioGroup = new RadioButtonGroup<>();
    radioGroup.setLabel("Status");
    radioGroup.setItems("In progress", "Done", "Cancelled");
    radioGroup.setValue("In progress");
    radioGroup.setReadOnly(true);
    add(radioGroup);
  
  }

}
You can also try this code with Online Java Compiler
Run Code

Output

Output

📍 Disabled

If we want the Radio Button options to be inaccessible and uneditable, we can set the Radio button state to Disabled. The options will be inaccessible by the screen reader technologies. 

To avoid layout modifications when an element's visibility changes and then let users know it exists even though it isn't available right now, we can Disable a Radio Button. 

Keeping the setEnabled property as false, the Radio Button options will not be visible and not be editable by the user.

⭐ Implementation for Disabled State

package com.vaadin.demo.component.radiobutton;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.router.Route;

@Route("radio-button-disabled")
public class RadioButtonDisabled extends Div {

  public RadioButtonDisabled() {
    
    RadioButtonGroup<String> radioGroup = new RadioButtonGroup<>();
    radioGroup.setLabel("Status");
    radioGroup.setItems("In progress", "Done", "Cancelled");
    radioGroup.setValue("In progress");
    radioGroup.setEnabled(false);
    add(radioGroup);
    
  }

}
You can also try this code with Online Java Compiler
Run Code

Output

Output

2️⃣ Orientation

The buttons can be put horizontally or vertically.

📍Horizontal

By default, the orientation of the Radio Button is horizontal. It can be helpful if we want to save screen space. It is recommended to have only three options. 

Radio Buttons are, by default, set to horizontal orientation.

⭐ Implementation of Horizontal Orientation

package com.vaadin.demo.component.radiobutton;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.router.Route;

@Route("radio-button-horizontal")
public class RadioButtonHorizontal extends Div {

    public RadioButtonHorizontal() {
        // tag::snippet[]
        RadioButtonGroup<String> radioGroup = new RadioButtonGroup<>();
        radioGroup.setLabel("Status");
        radioGroup.setItems("Pending", "Submitted", "Confirmed");
        radioGroup.setValue("Pending");
        add(radioGroup);
        // end::snippet[]
    }

}
You can also try this code with Online Java Compiler
Run Code

Output

Output

📍Vertical

The vertical orientation of Buttons makes the layout look clean and the options are also more readable.

Keep the addThemeVariants property as LUMO_VERTICAL.

⭐ Implementation of Vertical Orientation

package com.vaadin.demo.component.radiobutton;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.component.radiobutton.RadioGroupVariant;
import com.vaadin.flow.router.Route;

@Route("radio-button-vertical")
public class RadioButtonVertical extends Div {

  public RadioButtonVertical() {
             RadioButtonGroup<String> radioGroup = new RadioButtonGroup<>();
    radioGroup.addThemeVariants(RadioGroupVariant.LUMO_VERTICAL);
    radioGroup.setLabel("Status");
    radioGroup.setItems("Pending", "Submitted", "Confirmed");
    radioGroup.setValue("Pending");
    add(radioGroup);
  }

}
You can also try this code with Online Java Compiler
Run Code

Output

Output

 

3️⃣ Types of Options

📍Custom Options

If we want to give a choice to the user to enter their Custom Option, keep the “other” Radio Button as the last option. This “other” button will be coupled with a text field area. This Text field will remain hidden unless the “other” option is selected.

⭐ Implementation of Custom Options

RadioButtonGroup<Card> radioGroup = new RadioButtonGroup<>();
radioGroup.addThemeVariants(RadioGroupVariant.LUMO_VERTICAL);
radioGroup.setLabel("Payment method");

List<Card> cards = new ArrayList<>(DataService.getCards());
Card other = new Card();
other.setId(-1);
cards.add(other);
radioGroup.setItems(cards);
radioGroup.setValue(cards.get(0));

radioGroup.setRenderer(new ComponentRenderer<>(card -> {
  if (other.equals(card)) {
    return new Text("Other");
  } else {
    Image logo = new Image(card.getPictureUrl(), card.getName());
    logo.setHeight("1em");
    Span number = new Span(new Text(card.getAccountNumber()));
    return new HorizontalLayout(logo, number);
  }
}));

TextField textField = new TextField("Card number");
textField.setVisible(false);
radioGroup.addValueChangeListener(e -> textField.setVisible(other.equals(e.getValue())));

add(radioGroup, textField);
You can also try this code with Online Java Compiler
Run Code

 

📍Default Option

The most commonly selected option is kept as the default option. Usually, the default option is kept at the starting of the list. 

Some points to remember

 

⭐For Radio Button Groups, it is important to provide labels. This will help in clearly distinguishing them from one another, especially with multiple adjacent groups.

⭐ Items can be customized to include more than a single line of text.

⭐ Sometimes two radio buttons can be a better alternative to a single Checkbox.

Frequently Asked Questions

Is Vaadin utilized widely?

One of the most well-liked Java web development frameworks, Vaadin, is frequently used to create robust and scalable web applications.

Is Vaadin scaleable?

Finally, the answer is that Vaadin apps do scale effectively. Errors are possible with any framework, as with anything else. However, it's simple to develop applications with Vaadin that scale well, look amazing, and operate excellently, making your users happy by adhering to traditional Java best practices.

Is Vaadin a good framework?

A reliable web framework for creating sophisticated online applications is Vaadin. With Vaadin, creating web-based GUIs is incredibly easy, fast, and similar to creating desktop applications. There are circumstances, though, in which Vaadin is inappropriate.

Conclusion

Pat yourself on the back for finishing this article. We have discussed what Radio Buttons are in Vaadin and how to use them. We discussed the different states and orientations of the Radio Button. We also discussed the variations of Radio Buttons in Vaadin based on the option types. 

If you are interested in knowing more about Vaadin, we recommend you read some of our articles - 

🔥 Vaadin-Environment Setup

🔥 Vaadin Architecture

🔥 Vaadin-Confirm Dialog

          🔥 Vaadin-Combo Box 

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass