You are here: Recent News » Projects » AsWing » AsWing tutorials » Rating system

 

Rating system

Tags : ,

This tutorial will show you how to use the JFrame , JLabel , the BoxLayout and JRadioButton to create a Rating system. Lets create our main class Rating.as which extends JFrame. We will need a JLabel instance to display the message and three JRadioButton instances for different ratings .We will need to create two JPanel instances one for the JLabel which will display our current selection and the other for the radio buttons.

import org.aswing. *;
import org.aswing.utils.Delegate;
import org.aswing.geom.Dimension;
class Rating extends JFrame
{
	private var _radiobtnPanel : JPanel;
	private var _labelPanel : JPanel;
	private var _excellent : JRadioButton;
	private var _great : JRadioButton;
	private var _good : JRadioButton;
	private var _selected : JLabel;
	private var _buttonGroup : ButtonGroup;
	public functiopublic function Rating (Void,)
	{
		super (_root, true);
	}
	public static function main (Void) : Void
	{
		var myWindow : Rating = new Rating ();
		myWindow.setLocation (50, 50);
		myWindow.setSize (200, 150);
		myWindow.setResizable (false);
		myWindow.show ();
	}
}

The ButtonGroup class creates groups of buttons so we can add all our radio buttons to one group instance so that only one radio button can be selected at a given time. We instantiate our radio buttons and pass in the string values for each button which will be the display label for that instance. Wel also instantiate our JPanel instances and the JLabel instance and then add the radio buttons to our button group instance.

public function Rating (Void)
{
	_excellent = new JRadioButton ("Excellent");
	_great = new JRadioButton ("Great");
	_good = new JRadioButton ("Good");
	_selected = new JLabel ("Select Something");
	_radiobtnPanel = new JPanel ();
	_labelPanel = new JPanel ();
	_buttonGroup = new ButtonGroup ();
	_buttonGroup.append (_excellent);
	_buttonGroup.append (_great);
	_buttonGroup.append (_good);
	....................
 

Next we make all our radio buttons align to the right.

_excellent.setHorizontalAlignment(ASWingConstants.LEFT);
_great.setHorizontalAlignment(ASWingConstants.LEFT);
_good.setHorizontalAlignment(ASWingConstants.LEFT);

Now we create a common function that will be called when the user clicks on any of the radiobuttons.

_excellent.addEventListener(JRadioButton.ON_PRESS,Delegate.create(this,showRating));
_great.addEventListener(JRadioButton.ON_PRESS,Delegate.create(this,showRating));
_good.addEventListener(JRadioButton.ON_PRESS,Delegate.create(this,showRating));

In our function we set the message to be displayed by the label instance depending on which radiobutton was clicked.

private function showRating(eventObj:Event):Void{
  var temp:JRadioButton=JRadioButton(eventObj.getSource());
  _selected.setText("You selected " +temp.getText());
}

Let us set the preferred size for the label instance so that the layout manager will use these dimensions.

_selected.setPreferredSize(150,22)

Next we append the radio buttons to the JPanel instances using the BoxLayout layout manager The BoxLayout allows multiple components to be laid out either vertically or horizontally. The components will not wrap so, for example, a vertical arrangement of components will stay vertically arranged when the frame is resized. Also the BoxLayout will give each component the same size in vertical or horizontal line and disregard the components preferred size. The SoftBoxLayout will align components with regard to their preferred size. We use the FlowLayout for the label instance.

_radiobtnPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
_radiobtnPanel.append(_excellent);
_radiobtnPanel.append(_great);
_radiobtnPanel.append(_good);
_labelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
_labelPanel.append(_selected);

Finally we add the JPanel instances to the frame and set the title.

getContentPane().append(_radiobtnPanel,BorderLayout.NORTH);
getContentPane().append(_labelPanel,BorderLayout.CENTER);
setTitle("How do you rate ASWing");

Below is the compiled application.

Rating.as


/**
* @author firdosh
*/
import org.aswing. *;
import org.aswing.utils.Delegate;
class Rating extends JFrame
{
	private var _excellent : JRadioButton;
	private var _great : JRadioButton;
	private var _good : JRadioButton;
	private var _selected : JLabel;
	private var _radiobtnPanel : JPanel;
	private var _labelPanel : JPanel;
	private var _buttonGroup : ButtonGroup;
	public function Rating (Void)
	{
		_excellent = new JRadioButton ("Excellent");
		_great = new JRadioButton ("Great");
		_good = new JRadioButton ("Good");
		_selected = new JLabel ("Select Something");
		_radiobtnPanel = new JPanel ();
		_labelPanel = new JPanel ();
		_buttonGroup = new ButtonGroup ();
		_buttonGroup.append (_excellent);
		_buttonGroup.append (_great);
		_buttonGroup.append (_good);
		_excellent.setHorizontalAlignment (ASWingConstants.LEFT);
		_great.setHorizontalAlignment (ASWingConstants.LEFT);
		_good.setHorizontalAlignment (ASWingConstants.LEFT);
		_excellent.addEventListener (JRadioButton.ON_PRESS, Delegate.create (this, showRating));
		_great.addEventListener (JRadioButton.ON_PRESS, Delegate.create (this, showRating));
		_good.addEventListener (JRadioButton.ON_PRESS, Delegate.create (this, showRating));
		_selected.setPreferredSize (150, 22);
		_radiobtnPanel.setLayout (new BoxLayout (BoxLayout.Y_AXIS));
		_radiobtnPanel.append (_excellent);
		_radiobtnPanel.append (_great);
		_radiobtnPanel.append (_good);
		_labelPanel.setLayout (new FlowLayout (FlowLayout.LEFT));
		_labelPanel.append (_selected);
		getContentPane ().append (_radiobtnPanel, BorderLayout.NORTH);
		getContentPane ().append (_labelPanel, BorderLayout.CENTER);
		setTitle ("How do you rate ASWing");
	}
	private function showRating (eventObj : Event) : Void
	{
		var temp : JRadioButton = JRadioButton (eventObj.getSource ());
		_selected.setText ("You selected " + temp.getText ());
	}
	public static function main (Void) : Void
	{
		var myWindow : Rating = new Rating ();
		myWindow.setLocation (50, 50);
		myWindow.setSize (200, 150);
		myWindow.setResizable (false);
		myWindow.show ();
	}
}

projects/aswing/tutorials/rating_application_jframe_boxlayout_jlabel_jradiobutton.txt · Last modified: 2007/02/20 08:21 by niko