Results 1 to 7 of 7
  1. #1
    Heat is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Problem with actionperformed callback

    I tried to create function, what shows textdialog, when client press the buton, but the problem is that i have defined frame under public static void main and in ActionPerformed callback first parameter "frame" is undefined. How could i fix this?
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import javax.swing.*;

    public class Vestlus extends Applet implements ActionListener {
    TextField tf = new TextField("");
    Button nupp = new Button(" Sisesta ");



    public Vestlus()
    {
    add(tf);
    add(nupp);
    nupp.addActionListener(this);



    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == nupp)
    {
    JOptionPane.showMessageDialog(frame,"Hoiatus","Vää rtused ei klapi!", JOptionPane.INFORMATION_MESSAGE);


    }
    }
    public static void main(String args[])
    {

    JFrame frame = new JFrame("Project LA");
    frame.add(new Vestlus());
    frame.setSize(300,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(1);


    }

    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Problem with actionperformed callback

    Move your frame variable an instance field of your enclosing class. And initialize the frame and related fields in the constructor.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Problem with actionperformed callback

    Move the definition to a place where it can be seen by (is in scope for) other methods.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Heat is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: Problem with actionperformed callback

    (to a place where it can be seen) Yeah i must to this, but i dont know, how.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Problem with actionperformed callback

    Pass frame in the call to the constructor and save it in a variable defined in the class so its value is available to all the methods in the class.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Heat is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: Problem with actionperformed callback

    Is this normal way, or itś stupid?
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import javax.swing.*;
    
    public class Vestlus extends Applet implements ActionListener {
    			TextField tf = new TextField("");
    			Button nupp = new Button(" Sisesta ");
    			
    			
    			public static void main(String args[])
    			{
    				
    				
    				JFrame  frame = new JFrame("Project LA");
    				frame.add(new Vestlus());
    				frame.setSize(300,200);
    				frame.setVisible(true);
    				frame.setDefaultCloseOperation(1);
    				
    			}
    			
    			public Vestlus()
    			{
    				add(tf);
    				add(nupp);
    				nupp.addActionListener(this);
    				
    				
    				
    			}
    			public void actionPerformed(ActionEvent e)
    			{
    				if(e.getSource() == nupp)
    				{
    					JFrame  frame = new JFrame("Project LA");
    					JOptionPane.showMessageDialog(frame,"Hoiatus","Väärtused ei klapi!", JOptionPane.INFORMATION_MESSAGE);
    					
    					
    				}
    			}
    			
    			
    				
    			}
    Last edited by Heat; 03-08-2014 at 11:51 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Problem with actionperformed callback

    Please use code tags to wrap your code:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. use api without callback url?
    By Seiya0890 in forum New To Java
    Replies: 0
    Last Post: 03-02-2013, 06:02 PM
  2. Replies: 3
    Last Post: 03-28-2012, 04:44 PM
  3. query with a callback?
    By Filobel in forum JDBC
    Replies: 4
    Last Post: 01-20-2012, 10:33 AM
  4. Replies: 8
    Last Post: 02-28-2010, 09:36 AM
  5. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 07:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •