Noah Skull Weijian
Noah Skull Weijian

Reputation: 129

JRadioButton and component changes

I created a JDialog box where it has 2 radio button which will change the JLabel whenever I clicked the other button(for example: Monthly salary when I click the full time button and hourly pay when i click the part time button)

So my questions is how do i do that? Do I create ActionListener for the radioButton and create the those JPanel inside the the actionPerformed class?

example

Upvotes: 0

Views: 69

Answers (3)

user2575725
user2575725

Reputation:

Yes, you will need listener, I recommend ItemListener over ActionListener but there is no need to create Panel in listener. Instead change the label text itself.

Upvotes: 1

Kevin Mee
Kevin Mee

Reputation: 549

I think the best way to go about this is create an action listener for the button. When one is selected change the text with monthLabel.setText("Monthly Salary");

Upvotes: 1

CMPS
CMPS

Reputation: 7769

Here's how you can do it:

radio1.addActionListener(new ActionListener(){
   label.setText("Clicked from radio 1");
});

radio2.addActionListener(new ActionListener(){
   label.setText("Clicked from radio 2");
});

Upvotes: 0

Related Questions