Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...

    Hi,

    I have created the buttons along in the GUI, but I need help adding the ActionListener interface to make the buttons respond. Any assistance would be greatly appreciated...

    import javax.swing.*;
    import javax.swing.JFrame;//page 298 Core Java
    import javax.swing.JTextField;//page 379 Core Java
    import javax.swing.JButton;//page 331 Core Java
    import java.awt.Container;//page 331 Core Java
    import java.awt.FlowLayout;//page 371 Core Java
     
    class theButtons {
     
        public static void main(String args[]) {
     
            //declare/initialize variables
            JFrame frame;
            Container contentPane;
            JTextField textfield;
            JButton button;
            FlowLayout layout;
            String thePrincipal;
            String interestRate;
            String theTerm;
            String pleaseCalculate;
            String toRefresh;
     
            JFrame theFrame = new JFrame("McBride Financial Services");
            final int FRAME_WIDTH = 550;
              final int FRAME_HEIGHT = 300;
              theFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
     
            textfield = new JTextField("Type your text here.");
     
            contentPane = theFrame.getContentPane();
     
            thePrincipal = "Principal Amount";
            button = new JButton(thePrincipal);
            contentPane.add(textfield);
            contentPane.add(button);
            layout = new FlowLayout();
            contentPane.setLayout(layout);
     
            interestRate = "Interest Rate";
            button = new JButton(interestRate);
            contentPane.add(textfield);
            contentPane.add(button);
            layout = new FlowLayout();
            contentPane.setLayout(layout);
     
            theTerm = "Loan Term";
            button = new JButton(theTerm);
            contentPane.add(textfield);
            contentPane.add(button);
            layout = new FlowLayout();
            contentPane.setLayout(layout);
     
            pleaseCalculate = "Calculate";
            button = new JButton(pleaseCalculate);
            contentPane.add(textfield);
            contentPane.add(button);
            layout = new FlowLayout();
            contentPane.setLayout(layout);
     
            toRefresh = "Refresh";
            button = new JButton(toRefresh);
            contentPane.add(textfield);
            contentPane.add(button);
            layout = new FlowLayout();
            contentPane.setLayout(layout);
     
            theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            theFrame.pack();
            theFrame.setVisible(true);
        }
    }


  2. #2
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...

    hi beginning2understand,

    welcome to the java programming forums.

    below is the code: sorry because its not that great but hope it helps.

    i put the action listener in the textfield. if you click the button, it prints the text of that button.

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Guib {
     
       public static void main(String[] args) {
      Container contentPane;
       final JTextField textfield = new JTextField();
      final JButton button1, button2, button3, button4, button5;
      FlowLayout layout;
      String thePrincipal;
      String interestRate;
      String theTerm;
      String pleaseCalculate;
      String toRefresh;
     
            ActionListener a1 = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
     
                textfield.setText("Principal Amount");
             }
          };
     
            ActionListener a2 = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                textfield.setText("Interest Rate");
             }
          };
            ActionListener a3 = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                textfield.setText("Loan Term");
             }
          };
     
                ActionListener a4 = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
              textfield.setText("Calculate");
             }
          };
     
                ActionListener a5 = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
              textfield.setText("Refresh");
             }
          };
     
    JFrame theFrame = new JFrame("McBride Financial Services");
    final int FRAME_WIDTH = 550;
    final int FRAME_HEIGHT = 300;
    theFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
     
    textfield.setText("Type your text here.");
     
    contentPane = theFrame.getContentPane();
     
    thePrincipal = "Principal Amount";
    button1 = new JButton(thePrincipal);
    contentPane.add(textfield);
    contentPane.add(button1);
    layout = new FlowLayout();
    contentPane.setLayout(layout);
     
    interestRate = "Interest Rate";
    button2 = new JButton(interestRate);
    contentPane.add(textfield);
    contentPane.add(button2);
    layout = new FlowLayout();
    contentPane.setLayout(layout);
     
    theTerm = "Loan Term";
    button3 = new JButton(theTerm);
    contentPane.add(textfield);
    contentPane.add(button3);
    layout = new FlowLayout();
    contentPane.setLayout(layout);
     
    pleaseCalculate = "Calculate";
    button4 = new JButton(pleaseCalculate);
    //button.addActionListener(button);
    contentPane.add(textfield);
    contentPane.add(button4);
    layout = new FlowLayout();
    contentPane.setLayout(layout);
     
     
    toRefresh = "Refresh";
    button5 = new JButton(toRefresh);
    contentPane.add(textfield);
    contentPane.add(button5);
    layout = new FlowLayout();
    contentPane.setLayout(layout);
    //button5.addActionListener(a1);
     
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theFrame.pack();
    theFrame.setVisible(true);
     
    button1.addActionListener(a1);
    theFrame.getContentPane().add(button1);
     
    button2.addActionListener(a2);
    theFrame.getContentPane().add(button2);
     
    button3.addActionListener(a3);
    theFrame.getContentPane().add(button3);
     
    button4.addActionListener(a4);
    theFrame.getContentPane().add(button4);
     
    button5.addActionListener(a5);
    theFrame.getContentPane().add(button5);
     
     
       }
    }


    Cheers,
    Truffy

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...

    thank you for your post Truffy... I apologize for taking so long in getting back to you... life has a way of doing that sometimes...

  4. #4
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Talking Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...

    Quote Originally Posted by beginning2Understand View Post
    thank you for your post Truffy... I apologize for taking so long in getting back to you... life has a way of doing that sometimes...
    its okay? if it solved your problem pls marked as solved. thanks.

    Cheers,
    Truffy

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...

    will do...

  6. #6
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...

    Quote Originally Posted by beginning2Understand View Post
    will do...
    great.

    Cheers,
    Truffy.

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 06:14 PM
  2. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 03:49 AM
  3. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 05:38 PM