Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


SakthiPrabhu.g
Hi .Again me..
2 Answer(s)      6 years and 3 months ago
Posted in : Java Beginners



Advertisement
View Answers

December 19, 2008 at 4:12 AM


Hi friend,


import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

public class SRadioButtonDemo{

public static void main(String args[]){

JFrame frame = new JFrame("Button Group");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Examples");
panel.setBorder(border);
ButtonGroup group = new ButtonGroup();
AbstractButton abstract1 = new JToggleButton("Toggle Button");
panel.add(abstract1);
group.add(abstract1);
JRadioButton rbutton = new JRadioButton("Radio Button");
panel.add(rbutton);
group.add(rbutton);
JCheckBox chekbox = new JCheckBox("Check Box");
panel.add(chekbox);
group.add(chekbox);
AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
panel.add(abstract4);
group.add(abstract4);
AbstractButton jcheckmenu = new JCheckBoxMenuItem("Check Box Menu Item");
panel.add(jcheckmenu);
group.add(jcheckmenu);
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}

--------------------------------------------------------

Visit for more information.

http://www.roseindia.net/java/example/java/swing/

Thanks.


December 19, 2008 at 5:42 AM


I am sending running code according to your requirement. Please implement following code.

import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;



public class RadioButtonDemo extends JPanel implements ActionListener {
static String birdString = "Rose";
static String catString = "Cat";
static String dogString = "Dog";
static String tigerString = "Tiger";
static String undoString = "Undo";

JLabel picture;

public RadioButtonDemo() {
super(new BorderLayout());

//Create the radio buttons.
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand(birdString);
birdButton.setSelected(true);

JRadioButton catButton = new JRadioButton(catString);
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand(catString);

JRadioButton dogButton = new JRadioButton(dogString);
dogButton.setMnemonic(KeyEvent.VK_D);
dogButton.setActionCommand(dogString);

JRadioButton rabbitButton = new JRadioButton(tigerString);
rabbitButton.setMnemonic(KeyEvent.VK_R);
rabbitButton.setActionCommand(tigerString);

JRadioButton pigButton = new JRadioButton(undoString);
pigButton.setMnemonic(KeyEvent.VK_P);
pigButton.setActionCommand(undoString);

//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
group.add(dogButton);
group.add(rabbitButton);
group.add(pigButton);

//Register a listener for the radio buttons.
birdButton.addActionListener(this);
catButton.addActionListener(this);
dogButton.addActionListener(this);
rabbitButton.addActionListener(this);
pigButton.addActionListener(this);

//Set up the picture label.
picture = new JLabel(createImageIcon("images/" + birdString + ".gif"));


picture.setPreferredSize(new Dimension(177, 122));

//Put the radio buttons in a column in a panel.
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(birdButton);
radioPanel.add(catButton);
radioPanel.add(dogButton);
radioPanel.add(rabbitButton);
radioPanel.add(pigButton);

add(radioPanel, BorderLayout.LINE_START);
add(picture, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

// Listens to the radio buttons
public void actionPerformed(ActionEvent e) {
picture.setIcon(createImageIcon("images/" + e.getActionCommand() + ".gif"));
}

// Returns an ImageIcon, or null if the path was invalid
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = RadioButtonDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("RadioButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new RadioButtonDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);

frame.setSize(300,250);
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
---------------------------
Thanks.


Related Tutorials/Questions & Answers:
Hi .Again me.. - Java Beginners
Hi .Again me..  Hi Friend...... can u pls send me some code on JPanel.. JPanel shoul have 1pic 1RadioButton .. Like a Voter List... REsponse me..  Hi friend, import java.io.*; import java.awt.
hi again - Java Beginners
hi again  i did the changes on the code but still the time is not decreasing i wanna reach increasing running time target sorry for asking too...; // testing every character for (int n=0 ;n  Hi friend, Do changes
Advertisements
Hi..Again Doubt .. - Java Beginners
Hi..Again Doubt ..  Thank u for ur Very Good Response...Really great.. i have completed that.. If i click the RadioButton,,ActionListenr should get... WORRY WE'ILL ROCK "); } } }   Hi Can u send your MainMenu.java
hi frnds.. help me..
hi frnds.. help me..  i ve a doubt in incompatible type error (block letter). plz help me clear this error! thanks in advance. This is my code. double velo[][] = new double [task] [resource]; for(int i=0;iList<Double>
hi see and give me reply as soon as possible
hi see and give me reply as soon as possible  Hi Friend, I got path,but it will again ask path error first i was gave index.jsp.It is displayed and it is not going to next level. HTTP Status 404 - /struts/hello type
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a program that displays the following table (note that 1 mile is 1.609 kilometers). Miles Kilometers
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a program that prompts the user to enter the number of students and each student‚??s name and score, and finally displays the name of the students
hi... pls give me the example of java coding fo following question
hi... pls give me the example of java coding fo following question  example java coding that reads in the radius and length of cylinder and computes volume. formula: Area = radius * radius * PI Volume = area * length
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a program that reads in investment amount, annual interest rate, and number of years, and displays the future investment value using the following formula
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Suppose that the tuition for a university is RM10,000 this year and increases 5% every year. Write a program that computes the tuition in ten years and the total cost
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a function called ChangeToNext that take in an uppercase character parameter ch. The function returns the next uppercase character if ch is between 'A' and 'Y
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a class that contains the following two methods: /* converts from inches to centimeters */ Public static double inchToCentimeter(double in) /* converts from
hi... pls give me the example of java coding for following question
hi... pls give me the example of java coding for following question   Write a program that computes loan payments. The loan can be a car loan, a student loan, or a home mortgage loan. The program lets the user enter the interest
hi... pls give me the example of java coding fo following question
hi... pls give me the example of java coding fo following question  Write a java program that reads the following information and prints a payroll statement: Employee‚??s name (e.g: Sam) Number of hours worked in a week (e.g: 10
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Body Mass Index(BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters
hi i m new to hibernate..plzzzzzzzzzzzzzz help me.... - Hibernate
hi i m new to hibernate..plzzzzzzzzzzzzzz help me....  hi friends i m......... plzzzzzzzzzzzzzzz help me whats the problem... its showing me //************Error********* could not load an entity: [indiabulls.test.Contact#24]  Hi
HI.
HI.  hi,plz send me the code for me using search button bind the data from data base in dropdownlist
hi....
hi....  plzz sent me d code for counting vowels in a string... gui programme
Hi
Hi  Hi How to implement I18N concept in struts 1.3? Please reply to me
Hi
Hi  I want import txt fayl java.please say me...   Hi, Please clarify your problem! Thanks
Hi
Hi  Hi All, I am new to roseindia. I want to learn struts. I do not know anything in struts. What exactly is struts and where do we use it. Please help me. Thanks in advance. Regards, Deepak
Hi
Hi   I have got this code but am not totally understanding what the errors. Could someone Please help. Thanks in advance! import java.util.Random...!"); // this says to guess higher System.out.print("Wrong! Again Guess
hi
online multiple choice examination  hi i am developing online multiple choice examination for that i want to store questions,four options,correct answer in a xml file using jsp or java?can any one help me?   Please
Hi,
Hi,  Hi,what is the purpose of hash table
hi
hi  My program is not find java.io.File; why? help me please
hi
, again the label with √Ę‚?¨Ň?+Click to add another account√Ę‚?¨¬? should be enable
hi.......
for such a programme... plz help me...   Hi Friend, Try this: import java.awt....hi.......  i've a project on railway reservation... i need to connect netbeans and mysql with the help of jdbc driver... then i need to design
hi
, display the menu again. the user should respond to this prompt with another
Hi..
Hi..   what are the keywords available in simple HelloWorld program
Hi..
Hi..  what are access specifier available in java
Hi,
Hi,   labels = new Hashtable<Integer,JLabel>
 

 

 

DMCA.com