Page 1 of 1

Swing Tutorial Part III Learning Swing Rate Topic: -----

#1 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Post icon  Posted 25 September 2009 - 05:33 PM

Remember, Look at the link click here for a list of event and listener class with description.


JRadio Button

Radio button allows user to select only one radio button in each group at a time. If a radio button is selected, whatever the previous radio button is, it will be deselected. To work with radio buttons, we need two classes. First, we create the radio buttons with the JRadioButton class, whose constructors and methods are at the link below
"Swing Method & Constructor Table ." Then we create a group for the buttons with the ButtonGroup class. We must add the radio button themselves to a panel ( so they are displayed ) and to a button group. Here is the usual way of creating a radio button.

JRadioButton pepsi, coke, tea;

Then in the frame constructor, we call the JRadioButton constructor to create the radio button:

pepsi = new JRadioButton("Pepsi");

Now we would add the radio button to the panel the usual way. Finally, to create a button group to group radio buttons that work together, we would just call the ButtonGroup class constructor.

ButtonGroup group = new ButtonGroup();

Then add the method of the ButtonGroup to add each radio button to the group

group.add(pepsi);
group.add(coke);
group.add(tea);

Lets observe an example


/*
	The program demonstrate the use of JRadioButton

*
*/

import javax.swing.*;

public class RadioButton extends JFrame
{
	JRadioButton pepsi, coke, tea;

	JPanel panel = new JPanel();

	public RadioButton()
	{	
		// Sets the title

		setTitle("Radio Button");

		// Sets the window size

		setSize(300, 400);

		// Sets the action when the window close

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create a button group to group the radio buttons together

		ButtonGroup group = new ButtonGroup();

		// Creates a radio button for pepsi

		pepsi = new JRadioButton("Pepsi");

		// Creates a radio button for coke

		coke = new JRadioButton("Coke");

		// Creates a radio button for Tea

		tea = new JRadioButton("Tea");

		// Add the radio button to the group

		group.add(pepsi);
		group.add(coke);
		group.add(tea);

		// Adds the button to the panel

		panel.add(pepsi);
		panel.add(coke);
		panel.add(tea);

		// Adds the panel to the content pane

		add(panel);

		// Sets the window to be visible

		setVisible(true);

	}

	public static void main(String[] args)
	{
		new RadioButton();
	}
} 




Check Box

A check box is a control that the user can click to either check or uncheck. Check box is similar to radio buttons, and it's usually used to let the user specify Yes or No to an option. To create a check box, we use the JCheckBox class, then adds it to the panel.

JCheckBox ice_cream, banana, ribs;

The above statement declares the class variable to reference to the check box component. Then we can use the constructor from JCheckBox to create the check boxes and add them to the panel.

ice_cream = new JCheckBox("Ice Cream");

JPanel panel = new JPanel();
panel.add(ice_cream);

Notice that the above statements, we didn't specify which box is initially checked, so as a result they're initially unchecked. If we want to create a check box that is initially checked, we would create a constructor like the following:

ice_Cream = new JCheckBox("Ice Cream", true);

Again, Lets observe an example

/*
	The program demonstrate a check box
	It has an initial box checked

*
*/

import javax.swing.*;

public class CheckBox extends JFrame
{
	JCheckBox ice_cream, banana, ribs;
	JPanel panel;

	public CheckBox()
	{
		// Sets the title of the window

		setTitle("Check Bow Window");

		// Sets the size of the window

		setSize(300, 400);

		// Sets the action when the window closes

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Creates a panel

		panel = new JPanel();

		// Creates 3 check box with ice_cream initially checked

		ice_cream = new JCheckBox("Ice Cream", true);
		banana = new JCheckBox("Banana");
		ribs = new JCheckBox("Ribs");

		// Adds the check boxes to the panel

		panel.add(ice_cream);
		panel.add(banana);
		panel.add(ribs);

		// Adds the panel to the content pane

		add(panel);

		// Sets the window to be visible

		setVisible(true);

	}

	public static void main(String[] args)
	{
		new CheckBox();

	}
}





The example above are all basic example on how to create JCheckBox and JRadioButton, although it isn't much of a use. Hence, after we learn about different layouts we will see more on how to use JCheckBox and JRadioButtons to do more interesting things.



Borders

A border is a decorative element that visually groups components by drawing a line around them. We can apply a border to any object that inherits JComponent. To create a border we can call one of the static methods from the BorderFactory methods which listed in " Swing Methods & Constructor Table. "

To create a border and add to the panel

JPanel panel = new JPanel();
Border b = BorderFactory.createTitledBorder("Title");
panel.setBorder(b );

Hence, any components we add to the panel appear within this border.

Example

/*
	The program demonstrates bevel border

*
*/

import javax.swing.*;
import javax.swing.border.*;

public class BevelBorder extends JFrame
{
	JPanel panel;

	public BevelBorder()
	{
		// Sets the title of the window

		setTitle("Border Window");

		// Sets the size of window

		setSize(300, 400);

		// Sets the action when the window is close

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Creates a panel

		panel = new JPanel();

		// Creates a border name title

		Border b = BorderFactory.createTitledBorder("Title");
		panel.setBorder(b);

		// Adds the panel to the content pane

		add(panel);

		// Sets the window to be visible

		setVisible(true);

		}

	public static void main(String[] args)
	{
		new BevelBorder();
	}
}





Now lets combine JCheckBox, JRadioButton, and BevelBorder together to create a ordering program. It's pretty long so bear with me.


/*
	This is a small Starbucks order program
	It demonstrate the use of BevelBorder, JCheckBox, & JRadioButton

*
*/

import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class Starbucks extends JFrame
{
	JButton ok;
	JPanel radiopanel;
	JPanel checkboxpanel;
	JPanel mainpanel;
	JRadioButton tall, grande, venti;
	JCheckBox ice_green, coffee, orange_juice;

	public Starbucks()
	{
		// Sets title of the window

		setTitle("Welcome to Starbucks");

		// Sets the size of the window

		setSize(300, 400);

		// Sets the action if the window is close

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Creates a button listener

		ButtonListener bl = new ButtonListener();

		// Create two panels one for JRadioButton and one for the main panel

		radiopanel = new JPanel();
		mainpanel = new JPanel();

		// Creates a border for JRadioButton

		Border b = BorderFactory.createTitledBorder("Size");

		// Adds the border to the radio panel

		radiopanel.setBorder(b);

		// Group the radio buttons together

		ButtonGroup radiogroup = new ButtonGroup();

		// Add the radio buttons to the panel and group

		tall = new JRadioButton("Tall");
		radiopanel.add(tall);
		radiogroup.add(tall);

		grande = new JRadioButton("Grande");
		radiopanel.add(grande);
		radiogroup.add(grande);

		venti = new JRadioButton("Venti");
		radiopanel.add(venti);
		radiogroup.add(venti);

		// Adds the JRadioButton panel to the main panel

		mainpanel.add(radiopanel);

		// Creates the JCheckBox panel

		checkboxpanel = new JPanel();

		// Creates a border for the check box panel

		Border b2 = BorderFactory.createTitledBorder("Drinks");

		// Adds the border to the check box

		checkboxpanel.setBorder(b2);

		// Create JCheckbox and add the check boxes into the panel

		ice_green = new JCheckBox("Ice Green Tea");
		checkboxpanel.add(ice_green);

		coffee = new JCheckBox("Coffee");
		checkboxpanel.add(coffee);

		orange_juice = new JCheckBox("Orange Juice");
		checkboxpanel.add(orange_juice);

		// Adds the check box panel to the main panel

		mainpanel.add(checkboxpanel);

		// Creates a ok button

		ok = new JButton("OK");

		/*
			Creates a listener for ok button to listen for any events
			We pass the bl into the parameter, bl is created from the very beginning

		*
		*/

		ok.addActionListener(bl);

		// Adds the ok button into the main panel

		mainpanel.add(ok);

		// Adds the main panel into the content pane

		add(mainpanel);

		// Sets the window to be visible

		setVisible(true);
	}

	/*
		Inside here, all the events of JCheckBox and JRadioButton will be handled

	*
	*/

	public class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==ok)
			{		
					String drinks = "";

				// Determine what is been selected in JCheck Box

				if(ice_green.isSelected())
				{
					drinks += "Ice Green Tea\n";
				}
				if(coffee.isSelected())
				{
					drinks += "Coffee\n";
				}
				if(orange_juice.isSelected())
				{
					drinks += "Orange Juice\n";
				}

				// Determine what is been selected in JRadio Button

				String msg = "You ordered a ";

				if(tall.isSelected())
				{
					msg += " tall " + drinks;

				}
				if(grande.isSelected())
				{
					msg += " grande " + drinks;

				}

				if(venti.isSelected())
				{
					msg += " venti " + drinks;

				}

				// Outputs the order in JOptionPane

				JOptionPane.showMessageDialog(null, msg);

				/*
					Deselected all the check box
					Selects tall in radio button

				*
				*/

				ice_green.setSelected(false);
				coffee.setSelected(false);
				orange_juice.setSelected(false);

				tall.setSelected(true);

			}
		}
	}

	public static void main(String[] args)
	{
		new Starbucks();
	}
}




The above program we created a main panel to hold the JCheckBox and JRadioButton panel. The idea of the above program is to put two panels inside another panel.

Is This A Good Question/Topic? 1
  • +

Replies To: Swing Tutorial Part III

#2 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 04 October 2009 - 10:10 AM

Please!! Do leave some feedbacks so i know what to improve...

is it confusing, not clear enough, needs more examples and so on..

open for any suggestions...
Was This Post Helpful? 1
  • +
  • -

#3 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Posted 07 November 2010 - 06:30 PM

I have one question, on the radiobutton example, if you make a button group you still do this

panel.add(pepsi);

can you do something like this that would add them all now im a beginner so i dont know

panel.add(group);

??
Was This Post Helpful? 0
  • +
  • -

#4 arashzz  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 28-September 09

Posted 19 December 2010 - 07:42 AM

I also have a guestion regarding to the buttongroup .. what exactly is the purpose of binding our buttons to button group, I mean you explained that the reason is to show them on the panel, but since we are adding each button separately to the panel, as I tried by removing the button group the buttons are still there in the panel.

Can you explain alittle bit more please ? thnx in advance
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Guest*


Reputation:

Posted 10 February 2011 - 10:57 PM

View Postarashzz, on 19 December 2010 - 07:42 AM, said:

I also have a guestion regarding to the buttongroup .. what exactly is the purpose of binding our buttons to button group, I mean you explained that the reason is to show them on the panel, but since we are adding each button separately to the panel, as I tried by removing the button group the buttons are still there in the panel.

Can you explain alittle bit more please ? thnx in advance


Ya, I'm a beginner to swing & I also have the same question in my mind too.
Was This Post Helpful? 0

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10658
  • View blog
  • Posts: 39,571
  • Joined: 27-December 08

Posted 10 February 2011 - 11:03 PM

The ButtonGroup ensures that no two JRadioButtons in the group will be selected at the same time. This is behavior typical of grouped radio buttons on forms.
Was This Post Helpful? 2
  • +
  • -

#7 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 04 May 2012 - 10:00 AM

:P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1