Results 1 to 7 of 7
- 03-08-2014, 06:31 PM #1
Member
- Join Date
- Mar 2014
- Posts
- 4
- Rep Power
- 0
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);
}
}
- 03-08-2014, 06:41 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-08-2014, 06:42 PM #3
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.
- 03-08-2014, 06:45 PM #4
Member
- Join Date
- Mar 2014
- Posts
- 4
- Rep Power
- 0
Re: Problem with actionperformed callback
(to a place where it can be seen) Yeah i must to this, but i dont know, how.
- 03-08-2014, 07:04 PM #5
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.
- 03-08-2014, 07:22 PM #6
Member
- Join Date
- Mar 2014
- Posts
- 4
- Rep Power
- 0
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 10:51 PM.
- 03-08-2014, 09:18 PM #7
Similar Threads
-
use api without callback url?
By Seiya0890 in forum New To JavaReplies: 0Last Post: 03-02-2013, 05:02 PM -
Problem visualize Jframe contents during the execution of an ActionPerformed
By gtotto in forum AWT / SwingReplies: 3Last Post: 03-28-2012, 03:44 PM -
query with a callback?
By Filobel in forum JDBCReplies: 4Last Post: 01-20-2012, 09:33 AM -
i had a problem in adding a value on an array using actionperformed(actionevent)
By angelovers07 in forum JDBCReplies: 8Last Post: 02-28-2010, 08:36 AM -
actionPerformed problem
By tomitzel in forum New To JavaReplies: 1Last Post: 01-08-2008, 06:10 PM
Bookmarks