【Java】JRadioButtonを水平方向に整列させる方法をわかりやすく解説
JRadioButtonはJToggleButtonのサブクラスであり、「選択(selected)」または「非選択(deselected)」の2つの状態を持つボタンです。チェックボックスと異なり、ラジオボタンはグループに関連付けられており、同じグループ内で選択できるのは1つのラジオボタンだけです。このグループ化はButtonGroupクラスを使用して実装できます。グループ内のあるラジオボタンが選択されると、それまで選択されていた他のラジオボタンは自動的に選択解除されます。
ラジオボタンの配置は、BoxLayoutやBoxクラスを利用することで、水平方向にも垂直方向にも自由に整列させることができます。本記事では、Box.createHorizontalBox()メソッドを使って、複数のJRadioButtonを横一列に並べる方法をサンプルコードとともに紹介します。
水平配置のポイント
Box.createHorizontalBox()で水平方向のコンテナを作成する- 作成したBoxに
add()メソッドでラジオボタンを順番に追加する - 複数行に分けたい場合は、行ごとにBoxを作成し、親パネルのレイアウト(例:
GridLayout)で縦に積み重ねる
サンプルコード
import java.awt.*;
import javax.swing.*;
public class HorizontalRadioButtonsTest extends JPanel {
public HorizontalRadioButtonsTest() {
// 1行目のラジオボタン
JRadioButton jrb1 = new JRadioButton(" RB1");
JRadioButton jrb2 = new JRadioButton(" RB2");
JRadioButton jrb3 = new JRadioButton(" RB3");
JRadioButton jrb4 = new JRadioButton(" RB4");
JRadioButton jrb5 = new JRadioButton(" RB5");
Box box1 = Box.createHorizontalBox();
box1.add(jrb1);
box1.add(jrb2);
box1.add(jrb3);
box1.add(jrb4);
box1.add(jrb5);
// 2行目のラジオボタン
JRadioButton jrb6 = new JRadioButton(" RB6");
JRadioButton jrb7 = new JRadioButton(" RB7");
JRadioButton jrb8 = new JRadioButton(" RB8");
JRadioButton jrb9 = new JRadioButton(" RB9");
JRadioButton jrb10 = new JRadioButton(" RB10");
Box box2 = Box.createHorizontalBox();
box2.add(jrb6);
box2.add(jrb7);
box2.add(jrb8);
box2.add(jrb9);
box2.add(jrb10);
// 3行目のラジオボタン
JRadioButton jrb11 = new JRadioButton(" RB11");
JRadioButton jrb12 = new JRadioButton(" RB12");
JRadioButton jrb13 = new JRadioButton(" RB13");
JRadioButton jrb14 = new JRadioButton(" RB14");
JRadioButton jrb15 = new JRadioButton(" RB15");
Box box3 = Box.createHorizontalBox();
box3.add(jrb11);
box3.add(jrb12);
box3.add(jrb13);
box3.add(jrb14);
box3.add(jrb15);
// 4行目のラジオボタン
JRadioButton jrb16 = new JRadioButton(" RB16");
JRadioButton jrb17 = new JRadioButton(" RB17");
JRadioButton jrb18 = new JRadioButton(" RB18");
JRadioButton jrb19 = new JRadioButton(" RB19");
JRadioButton jrb20 = new JRadioButton(" RB20");
Box box4 = Box.createHorizontalBox();
box4.add(jrb16);
box4.add(jrb17);
box4.add(jrb18);
box4.add(jrb19);
box4.add(jrb20);
// 各行のBoxをGridLayoutで縦に配置
setLayout(new GridLayout(5, 1));
add(box1);
add(box2);
add(box3);
add(box4);
}
public static void main(String[] args) {
JFrame frame = new JFrame("HorizontalRadioButtons Test");
frame.add(new HorizontalRadioButtonsTest());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(375, 250);
frame.setVisible(true);
}
}
実行結果

補足:ButtonGroupで排他制御を行う
上記のコードでは見た目の配置のみを行っていますが、実際のアプリケーションではラジオボタンに「1つだけ選択できる」という排他制御を持たせるのが一般的です。その場合は、以下のようにButtonGroupを作成し、すべてのラジオボタンを追加してください。
ButtonGroup group = new ButtonGroup(); group.add(jrb1); group.add(jrb2); group.add(jrb3); // ... 必要な数だけ追加
ButtonGroupに追加されたラジオボタンは、同一グループ内で同時に1つしか選択できなくなり、ユーザーにとって直感的な操作性を実現できます。
-
Java SwingのJButtonでHTMLテキストを実装する方法
JButtonはAbstractButtonのサブクラスであり、Java Swingのコンポーネント階層において非常に重要な役割を担うクラスです。JButtonは、ログイン画面を持つアプリケーションなど、ユーザーに操作を促す場面で広く利用されています。ボタンを押下・クリックすると、ActionListenerインターフェースを通じてアクションイベントが発生し、それに応じた処理を実行できます。また、JButtonにはテキストのみ、アイコンのみ、あるいはテキストとアイコンの両方を設定することが可能です。さらに便利な点として、ボタンのラベルにHTMLタグを使用できるため、太字(bold)や斜体(it
-
JavaでJPanelのpaintComponent()メソッドを実装する方法をわかりやすく解説
JPanelはJavaにおける軽量コンテナであり、それ自体は不可視(インビジブル)なコンポーネントとして扱われます。デフォルトのレイアウトにはFlowLayoutが採用されています。JPanelを生成した後は、Containerクラスから継承されたadd()メソッドを呼び出すことで、ボタンやラベルなどの他のコンポーネントをJPanelオブジェクトに自由に追加できます。 paintComponent()メソッドとは このメソッドは、単なる背景色の描画にとどまらず、JPanel上に独自の図形や画像などを描画したい場合に必要となります。paintComponent()メソッドはすでにJPanelクラ