import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.Box;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
public class Program extends JPanel implements ActionListener {
private JButton enter;
private JButton clear;
private JButton exit;
private JRadioButton option1;
private JRadioButton option2;
private JRadioButton option3;
private int array1[] = {15, 5, 25};
private int array2[] = {2, 10, 4};
private JFormattedTextField output1;
Dimension size = new Dimension(80, 20);
private static Program program;
static JFrame aWindow = new JFrame("A Program");
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() { // Anonymous Runnable class object
public void run() { // Run method executed in thread
program = new Program(); // add the class to the mian method
program.createGUI(); // Call static GUI creator
}
} );
} // end main method
public void createGUI () {
Toolkit theKit = awindow.getToolkit(); // Call the toolkit
// use the called toolkit to get the screen size
Dimension wndSize = theKit.getScreenSize();
// set location of the GUI
awindow.setBounds(wndSize.width/4, wndSize.height/4, 500, 375);
// Tells the program to stop running whent the program is exited
awindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Tells the program to display the window
awindow.setVisible(true);
option1 = new JRadioButton("Option 1 outputs 30");
option2 = new JRadioButton("Option 2 outputs 50");
option3 = new JRadioButton("Option 3 outputs 100");
enter = new JButton("Enter");
enter.setPreferredSize(size);
enter.addActionListener(this);
clear = new JButton("Clear");
clear.setPreferredSize(size);
clear.addActionListener(this);
exit = new JButton("Exit");
exit.setPreferredSize(size);
exit.addActionListener(this);
output1 = new JFormattedTextField();
Box optionsBox = Box.createHorizontalBox();
optionsBox.add(option1);
optionsBox.add(option2);
optionsBox.add(option3);
Box outputBox = Box.createHorizontalBox();
outputBox.add(output1);
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(enter);
buttonBox.add(clear);
buttonBox.add(exit);
JPanel container = new JPanel(new GridLayout(0,1));
container.add(optionsBox);
container.add(buttonBox);
container.add(outputBox);
// Initilize a variable to get the size of the window
Container content = awindow.getContentPane();
// Define the layout based on the size of the window retrieved from previous statement
BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS);
// Add the main pane and buttons to the window
content.setLayout(box);
content.add(container);
}// end createGUI method
public void actionPerformed(ActionEvent event) {
// Creating a variable that is equal to the event, defining which button is pressed
Object action = event.getSource();
Object optionselection = event.getActionCommand();
if (action == enter) {
output1.setText(array1[] * array2[]);
}// end enter if
if (action == clear) {
output1.setText(" ");
}// end enter if
if (action == exit) {
System.exit(0);
}// end enter if
}// end actionPerformed method
}// end program class
6 Replies - 3047 Views - Last Post: 20 May 2011 - 05:08 PM
#1
Help populating information from a JRadioButton to an Array
Posted 19 May 2011 - 03:06 PM
I am attempting to have three options to select from, when button is pressed I need the selected JRadioButton to pass information to a loop that contains arrays then output. I am struggling with how to grab the selected JRadioButton in the action listener and how to then assign that selected value to an array. for example option1 would pass the value of 0, option2 the value of 1 and option3 the value of 2 so that the pre-set values in the array can be used in the loop--- I have written a basic program to illustrate my delima, can someone help me to figure how this is done in my example and I will apply this knowledge to my program
Replies To: Help populating information from a JRadioButton to an Array
#2
Re: Help populating information from a JRadioButton to an Array
Posted 19 May 2011 - 03:28 PM
public void actionPerformed(ActionEvent e) {
int val = 0;
if(option1.isSelected())
val = 30;
else if(option2.isSelected())
val = 50;
else val = 100;
This post has been edited by pbl: 19 May 2011 - 03:29 PM
#3
Re: Help populating information from a JRadioButton to an Array
Posted 19 May 2011 - 03:29 PM
You will need to add the radiobuttons to a ButtonGroup. Then you could add a ChangeListener to the radiobuttons, and have a enum telling you which one is selected. like: myRadiobutton.addChangeListener( this );
#4
Re: Help populating information from a JRadioButton to an Array
Posted 19 May 2011 - 03:35 PM
Set the names of the radio buttons to "0", "1" and "2" and add an instance of the following inner class to each:
private class ArrayChoiceAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent)e.getSource();
selectedValue = array[Integer.valueOf(c.getName())]; // 'selectedValue' is instance variable of outer class, as is 'array'
}
}
#5
Re: Help populating information from a JRadioButton to an Array
Posted 20 May 2011 - 03:47 AM
g00se, on 19 May 2011 - 03:35 PM, said:
Set the names of the radio buttons to "0", "1" and "2" and add an instance of the following inner class to each:
private class ArrayChoiceAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent)e.getSource();
selectedValue = array[Integer.valueOf(c.getName())]; // 'selectedValue' is instance variable of outer class, as is 'array'
}
}
Okay, I will give that a shot, thank you...
-To the reply about the button group, thank you. I forgot to add my radio buttons to a group in my sample program as well as my real program, that would have been a big oops!
#6
Re: Help populating information from a JRadioButton to an Array
Posted 20 May 2011 - 04:21 AM
mylosol, on 20 May 2011 - 03:47 AM, said:
g00se, on 19 May 2011 - 03:35 PM, said:
Set the names of the radio buttons to "0", "1" and "2" and add an instance of the following inner class to each:
private class ArrayChoiceAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent)e.getSource();
selectedValue = array[Integer.valueOf(c.getName())]; // 'selectedValue' is instance variable of outer class, as is 'array'
}
}
Okay, I will give that a shot, thank you...
-To the reply about the button group, thank you. I forgot to add my radio buttons to a group in my sample program as well as my real program, that would have been a big oops!
I think you should try pbl's example. At least use it before mine, as my approach just gives you more work than is actually needed
#7
Re: Help populating information from a JRadioButton to an Array
Posted 20 May 2011 - 05:08 PM
I ended up going with a simple if else
int i;
int j;
if (option1.isSelected()){i = 0; j = 0;}
else if(option2.isSelected()){i = 1; j = 1;}
else if(option3.isSelected()){i = 2; j = 2;}
else {i = 0; j = 0;}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|