answersLogoWhite

0

An ActionListener is exactly what it sounds like. It's an interface used by other classes to listen for an action event. The simplest example of this is on a Button. Normally when you add a Button to a Component nothing will happen when you press it. You need to use a button.addActionListener(actionListener) call to make it listen for button clicks.

User Avatar

Wiki User

16y ago

What else can I help you with?

Continue Learning about Engineering

How is interface instantiated in anonymous class for example new ActionListener?

They key word here is anonymous class. While ActionListener may be an interface, the anonymous class would be a subclass of ActionListener. It would be like creating a new class which implements ActionListener.JButton button = new JButton("Press Me!");button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ev) {System.out.println("PRESS");}});


Which methods are there in ActionListener interface?

The ActionListener interface has a single method.void actionPerformed(ActionEvent e) For full Java documentation see Oracle JavaDoc URL in related links below.


Is the ActionListener and MouseListener is same in java?

No, ActionListener and MouseListener are not the same in Java. ActionListener is used to handle action events, such as button clicks or menu selections, while MouseListener is specifically designed to handle mouse events, such as mouse clicks, presses, releases, and movements. Each listener serves a distinct purpose in responding to different types of user interactions in GUI applications.


What is the meaning of anonymous classes for events in java?

Anonymous classes, basically, have no name. It combines the class declaration and the creation of an instance of the class in one step. If an event handler is not shared by multiple components there is no need to declare a class to handle the event. The handler can be implemented with the use of an anonymous inner class. i.e. : button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "You clicked button1."); } });


Source code for notepad in java?

import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.io.*; public class Editor extends Frame { String filename; TextArea tx; Clipboard clip = getToolkit().getSystemClipboard(); Editor() { setLayout(new GridLayout(1,1)); tx = new TextArea(); add(tx); MenuBar mb = new MenuBar(); Menu F = new Menu("file"); MenuItem n = new MenuItem("New"); MenuItem o = new MenuItem("Open"); MenuItem s = new MenuItem("Save"); MenuItem e = new MenuItem("Exit"); n.addActionListener(new New()); F.add(n); o.addActionListener(new Open()); F.add(o); s.addActionListener(new Save()); F.add(s); e.addActionListener(new Exit()); F.add(e); mb.add(F); Menu E = new Menu("Edit"); MenuItem cut = new MenuItem("Cut"); MenuItem copy = new MenuItem("Copy"); MenuItem paste = new MenuItem("Paste"); cut.addActionListener(new Cut()); E.add(cut); copy.addActionListener(new Copy()); E.add(copy); paste.addActionListener(new Paste()); E.add(paste); mb.add(E); setMenuBar(mb); mylistener mylist = new mylistener(); addWindowListener(mylist); } class mylistener extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } class New implements ActionListener { public void actionPerformed(ActionEvent e) { tx.setText(" "); setTitle(filename); } } class Open implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this, "select File",FileDialog.LOAD); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); ReadFile(); } tx.requestFocus(); } } class Save implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); try { DataOutputStream d = new DataOutputStream(new FileOutputStream(filename)); String line = tx.getText(); BufferedReader br = new BufferedReader(new StringReader(line)); while((line = br.readLine())!=null) { d.writeBytes(line + "\r\n"); d.close(); } } catch(Exception ex) { System.out.println("File not found"); } tx.requestFocus(); } } } class Exit implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } void ReadFile() { BufferedReader d; StringBuffer sb = new StringBuffer(); try { d = new BufferedReader(new FileReader(filename)); String line; while((line=d.readLine())!=null) sb.append(line + "\n"); tx.setText(sb.toString()); d.close(); } catch(FileNotFoundException fe) { System.out.println("File not Found"); } catch(IOException ioe){} } class Cut implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection ss = new StringSelection(sel); clip.setContents(ss,ss); tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd()); } } class Copy implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection clipString = new StringSelection(sel); clip.setContents(clipString,clipString); } } class Paste implements ActionListener { public void actionPerformed(ActionEvent e) { Transferable cliptran = clip.getContents(Editor.this); try { String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor); tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd()); } catch(Exception exc) { System.out.println("not string flavour"); } } } public static void main(String args[]) { Frame f = new Editor(); f.setSize(500,400); f.setVisible(true); f.show(); } }

Related Questions

How is interface instantiated in anonymous class for example new ActionListener?

They key word here is anonymous class. While ActionListener may be an interface, the anonymous class would be a subclass of ActionListener. It would be like creating a new class which implements ActionListener.JButton button = new JButton("Press Me!");button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ev) {System.out.println("PRESS");}});


Which methods are there in ActionListener interface?

The ActionListener interface has a single method.void actionPerformed(ActionEvent e) For full Java documentation see Oracle JavaDoc URL in related links below.


Is the ActionListener and MouseListener is same in java?

No, ActionListener and MouseListener are not the same in Java. ActionListener is used to handle action events, such as button clicks or menu selections, while MouseListener is specifically designed to handle mouse events, such as mouse clicks, presses, releases, and movements. Each listener serves a distinct purpose in responding to different types of user interactions in GUI applications.


How can you show a message hello on button click in java language?

I am assuming you want a dialog to pop up. 1) Write a class that implements ActionListener. Implement actionPerformed(ActionEvent). 2) In the actionPerformed() method, put "JOptionPane.showConfirmDialog(<the JFrame or JDialog or component you are using to display the button>, <the title you want>, <the text you want>);" 3) Call the method addActionListener(<your ActionListener-implementing class here>); on the JButton. 4) Display the button 5) Click the button to your heart's content. Did that answer your question?


How do you make login page with java in frame?

To create a login page in Java using a JFrame, you can utilize the Swing library. First, create a new JFrame and set its layout to a suitable manager, like GridLayout or FlowLayout. Add components such as JTextFields for username and password, JLabels for prompts, and a JButton for submission. Implement an ActionListener for the button to handle the login logic, validating user input accordingly.


What is the meaning of anonymous classes for events in java?

Anonymous classes, basically, have no name. It combines the class declaration and the creation of an instance of the class in one step. If an event handler is not shared by multiple components there is no need to declare a class to handle the event. The handler can be implemented with the use of an anonymous inner class. i.e. : button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "You clicked button1."); } });


Source code for notepad in java?

import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.io.*; public class Editor extends Frame { String filename; TextArea tx; Clipboard clip = getToolkit().getSystemClipboard(); Editor() { setLayout(new GridLayout(1,1)); tx = new TextArea(); add(tx); MenuBar mb = new MenuBar(); Menu F = new Menu("file"); MenuItem n = new MenuItem("New"); MenuItem o = new MenuItem("Open"); MenuItem s = new MenuItem("Save"); MenuItem e = new MenuItem("Exit"); n.addActionListener(new New()); F.add(n); o.addActionListener(new Open()); F.add(o); s.addActionListener(new Save()); F.add(s); e.addActionListener(new Exit()); F.add(e); mb.add(F); Menu E = new Menu("Edit"); MenuItem cut = new MenuItem("Cut"); MenuItem copy = new MenuItem("Copy"); MenuItem paste = new MenuItem("Paste"); cut.addActionListener(new Cut()); E.add(cut); copy.addActionListener(new Copy()); E.add(copy); paste.addActionListener(new Paste()); E.add(paste); mb.add(E); setMenuBar(mb); mylistener mylist = new mylistener(); addWindowListener(mylist); } class mylistener extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } class New implements ActionListener { public void actionPerformed(ActionEvent e) { tx.setText(" "); setTitle(filename); } } class Open implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this, "select File",FileDialog.LOAD); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); ReadFile(); } tx.requestFocus(); } } class Save implements ActionListener { public void actionPerformed(ActionEvent e) { FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE); fd.show(); if (fd.getFile()!=null) { filename = fd.getDirectory() + fd.getFile(); setTitle(filename); try { DataOutputStream d = new DataOutputStream(new FileOutputStream(filename)); String line = tx.getText(); BufferedReader br = new BufferedReader(new StringReader(line)); while((line = br.readLine())!=null) { d.writeBytes(line + "\r\n"); d.close(); } } catch(Exception ex) { System.out.println("File not found"); } tx.requestFocus(); } } } class Exit implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } void ReadFile() { BufferedReader d; StringBuffer sb = new StringBuffer(); try { d = new BufferedReader(new FileReader(filename)); String line; while((line=d.readLine())!=null) sb.append(line + "\n"); tx.setText(sb.toString()); d.close(); } catch(FileNotFoundException fe) { System.out.println("File not Found"); } catch(IOException ioe){} } class Cut implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection ss = new StringSelection(sel); clip.setContents(ss,ss); tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd()); } } class Copy implements ActionListener { public void actionPerformed(ActionEvent e) { String sel = tx.getSelectedText(); StringSelection clipString = new StringSelection(sel); clip.setContents(clipString,clipString); } } class Paste implements ActionListener { public void actionPerformed(ActionEvent e) { Transferable cliptran = clip.getContents(Editor.this); try { String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor); tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd()); } catch(Exception exc) { System.out.println("not string flavour"); } } } public static void main(String args[]) { Frame f = new Editor(); f.setSize(500,400); f.setVisible(true); f.show(); } }


How do you write a working key listener in java that responds to an 'enter'?

Almost all controls' ActionListener triggers the actionPerformed upon the enter key press == == Using an anonymous class implementation: Component c; // this is the component you want to add a listener to c.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { if(e.getKeyChar() == e.VK_ENTER) { // put the code you want to execute when Enter is pressed here System.out.println("ENTER PRESSED"); } } // unused abstract methods public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} });


A sample code for using password field in java?

We'll look at a simple program which displays a small window containing only a JPasswordField object. When you press the Enter key while editing the password field, we'll print out the password and then change what character is used to mask it. final JFrame frame = new JFrame("Password Test Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPasswordField passfield = new JPasswordField(); passfield.addActionListener(new ActionListener() { // When we press the Enter key, do some stuff to passfield... public final void actionPerformed(final ActionEvent ev) { // Print out the underlying password. System.out.println(new String(passfield.getPassword())); // Change the character used to mask the password. passfield.setEchoChar((char) (passfield.getEchoChar() + 1)); } }); frame.add(passfield); frame.setSize(300, 60); frame.setVisible(true);


Java program code for student Information?

import javax.swing.*; import java.awt.*; import java.awt.event.*; class jwp extends JFrame implements ActionListener{ JButton button = new JButton ("Submit"); JTextField field = new JTextField(); JLabel label = new JLabel("Enter your name : ",Label.RIGHT); public jwp(){ super("JFrame with panel and button"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(null); panel.add(label); panel.add(field); panel.add(button); setContentPane(panel); button.setBounds(100,70,100,50); label.setBounds(20,30,200,20); field.setBounds(120,30,100,20); button.addActionListener(this);} public void actionPerformed (ActionEvent e){ if (e.getSource() ==button){ JOptionPane.showMessageDialog(this,"Youre name was "+field.getText());} } } public class frame1 { public static void main (String[] args) { jwp start = new jwp(); start.setSize(300, 200); start.setVisible(true); } } that's alll.......i can't upgrade it anymore......


Is this code right for JAVA?

import java.awt.*;import java.Applet;import java.awt.Event.*;public class CircleDrawer extends Applet implements ActionListener{Button goButton;Button resetButton;TextField radiusField;TextField xposField;TextField yposField;public void init(){goButton = new Button("Generate!");resetButton = new Button("Reset");radiusField = new TextField("5",3);xposField = new TextField("0",2);xposField = new TextField("0",2);goButton.addActionListener(this);resetButton.addActionListener(this);setBackground(Color.black);}public void paint(Graphics g){g.setColor(Color.lime);g.drawLine(0,128,256,128);g.drawLine(128,64,128,256);g.drawArc(xposField.getInt()+128,yposField.getInt()+128,radiusField.getInt()*2,radiusField.getInt()*2,0,360);g.drawString("Radius:",0,48);add(radiusField);g.drawString("X:");add(xposField);g.drawString("Y:");add(yposField);add(goButton);add(resetButton);}public void actionPreformed(ActionEvent evt){if (evt.getSource() == goButton){repaint();}}}This is the code


Why do we need type casting in programming?

Becuase, sometimes data is in a form that is unrecognizable to the program. Example: int a = 2; int b = 3; double c = a / b; We cast c as a double. if it was not, then the data would not have a decimal value.