File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How to perform Calculation when a button a pressed in a Frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to perform Calculation when a button a pressed in a Frame" Watch "How to perform Calculation when a button a pressed in a Frame" New topic

How to perform Calculation when a button a pressed in a Frame

sampath karnati

Joined: Jan 19, 2005
Posts: 3
Hi friends,

I have created a Frame which has some checkboxes , a button and text field. when i check some checkboxes and press the Button (ie in my program Metrics level Button ) it should display the result as number of checkboxes that are checked divided by total number of chechboxes. ie if i check some 6 check boxes and press the Metrics level button it should display 6 divided by 12 ie 0.5 in the Result Textfield.

I am sending the code i have written.

Thanks in advance.

public class Frame extends java.awt.Frame {

/** Creates new form Frame */
public Frame() {
setSize(800, 800);

private void initComponents() {
label1 = new java.awt.Label();
checkbox1 = new java.awt.Checkbox();
checkbox2 = new java.awt.Checkbox();
checkbox3 = new java.awt.Checkbox();
checkbox4 = new java.awt.Checkbox();
checkbox5 = new java.awt.Checkbox();
checkbox6 = new java.awt.Checkbox();
checkbox7 = new java.awt.Checkbox();
checkbox8 = new java.awt.Checkbox();
checkbox9 = new java.awt.Checkbox();
checkbox10 = new java.awt.Checkbox();
checkbox11 = new java.awt.Checkbox();
checkbox12 = new java.awt.Checkbox();
button1 = new java.awt.Button();
textField1 = new java.awt.TextField();


addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {

label1.setText("Select the Metrics below");
label1.setBounds(320, 20, 136, 20);

checkbox1.setLabel("Architecture Metrics");
checkbox1.setBounds(240, 80, 84, 20);

checkbox2.setLabel("Runtime Metrics");
checkbox2.setBounds(240, 200, 115, 20);

checkbox3.setLabel("Documentation Metrics");
checkbox3.setBounds(240, 320, 152, 20);

checkbox4.setBounds(280, 110, 49, 20);

checkbox5.setBounds(280, 130, 75, 20);

checkbox6.setBounds(280, 150, 86, 20);

checkbox7.setBounds(290, 230, 49, 20);

checkbox8.setBounds(290, 250, 75, 20);

checkbox9.setBounds(290, 270, 86, 20);

checkbox10.setBounds(300, 350, 49, 20);

checkbox11.setBounds(300, 370, 75, 20);

checkbox12.setBounds(300, 390, 86, 20);

button1.setLabel("Metrics level");
button1.setBounds(290, 470, 83, 24);

textField1.setBounds(400, 470, 60, 20);

public void actionPerformed(ActionEvent e) {

****** I think code should be added here for the button pressed event*******

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {

* @param args the command line arguments
public static void main(String args[]) {
new Frame().show();

// Variables declaration - do not modify
private java.awt.Button button1;
private java.awt.Checkbox checkbox1;
private java.awt.Checkbox checkbox10;
private java.awt.Checkbox checkbox11;
private java.awt.Checkbox checkbox12;
private java.awt.Checkbox checkbox2;
private java.awt.Checkbox checkbox3;
private java.awt.Checkbox checkbox4;
private java.awt.Checkbox checkbox5;
private java.awt.Checkbox checkbox6;
private java.awt.Checkbox checkbox7;
private java.awt.Checkbox checkbox8;
private java.awt.Checkbox checkbox9;
private java.awt.Label label1;
private java.awt.TextField textField1;
// End of variables declaration

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Well, you told us what it should do and even pointed out in your code where you need to start making these calculations. However, you never really said what you needed help with. Does the code you have not work? Do you not know how to use operators in Java (+ * - /)? Do you not know how to get the number of checked check boxes? In other words, ask a question.

GenRocket - Experts at Building Test Data
sampath karnati

Joined: Jan 19, 2005
Posts: 3
hi Gregg Bolinger,

Thanks for the reply. Actually my question is i dont know how to calculate the number of checked check boxes when i press the Metrics level button in my code. Can you help me .

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

There are a couple of ways. Either way you need to keep some sort of counter.

1. Add an ItemListener to your checkboxes. When the checkbox's are checked or unchecked, have your listener update your counter accordingly. Then your counter will always be == the current number of checked boxes.

2. JCheckbox has a method, isSelected(). When you click the calculate button, the first thing to do would be to check all your checkboxes with this method and update your counter accordingly.

Option #1 would be my preferred way of doing it. You can find some good information on JCeckboxes on Sun's Swing Tutorial and it shows how to handle the ItemListener.
I agree. Here's the link:
subject: How to perform Calculation when a button a pressed in a Frame
It's not a secret anymore!