I was trying to change the text "Java" to "Love Java" by using MouseAdapter. The text "Java" comes out when mouseEntered and "Love Java" comes out when mouseExited. So I created class MyMouse extends MouseAdapter and trying to use the methods.
But there is the error message "The method getSource() is undefined for the type MouseEvent". When I searched it, this code is nothing wrong with it but I don't know why I'm getting this error message, and also of course the result is not working.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseEvent extends JFrame{
MouseEvent(){
setTitle("Practicing mouse event");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lb = new JLabel("Java");
MyMouse mym = new MyMouse();
lb.addMouseListener(mym);
lb.setSize(300,100);
add(lb);
setSize(400,200);
setVisible(true);
}
public static void main(String[] args) {
new MouseEvent();
}
}
class MyMouse extends MouseAdapter{
public void mouseEntered(MouseEvent e) {
JLabel lb = (JLabel)e.getSource();
lb.setText("Love Java");
}
public void mouseExited(MouseEvent e) {
JLabel lb = (JLabel)e.getSource();
lb.setText("Java");
}
}
The MouseEvent object e you are using inside public void mouseEntered(MouseEvent e) or public void mouseExited(MouseEvent e) should be from java.awt.event.MouseEvent.
But, as you have named your main class also as MouseEvent, in that case the MouseEvent object e inside mouseEntered() and mouseExited() methods are actually the object of your main class. That's why it was searching for getSource() method in your main class and failed.
Please change your main class name to something other than MouseEvent. For example:
public class MyMouseEvent extends JFrame {
MyMouseEvent() {
...
...
public static void main(String[] args) {
new MyMouseEvent();
...
The problem can be solved through these changes to the code:
Rename the main class of the program so that its name does not conflict with that of the class (java.awt.event.MouseEvent) imported from Java library.
Add #Override annotations to mouseEntered and mouseExited methods to avoid compiler warnings
Call setVisible(true) on the frame to display after instantiation
Here is the working example with the above mentioned changes:
// File name: Demo.java
// This name was chosen to avoid conflict with java.awt.event.MouseEvent
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyMouse extends MouseAdapter{
// Add #Override annotation to overridden methods
#Override
public void mouseEntered(MouseEvent e) {
JLabel lb = (JLabel)e.getSource();
lb.setText("Love Java");
}
// Add #Override annotation to overridden methods
#Override
public void mouseExited(MouseEvent e) {
JLabel lb = (JLabel)e.getSource();
lb.setText("Java");
}
}
public class Demo extends JFrame{
Demo() {
setTitle("Practicing mouse event");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lb = new JLabel("Java");
MyMouse mym = new MyMouse();
lb.addMouseListener(mym);
lb.setSize(300,100);
add(lb);
setSize(400,200);
}
public static void main(String[] args) {
Demo demo = new Demo();
// Display the frame
demo.setVisible(true);
}
}
Output:
> javac Demo.java
> java Demo
Related
my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Events1 extends JFrame {
private JLabel label;
private JButton button;
public Events1() {
setLayout(new FlowLayout());
label = new JLabel("");
button = new JButton("Click for text");
add(button);
add(label);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerfomed(ActionEvent e) {
label.setText("See motherfucker it does do stuff");
}
}
public static void main(String[] args) {
Events1 window = new Events1();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500, 500); //.pack();
window.setVisible(true);
window.setTitle("Attempt 2");
}
}
Basically I'm new to GUI's and get the error message when I try to compile the above code:
Events1.java:25: error: Events1.event is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class event implements ActionListener {
^
1 error
I basically made this code based on the information on the Oracle Docs and and pretty confused of why this doesn't work/how to fix it.
Any help is greatly appreciated thanks.
You have a typo in overriden method
public void actionPerformed(ActionEvent e)
That's why you should use #Override annotation for overriden methods and IDE support for this kind of operations.
The frame opens and close normally but mouse click doesn't work.
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Create a frame window that responds to mouse click
public class AWT3 extends Frame {
String Mmsg="";
int mouseX=0, mouseY=0;
public AWT3() {
addWindowListener(new MyWindowwAdapter(this));
addMouseListener(new MyMouseeAdapter(this));
}
public void paint(Graphics g){
g.drawString(Mmsg, mouseX, mouseY);
}
public static void main(String args[]){
AWT3 awt3 = new AWT3();
awt3.setSize(new dimension(500, 500));
awt3.setTitle("Window framee");
awt3.setVisible(true);
}
}
class MyWindowwAdapter extends WindowAdapter{
AWT3 awt3;
public MyWindowwAdapter(AWT3 awt3) {
this.awt3=awt3;
}
public void windowClosing(WindowEvent we){
awt3.setVisible(false);
}
}
class MyMouseeAdapter extends MouseAdapter{
AWT3 awt3;
public MyMouseeAdapter(AWT3 awt3) {
this.awt3=awt3;
}
public void MouseClicked(MouseEvent me){
awt3.Mmsg="the mouse is clicked";
awt3.mouseX= me.getX();
awt3.mouseY=me.getY();``
awt3.repaint();
}
}
From what it looks like, this code won't compile. You have an error that you need to fix:
awt3.setSize(new dimension(500, 500));
to
awt3.setSize(new Dimension(500, 500));
and add the proper import java.awt.Dimension as pointed out by others.
Another mistake is that MouseClicked(MouseEvent me) is not overriding the super class method from MouseAdapter as its syntactically wrong (super class method starts with small case). Change it to mouseClicked(MouseEvent me) (add the optional #Override annotation if you wish).
The method name should be public void mouseClicked(MouseEvent me)
instead of public void MouseClicked(MouseEvent me).
mouseClicked() is when the mouse button has been pressed and released.
mousePressed() is when the mouse button has been pressed.
Your code is working. tested on java 1.7. only the problem I saw, was with out importing the java.awt.Dimension class you are trying to create a new dimension(500, 500); although the class name is in simple form you can fix this error and try the code.
I have sticked below listener inside JtextField when action performed event,
so as to perform action against any change made in text box once user make any change.
but the problem is the code not starting or working unless you press enter only then
the code execute the code, i need to know what i have to add and where to enable below code once text Filed changed instantly .I can see some similar help referring to Oracle listener help but am unable to manage so i need direct simple way.
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
JOptionPane.showMessageDialog(null, "Change case");
}
public void insertUpdate(DocumentEvent de) {
JOptionPane.showMessageDialog(null, "Update Case");
}
public void removeUpdate(DocumentEvent de) {
JOptionPane.showMessageDialog(null, "Remove case");
}
});
// TODO add your handling code here:
}
ActionListener for a text field only listens for enter key being typed. So what your code essentialy does is: when enter key is pressed adds a new DocumentListener to the text field.
The DocumentListener is what you want so take that code (adding the document listener) out of the jTextField1ActionPerformed method and put it in the constructor of the class. Or have a private method, so as not to clutter the constructor.
Assuming you are using Netbeans GUI editor (looks like it from your method signature):
public class MyFrame exentds JFrame {
public MyFrame() {
initComponents();
addDocumentListenerToField();
}
private void addDocumentListenerToField() {
jTextField.getDocument().addDocumentListener(..);
}
}
UPDATE: DEMO
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class DocListeenerDemo extends JFrame {
private JTextField field;
private JLabel label;
public DocListeenerDemo() {
initComponents();
addDocumentListenerToField();
}
private void initComponents() {
setLayout(new GridLayout(0, 1));
field = new JTextField(20);
label = new JLabel("", SwingConstants.CENTER);
add(field);
add(label);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationByPlatform(true);
}
private void addDocumentListenerToField() {
field.getDocument().addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent arg0) {doYourStuff();}
public void insertUpdate(DocumentEvent arg0) {doYourStuff();}
public void removeUpdate(DocumentEvent arg0) {doYourStuff();}
public void doYourStuff() {
label.setText(field.getText());
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
DocListeenerDemo demo = new DocListeenerDemo();
demo.setVisible(true);
}
});
}
}
I have not figure out a way to add the DocumentListener through the GUI tool. Sucks.
try using actionListener rather than documentListener
I am coding one applet that will show the number of mouse clicks on a button, and another that will show the coordinates of a mouse click and change the background color to a random color.
In both cases, I encountered the "is not abstract and does not override abstract method" error in BlueJ, and I need help understanding what I am doing wrong. I'm very new to this, so any tips/proofreadings are welcome as well :)
here's the first applet:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Option1 extends Frame implements ActionListener {
public void main (String[] args) {
int click = 0;
JFrame base = new JFrame ("Button Click Counter");
base.getContentPane().setLayout(null);
base.setSize(500,500);
base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me!");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed (ActionEvent e) {
click++; }
});
JTextField count = new JTextField(click);
this.add(button);
this.add(count);
}}
and the coordinates one:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class Option2 extends Frame implements MouseListener {
double x;
double y;
public void init() {
addMouseListener(this);
JFrame base = new JFrame("Mouse Coordinates");
base.getContentPane().setLayout(null);
base.setSize(500,500);
base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField answer = new JTextField(x + "," + y);
}
public void mouseClicked( MouseEvent e ) {
x = e.getX();
y = e.getY();
this.setBackground(new Color((int)(Math.random() * 0x1000000)));
}}
Thanks in advance!
Option1 declares to implement the ActionListener interface:
public class Option1 extends Frame implements ActionListener {
If a class does so, it must be either abstract (which Option1 is not) or it must implement the methods declared in the interface. When you take a look at the API for ActionListener, you'll find one method there:
void actionPerformed(ActionEvent e)
You need to implement this method in Option1, e.g.
#Override
public void actionPerformed(ActionEvent e) {
click++;
}
and then register Option1 as ActionListener:
button.addActionListener(this);
The thing you tried is to implement the ClickListener in an anonymous inner class, which is also ok, but in this case Option1 must not implement ActionListener.
Your main method in Option1 is not declared properly, a main method usually is
public static void main(String[] args)
But the initialization you do in the main method should be done in the constructor or (as with Option2) in a separate method
public class Option1 extends Frame implements ActionListener {
private int click = 0;
public Option1() {
JFrame base = new JFrame("Button Click Counter");
base.getContentPane().setLayout(null);
base.setSize(500, 500);
base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me!");
button.addActionListener(this);
JTextField count = new JTextField(click);
this.add(button);
this.add(count);
}
#Override
public void actionPerformed(ActionEvent e) {
click++;
}
}
The problem with Option2 is that you only implement the method mouseClicked from the interface MouseListener. There are a couple others that need to be implemented:
void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)
Even if you do not want to handle these events, you need to have the empty methods.
A final tip: I don't know your IDE, but many IDEs (Eclipse, Netbeans, IntelliJ) have an option to generate the required methods for an interface for you, which saves a lot of typing ;)
you're implementing an interface...
at least you pronouce you do it when you write
public class Option1 extends Frame implements ActionListener
but you don't keep your contract! if you say you implement ActionListener, then you guaranteed that you have this method
public void actionPerformed(ActionEvent e) {
}
but your code lacks this method - and that's your error message...
I have a JComponent, and I want it do preform a piece of code when the component is pressed. Can someone help me?
JComponent comp = new JPanel();
comp.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
// Place your code here
}
});
Assuming you have a Class that extends JComponent
import java.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyClass extends JComponent{
public MyClass(){
//Other code
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
// Place your code here
}
});
//Other code
}
}