[Java Swing] JRadioButton trong Java – JRadioButton in java

Nội dung
Tạo JRadioButton
Đặt JRadioButton vào nhóm
Bắt sự kiện cho JRadioButton

JRadioButton là một đối tượng cho phép chúng ta chọn lựa các thuộc tính giốn như JCheckBox. Tuy nhiên chúng ta hay sử dụng JRadioButton khi mà muốn người dùng chỉ chọn được một trong các thuộc tính. Ví dụ chọn giới tính thì người dùng chỉ được chọn là Nam hoặc Nữ.

Cách tạo JRadioButton – How to create JRadioButton

Để tạo được JRadioButton chúng ta dùng một trong các hàm khởi tạo sau:
– JRadioButton(): Tạo JRadioButton không có text, không được chọn trước.
– JRadioButton(Action a): Tạo JRadioButton với sự kiện a
– JRadioButton(Icon icon): Tạo JRadioButton với icon
– JRadioButton(Icon icon, boolean selected): Tạo JRadioButton với icon và đặt là được chọn hay không
– JRadioButton(String text): Tạo JRadioButton với text
– JRadioButton(String text, boolean selected): Tạo JRadioButton với text và đặt là được chọn hay không
– JRadioButton(String text, Icon icon): Tạo JRadioButton với text và icon
– JRadioButton(String text, Icon icon, boolean selected): Tạo JRadioButton với text, icon và đặt lựa chọn.

Bây giờ chúng ta sẽ thực hiện tạo JFrame gồm 2 JRadioButton là Boy và Girl (Boy được chọn mặc định) cùng với 1 JLabel để hiển thực hiện hiển thị kết quả khi chọn JRadioButton.

create JRadioButton

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package nguyenvanquan7826.JRadioButton;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
 
public class MyRadioButton extends JFrame {
    private JRadioButton radBoy, radGirl;
    private JLabel lbGender;
 
    public MyRadioButton() {
        // add main panel
        add(createMainPanel());
        // set display
        setTitle("JRadioButton");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200, 100);
        setLocationRelativeTo(null);
        setVisible(true);
    }
 
    // create main panel
    private JPanel createMainPanel() {
        JPanel panel = new JPanel();
        // add two radiobutton and a label to frame
        panel.add(radBoy = createRadioButton("Boy", true));
        panel.add(radGirl = createRadioButton("Girl", false));
        panel.add(lbGender = createLabel("You are a boy"));
        return panel;
    }
 
    // create a JRadioButton with name
    private JRadioButton createRadioButton(String name, boolean select) {
        JRadioButton rad = new JRadioButton(name, select);
        return rad;
    }
 
    // create a JLabel with text
    private JLabel createLabel(String text) {
        JLabel lb = new JLabel(text);
        return lb;
    }
 
    public static void main(String[] args) {
        new MyRadioButton();
    }
}

Vậy là đã tạo được JRadioButton, tuy nhiên khi bạn chọn giới tính thì không có hiện tượng gì xảy ra với JLabel cả, thậm chí là cùng một lúc có thể chọn cả 2 JRadioButton. Vậy làm sao để chỉ chọn được một JRadioButton? làm sao để bắt sự kiện cho JRadioButton?

Đặt JRadioButton vào nhóm – Set group for JRadioButton

Để làm được việc mà chỉ cho phép người dùng chọn một trong các JRadioButton chúng ta cần đặt các JRadioButton vào một nhóm – ButtonGroup. Tức là chúng ta cần tạo ra một ButtonGroup và đặt chúng vào trong ButtonGroup đó. Bạn sửa lại code của phương thức createMainPanel() như sau:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
private JPanel createMainPanel() {
    JPanel panel = new JPanel();
    // add two radiobutton and a label to frame
    panel.add(radBoy = createRadioButton("Boy", true));
    panel.add(radGirl = createRadioButton("Girl", false));
    panel.add(lbGender = createLabel("You are a boy"));
 
    // create ButtonGroup for radBoy and radGirl
    ButtonGroup genderGroup = new ButtonGroup();
    // add radBoy and radGirl into Group
    genderGroup.add(radBoy);
    genderGroup.add(radGirl);
 
    return panel;
}

Vậy là giờ chúng ta chỉ được chọn Boy hoặc Girl.

Bắt sự kiện cho JRadioButton – Action on JRadioButton

Tiếp theo sẽ là bắt sự kiện cho JRadioButton, nó tương tự như bắt sự kiện cho JCheckBox. Chúng ta cần implements giao diện ItemListener để thực hiện addItemListener cho các JRadioButton. Tiếp theo là viết phương thức itemStateChanged để bắt sự kiện. Kiểm tra một JRadioButton được chọn hay không bằng phương thức isSelected().
Chúng ta sửa lại phương thức createRadioButton như sau:

1
2
3
4
5
private JRadioButton createRadioButton(String name, boolean select) {
    JRadioButton rad = new JRadioButton(name, select);
    rad.addItemListener(this);
    return rad;
}

Tiếp theo là bắt sự kiện trong phương thức itemStateChanged

1
2
3
4
5
6
7
8
private void changeSelect() {
    lbGender.setText("You are a " + (radBoy.isSelected() ? "boy" : "girl"));
}
 
@Override
public void itemStateChanged(ItemEvent e) {
    changeSelect();
}

Code hoàn chỉnh của chúng ta như sau:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package nguyenvanquan7826.JRadioButton;
 
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
 
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
 
public class MyRadioButton extends JFrame implements ItemListener {
    private JRadioButton radBoy, radGirl;
    private JLabel lbGender;
 
    public MyRadioButton() {
        // add main panel
        add(createMainPanel());
        // set display
        setTitle("JRadioButton");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200, 100);
        setLocationRelativeTo(null);
        setVisible(true);
    }
 
    // create main panel
    private JPanel createMainPanel() {
        JPanel panel = new JPanel();
        // add two radiobutton and a label to frame
        panel.add(radBoy = createRadioButton("Boy", true));
        panel.add(radGirl = createRadioButton("Girl", false));
        panel.add(lbGender = createLabel("You are a boy"));
 
        // create ButtonGroup for radBoy and radGirl
        ButtonGroup genderGroup = new ButtonGroup();
        // add radBoy and radGirl into Group
        genderGroup.add(radBoy);
        genderGroup.add(radGirl);
 
        return panel;
    }
 
    // create a JRadioButton with name
    private JRadioButton createRadioButton(String name, boolean select) {
        JRadioButton rad = new JRadioButton(name, select);
        rad.addItemListener(this);
        return rad;
    }
 
    // create a JLabel with text
    private JLabel createLabel(String text) {
        JLabel lb = new JLabel(text);
        return lb;
    }
 
    private void changeSelect() {
        lbGender.setText("You are a " + (radBoy.isSelected() ? "boy" : "girl"));
    }
 
    @Override
    public void itemStateChanged(ItemEvent e) {
        changeSelect();
    }
 
    public static void main(String[] args) {
        new MyRadioButton();
    }
 
}

Đọc thêm: class JRadioButton, use JRadioButton