4 Replies - 831 Views - Last Post: 01 March 2013 - 07:03 PM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Change Panels with Radio Buttons

Posted 28 February 2013 - 09:01 PM

Hi everyone! I am doing a project right now and for part of it, I need to change what JPanel is visible when I click on a different radio button. I am having a lot of trouble figuring out how to do this. Do I need to use te Card Layout? If somebody could provide a pseudocode for me, it would be really, really awesome! Thank you so much in advance! Just to clear it up, when I have one radio button pressed, it needs to show one panel, and when you click the other one, it should show a different panel.

Is This A Good Question/Topic? 0
  • +

Replies To: Change Panels with Radio Buttons

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Change Panels with Radio Buttons

Posted 28 February 2013 - 09:42 PM

Yes, you need a CardLayout
Was This Post Helpful? 0
  • +
  • -

#3 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Change Panels with Radio Buttons

Posted 28 February 2013 - 09:52 PM

Do you think you could provide me with a simple pseudocode because I am having trouble implementing it into my code. I have been trying to do it separate from my project program in a more simple manner, but I am still having trouble. Here is the code that I have so far in my example code.

import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JRadioButton;

import javax.swing.*;

public class RadioButton implements ItemListener {
	
	JPanel cards;
	
	
	public void addComponentToPane(Container pane) {
		JPanel radioButton = new JPanel();
		JRadioButton rb1 = new JRadioButton("One", true);
		JRadioButton rb2 = new JRadioButton("Two", false);
		rb1.addItemListener(this);
		rb2.addItemListener(this);
		radioButton.add(rb1);
		radioButton.add(rb2);
		
		JPanel card1 = new JPanel();
		card1.add(new JButton("Button 1"));
		card1.add(new JButton("Button 2"));
		card1.add(new JButton("Button 3"));
		
		JPanel card2 = new JPanel();
		card2.add(new JTextField("TextField", 20));
		
		cards = new JPanel(new CardLayout());
		cards.add(card1);
		cards.add(card2);
		
		pane.add(radioButton, BorderLayout.PAGE_START);
		pane.add(cards, BorderLayout.CENTER);
	}

	public void itemStateChanged(ItemEvent e) {
		CardLayout cl = (CardLayout)(cards.getLayout());
		cl.show(cards, (String)e.getItem());
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		RadioButton demo = new RadioButton();
		demo.addComponentToPane(frame.getContentPane());
		
		frame.pack();
		frame.setVisible(true);
	}

}



Can you tell me what's wrong in that code please? It would be much appreciated!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Change Panels with Radio Buttons

Posted 01 March 2013 - 12:41 PM

1) you have to give a name to the panel you add to the JPanel having CardLayout
2) you use that name to select the JPanel to display
3) better to use an ActionListener
4) have the JRadioButton defined in the instance variable so the actionPerformed() can differentiate them

P.S.
you will need a ButtonGroup if you do not want the 2 JButton "ON" at the same time

import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JRadioButton;

import javax.swing.*;

public class RadioButton implements ActionListener {
	
	JPanel cards;
	JRadioButton rb1 = new JRadioButton("One", true);
	JRadioButton rb2 = new JRadioButton("Two", false);
	
	
	public void addComponentToPane(Container pane) {
		JPanel radioButton = new JPanel();
		rb1.addActionListener(this);
		rb2.addActionListener(this);
		radioButton.add(rb1);
		radioButton.add(rb2);
		
		JPanel card1 = new JPanel();
		card1.add(new JButton("Button 1"));
		card1.add(new JButton("Button 2"));
		card1.add(new JButton("Button 3"));
		
		JPanel card2 = new JPanel();
		card2.add(new JTextField("TextField", 20));
		
		cards = new JPanel(new CardLayout());
		cards.add(card1, "MyPanel1");
		cards.add(card2, "MyPanel2");
		
		pane.add(radioButton, BorderLayout.PAGE_START);
		pane.add(cards, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent e) {
		CardLayout cl = (CardLayout)(cards.getLayout());
		if(e.getSource() == rb1)
		    cl.show(cards, "MyPanel1");
		else
			cl.show(cards, "MyPanel2");
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		RadioButton demo = new RadioButton();
		demo.addComponentToPane(frame.getContentPane());
		
		frame.pack();
		frame.setVisible(true);
	}


}


Was This Post Helpful? 1
  • +
  • -

#5 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Change Panels with Radio Buttons

Posted 01 March 2013 - 07:03 PM

Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1