Welcome to Dream.In.Code
Become a Java Expert!

Join 150,199 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,993 people online right now. Registration is fast and FREE... Join Now!




GUI event handling

 
Closed TopicStart new topic

GUI event handling, compute for the monthly interest and monthly payment.

smile_myglitz08
12 Sep, 2008 - 11:42 AM
Post #1

New D.I.C Head
*

Joined: 31 Jul, 2008
Posts: 27


My Contributions
hi.
i just want to ask for a help..
pls help me fix my code..
am having a hard time in GUI event handling..
can u help me???
am still confused in using event in java..
hope you can help me.

CODE


import java.awt.*;
import javax.swing.*;
//import java.awt.event.*;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author japh
*/
public class LoanInfo extends JFrame{
    
    
    JPanel mainPanel;
    JPanel mpl;
    JPanel pnl;
    JPanel panel;
    JPanel Lbuttons;      
    JLabel name;
    JLabel amount;
    JLabel months;
    JLabel interest1;
    JLabel lbl;
    JButton monInt;
    JButton monPay;
    JLabel rate;
    JTextField atxt;
    JTextField text;
    JTextField mtxt;
    JTextField interest;
      
      
    //set up GUI components
    private void launchFrame(){
        setTitle("Loan Index");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);      
        setResizable(false);        
        name= new JLabel("Name: ");
        text= new JTextField(25);
        amount= new JLabel("Amount: ");
        atxt= new JTextField(10);
        months=new JLabel("Number Of Months To Pay: ");
        mtxt= new JTextField(4);
        interest1=new JLabel("Interest: ");
        interest=new JTextField(5);
        rate=new JLabel("%");
        lbl=new JLabel("Compute for: ");
        monInt=new JButton("Monthly Interest");
        monPay=new JButton("Monthly Payment");        
          mainPanel=new JPanel(new BorderLayout());          
        pnl= new JPanel(new FlowLayout(FlowLayout.RIGHT));  
        mpl=new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel=new JPanel(new FlowLayout(FlowLayout.LEFT));
         Lbuttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
         pnl.add(amount);
        pnl.add(atxt);
        pnl.add(months);
        pnl.add(mtxt);        
        mpl.add(name);
        mpl.add(text);
        panel.add(interest1);
        panel.add(interest);
        panel.add(rate);
        Lbuttons.add(lbl);
        Lbuttons.add(monInt);
        Lbuttons.add(monPay);
            
        mainPanel.add(pnl,BorderLayout.WEST);
        mainPanel.add(mpl,BorderLayout.NORTH);
          mainPanel.add(panel,BorderLayout.EAST);
          mainPanel.add(Lbuttons,BorderLayout.SOUTH);
  
       //adding event Listeners
       
       //    monInt.addActionListener(this);
       //    monPay.addActionListener(this);
  
    
        add(mainPanel);    
        pack();
        setVisible(true);
    }
    
     public void actPerformed(ActionEvent d){
        Object ch= d.getSource();
        int monthly,i,j;
        int payment;
        if(ch==monInt){            
            i=(rate*months);
            j=(i*amount);
            monthly=(j*1);
            JOptionPane.showMessageDialog(null,monthly);
            
            
        }
        else{
            payment=(monthly+rate);
            if(ch==monPay){
                
              JOptionPane.showMessageDialog(this,"Password is Correct!");
              
           }
        
  
}
    
public static void main(String[] args){
        LoanInfo loan= new LoanInfo();
        loan.launchFrame();    
    }
   }

User is offlineProfile CardPM
+Quote Post

BigAnt
RE: GUI Event Handling
12 Sep, 2008 - 12:31 PM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Aug, 2008
Posts: 361



Thanked: 31 times
Dream Kudos: 25
My Contributions
you have to import ActionEvent:
import java.awt.event.ActionEvent;

Also when you do the calculations the Jlabels you have to use the getText() method to get the value of the labels and then parse it to the correct type to use in the calulations

Also in the action method you reference the variable monthly in the else statement when it is initialized in the first if statement, meaning it hasn't been initialized in the else statement
User is offlineProfile CardPM
+Quote Post

smile_myglitz08
RE: GUI Event Handling
12 Sep, 2008 - 01:03 PM
Post #3

New D.I.C Head
*

Joined: 31 Jul, 2008
Posts: 27


My Contributions
QUOTE(BigAnt @ 12 Sep, 2008 - 01:31 PM) *

you have to import ActionEvent:
import java.awt.event.ActionEvent;

Also when you do the calculations the Jlabels you have to use the getText() method to get the value of the labels and then parse it to the correct type to use in the calulations

Also in the action method you reference the variable monthly in the else statement when it is initialized in the first if statement, meaning it hasn't been initialized in the else statement


Like this one???
CODE


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author japh
*/
public class LoanInfo extends JFrame implements ActionListener{
    
    
    JPanel mainPanel;
    JPanel mpl;
    JPanel pnl;
    JPanel panel;
    JPanel Lbuttons;      
    JLabel name;
    JLabel amount;
    JLabel months;
    JLabel interest1;
    JLabel lbl;
    JButton monInt;
    JButton monPay;
    JLabel rate;
    JTextField atxt;
    JTextField text;
    JTextField mtxt;
    JTextField interest;
      
      
    //set up GUI components
    private void launchFrame(){
        setTitle("Loan Index");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);      
        setResizable(false);        
        name= new JLabel("Name: ");
        text= new JTextField(25);
        amount= new JLabel("Amount: ");
        atxt= new JTextField(10);
        months=new JLabel("Number Of Months To Pay: ");
        mtxt= new JTextField(4);
        interest1=new JLabel("Interest: ");
        interest=new JTextField(5);
        rate=new JLabel("%");
        lbl=new JLabel("Compute for: ");
        monInt=new JButton("Monthly Interest");
        monPay=new JButton("Monthly Payment");        
          mainPanel=new JPanel(new BorderLayout());          
        pnl= new JPanel(new FlowLayout(FlowLayout.RIGHT));  
        mpl=new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel=new JPanel(new FlowLayout(FlowLayout.LEFT));
         Lbuttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
         pnl.add(amount);
        pnl.add(atxt);
        pnl.add(months);
        pnl.add(mtxt);        
        mpl.add(name);
        mpl.add(text);
        panel.add(interest1);
        panel.add(interest);
        panel.add(rate);
        Lbuttons.add(lbl);
        Lbuttons.add(monInt);
        Lbuttons.add(monPay);
            
        mainPanel.add(pnl,BorderLayout.WEST);
        mainPanel.add(mpl,BorderLayout.NORTH);
          mainPanel.add(panel,BorderLayout.EAST);
          mainPanel.add(Lbuttons,BorderLayout.SOUTH);
  
       //adding event Listeners
       
           monInt.addActionListener(this);
           monPay.addActionListener(this);
  
    
        add(mainPanel);    
        pack();
        setVisible(true);
    }
    
     private class ActionHandler implements ActionListener{
        public void actionPerformed(ActionEvent d){
        Object ch= d.getSource();
        String amount1=String.valueOf(atxt.getText());
        String interest2=String.valueOf(interest.getText());
        String months2=String.valueOf(mtxt.getText());
      
       double monthly,i,j;
       double payment;
        if(ch==monInt){            
            i=(rate*months);
            j=(i*amount);
            monthly=(j*1);
            JOptionPane.showMessageDialog(null,monthly);
            
            
        }
        else{
            payment=(monthly+rate);
            if(ch==monPay){
                
              JOptionPane.showMessageDialog(null,payment);
               System.exit(0);
           }
        
  
}
    
public static void main(String[] args){
        LoanInfo loan= new LoanInfo();
        loan.launchFrame();    
    }
   }

but there are still a lot of error,,.help me..
i dont know what to do next..


User is offlineProfile CardPM
+Quote Post

BigAnt
RE: GUI Event Handling
12 Sep, 2008 - 01:47 PM
Post #4

D.I.C Regular
Group Icon

Joined: 16 Aug, 2008
Posts: 361



Thanked: 31 times
Dream Kudos: 25
My Contributions
If you are going to have a seperate class for the ActionListener then the LoanInfo class should not implement ActionListener and to add the listener you add
ActionHandler myHandler = new ActionHandler();

and add it to the button by calling the add action listener method with myHandler as the argument.

Also you have to parse the label values to the correct type to do the calculations

User is offlineProfile CardPM
+Quote Post

smile_myglitz08
RE: GUI Event Handling
12 Sep, 2008 - 01:55 PM
Post #5

New D.I.C Head
*

Joined: 31 Jul, 2008
Posts: 27


My Contributions
QUOTE(BigAnt @ 12 Sep, 2008 - 02:47 PM) *

If you are going to have a seperate class for the ActionListener then the LoanInfo class should not implement ActionListener and to add the listener you add
ActionHandler myHandler = new ActionHandler();

and add it to the button by calling the add action listener method with myHandler as the argument.

Also you have to parse the label values to the correct type to do the calculations



Am sorry..I don't get it.. I think I can't make it to the deadline..thanks for the help..

Cannot make it for the deadline... let's close the topic

This post has been edited by pbl: 12 Sep, 2008 - 08:56 PM
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/9/09 04:54AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month