JRadioButton Applications

To understand various events and applications of JRadioButtons , try to develop these applications. The required functions / events for these applications are explained at JRadioButton tutorial.

You can download the source code of each application at the end of the solution code.

Questions on JRadioButton

  1. Keep one pair of radio buttons r1 and r2. Add text or label saying Enable to r1 and text Disable to r2. Keep one button b1 above the radio buttons. You can enable the button ( b1 ) by selecting radio button r1 and disable the button by selecting the radio button r2. If r1 is selected then r2 is deselected and if r1 is selected then r2 should be deselected.
  2. Keep one pair of radio buttons r1 and r2 in a group. Add two buttons b1 and b2. On Click of b1, r1 radio button will be selected and on Click of button b2, radio button r2 will be selected.
  3. Keep one text box ( t1 ) and one button ( b1) . Add one radio button r1. By default the textbox t1 should display the text or Label of radio button r1. After changing the text of t1 and by pressing the button b1 the text of the radio button r1 should change and reflect the text entered at text box t1.

Solution 1


Radio button enable selection Radio button disable selection

JButton b1 = new JButton("Button b1");
b1.setBounds(36, 71, 89, 23);
contentPane.add(b1);
		
JRadioButton r1 = new JRadioButton("Enable");

r1.setBounds(6, 144, 68, 23);
contentPane.add(r1);
		
JRadioButton r2 = new JRadioButton("Disable");
r2.setBounds(112, 144, 68, 23);
contentPane.add(r2);
		
ButtonGroup my_group = new ButtonGroup(); // New group created
my_group.add(r1);
my_group.add(r2);
		
r1.addActionListener(new ActionListener() {  
	  public void actionPerformed(ActionEvent e) 
        {
		  b1.setEnabled(true);
        }
});
r2.addActionListener(new ActionListener() {  
	  public void actionPerformed(ActionEvent e) 
        {
		  b1.setEnabled(false);
        }
});
Full code is here ( swing-radiobutton-sol1.txt)

Solution 2

Selecting Radio buttons
JRadioButton r1 = new JRadioButton("r1 ");
	r1.setBounds(57, 172, 58, 23);
	contentPane.add(r1);
		
	JRadioButton r2 = new JRadioButton("r2");
	r2.setBounds(169, 172, 111, 23);
	contentPane.add(r2);
		
	ButtonGroup my_group = new ButtonGroup(); 
	my_group.add(r1);
	my_group.add(r2);
		
	JButton b1 = new JButton("Select r1");
	b1.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			r1.setSelected(true); // r1 will be selected
		}
	});
	b1.setBounds(26, 39, 89, 23);
	contentPane.add(b1);
		
	JButton b2 = new JButton("Select r2");
	b2.setBounds(147, 39, 89, 23);
	contentPane.add(b2);
		
	b2.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			r2.setSelected(true); // r1 will be selected
		}
	});
	}
Full code is here ( swing-radiobutton-sol2.txt)

Solution 3

Managing Label of JRadioButton
To update the default label of JRadioButton r1 in text box t1
t1.setText(r1.getText());
Code to update the label of JRadioButton r1
b1.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			r1.setText(t1.getText()); // update text or r1
		}
	});
Full code is here ( swing-radiobutton-sol3.txt)

Button with Textfield and Lable

swing


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer