Tuesday, June 9, 2009

To create a selection menu : -

To create a selection menu : -
Today I had tired out to learn how to create a CheckMenu. I am writing it down for my future refernce
Step 1
First I decided to create a frame and display it

The class which I create will be using "JFrame" Class and "ActionListener" interfaces
imported packages , I
"
java.awt.GridLayout;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
javax.swing.JFrame;
javax.swing.JPanel;

"


Now in the constructor I created Jpanel and Gridlayout
and in the Constructor I had done the following steps
Created a instance of the class
then I set the size
Then To made it visible

Now my Constructor have
"
JFrame frame = new Main();
frame.setSize(150, 200);
frame.setVisible(true);

"

Now my MyRadioCheckMenu.java has the following code

"
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Main extends JFrame implements ActionListener {

public MyRadioCheckMenu(){
//Create a JPanel and GridLayout
JPanel p = new JPanel();
p.setLayout(new GridLayout(8, 1));

}

public static void main(String[] args) {
JFrame frame = new MyRadioCheckMenu();
frame.setSize(150, 200);
frame.setVisible(true);


}

public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}

}

"

##
Now when you run the Main.java file you could see a window .This is the first step now we will see to add check boxes
###################################################################################
Now my aim is to create a radio button and display it
For this what I have to do ?
# Create a method for adding all radio buttons to a Button Group and then add the button group to JPanel


For this first I did define JradioButtons such as

private JRadioButton butterChicken;
private JRadioButton chillyChicken;
private JRadioButton friedRice;
private JRadioButton chickenNoodles;
private JRadioButton tandoriRoti;
private JRadioButton lassiDrink;





I have method for retruning radio button as

private JRadioButton addRadioButton(String s, ButtonGroup g, JPanel p) {
JRadioButton button = new JRadioButton(s, false);
button.addActionListener(this);
g.add(button);
p.add(button);
return button;
}


Then I again I added in the constructor

ButtonGroup g = new ButtonGroup();
butterChicken = addRadioButton("Divide By Zero",g,p);
getContentPane().add(p);

Now my constructor looks like

public MyRadioCheckMenu(){
//Create a JPanel and GridLayout
JPanel p = new JPanel();
p.setLayout(new GridLayout(8, 1));
ButtonGroup g = new ButtonGroup();
butterChicken = addRadioButton("Butter Chicken",g,p);
getContentPane().add(p);

}




and my code for displaying one radio button is



package mycheckmenu;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class MyRadioCheckMenu extends JFrame implements ActionListener {
private JRadioButton butterChicken;
private JRadioButton chillyChicken;
private JRadioButton friedRice;
private JRadioButton chickenNoodles;
private JRadioButton tandoriRoti;
private JRadioButton lassiDrink;


public MyRadioCheckMenu(){
//Create a JPanel and GridLayout
JPanel p = new JPanel();
p.setLayout(new GridLayout(8, 1));
ButtonGroup g = new ButtonGroup();
butterChicken = addRadioButton("Butter Chicken",g,p);
getContentPane().add(p);

}

private JRadioButton addRadioButton(String s, ButtonGroup g, JPanel p) {
JRadioButton button = new JRadioButton(s, false);
button.addActionListener(this);
g.add(button);
p.add(button);
return button;
}

public static void main(String[] args) {
JFrame frame = new MyRadioCheckMenu();
frame.setSize(150, 200);
frame.setVisible(true);


}

public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}

}


###################
Now let me try out to display more radio button
in the constructor I added more buttons
At present my constructor looks like

public MyRadioCheckMenu(){
//Create a JPanel and GridLayout
JPanel p = new JPanel();
p.setLayout(new GridLayout(8, 1));
ButtonGroup g = new ButtonGroup();
butterChicken = addRadioButton("Butter Chicken",g,p);
chillyChicken = addRadioButton("Chiily Chciken", g, p);
friedRice = addRadioButton("Fried Rice", g, p);
chickenNoodles = addRadioButton("Chicken Noodles", g, p);
tandoriRoti = addRadioButton("Tandoori Roti", g, p);
lassiDrink = addRadioButton("Sweet Lasi", g, p);
getContentPane().add(p);

}


and my code look like


###



package mycheckmenu;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class MyRadioCheckMenu extends JFrame implements ActionListener {
private JRadioButton butterChicken;
private JRadioButton chillyChicken;
private JRadioButton friedRice;
private JRadioButton chickenNoodles;
private JRadioButton tandoriRoti;
private JRadioButton lassiDrink;


public MyRadioCheckMenu(){
//Create a JPanel and GridLayout
JPanel p = new JPanel();
p.setLayout(new GridLayout(8, 1));
ButtonGroup g = new ButtonGroup();
butterChicken = addRadioButton("Butter Chicken",g,p);
chillyChicken = addRadioButton("Chiily Chciken", g, p);
friedRice = addRadioButton("Fried Rice", g, p);
chickenNoodles = addRadioButton("Chicken Noodles", g, p);
tandoriRoti = addRadioButton("Tandoori Roti", g, p);
lassiDrink = addRadioButton("Sweet Lasi", g, p);
getContentPane().add(p);

}

private JRadioButton addRadioButton(String s, ButtonGroup g, JPanel p) {
JRadioButton button = new JRadioButton(s, false);
button.addActionListener(this);
g.add(button);
p.add(button);
return button;
}

public static void main(String[] args) {
JFrame frame = new MyRadioCheckMenu();
frame.setSize(150, 200);
frame.setVisible(true);


}

public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}

}



###
Next is to trigger some actions or activity when we click the check menu

No comments: