0

Mouse event appears not to work, and i can't find out, why.
I added a debug output at imgEdit.drawDot and there's no output at the console. I'm a newbie in java, so my code may seem to be very bad, as well as my english

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/**
 * Created by doctor on 12/29/15.
 */
public class MainUI {
 Window mainWindow;
 MainUI() {
 mainWindow = new Window();
 }
}
class Window extends JFrame {
 Window() {
 setBounds(0, 0, 600, 400);
 setTitle("RebBrush");
 Panel mainPanel = new Panel();
 Container mainCont = getContentPane();
 mainCont.setLayout(null);
 mainCont.add(mainPanel);
 setVisible(true);
 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 }
}
class Panel extends JPanel {
 private ImageEdit imgEdit;
 private JLabel imgLabel;
 Panel() {
 setLayout(null);
 imgEdit = new ImageEdit(600, 400);
 imgLabel = new JLabel(new ImageIcon(imgEdit.getImage()));
 imgLabel.setBounds(0, 0, 600, 400);
 add(imgLabel);
 addMouseMotionListener(new MouseMotionListener() {
 @Override
 public void mouseDragged(MouseEvent e) {
 imgEdit.drawDot(e.getX(), e.getY());
 }
 @Override
 public void mouseMoved(MouseEvent e) {
 }
 });
 }
}
Mshnik
7,0621 gold badge27 silver badges38 bronze badges
asked Dec 29, 2015 at 15:45
6
  • mouseDragged is when the button is kept clicked and the moved like a selection area. Commented Dec 29, 2015 at 15:49
  • Are you sure that changing the image which you used to create the ImageIcon will change anything on the screen? Commented Dec 29, 2015 at 15:49
  • There's a debug output there, and that code area isn't even executed Commented Dec 29, 2015 at 15:51
  • Shouldn't the MouseMotionListener be on the imgLabel ? Commented Dec 29, 2015 at 15:56
  • do you see the mainPanel ?you are adding mainPanel to null layout and you haven't set bounds for it.use layout managers . set color to main panel and see where is it .mainPanel.setBackground(Color.yellow); Commented Dec 29, 2015 at 15:56

1 Answer 1

1

Simply getting rid of the null layouts did the trick for me. I'm not sure what ImageEdit is (some other class you've defined?), but by running the following I see "Mouse Dragged" show up in the console, so the mouseDragged method is definitely being called. Just uncomment the imageEdit stuff to put it back in.

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/**
 * Created by doctor on 12/29/15.
 */
public class MainUI {
 Window mainWindow;
 MainUI() {
 mainWindow = new Window();
 }
 public static void main(String[] args) {
 new MainUI();
 }
}
class Window extends JFrame {
 Window() {
 setBounds(0, 0, 600, 400);
 setTitle("RebBrush");
 Panel mainPanel = new Panel();
 Container mainCont = getContentPane();
 mainCont.add(mainPanel);
 setVisible(true);
 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 }
}
class Panel extends JPanel {
 //private ImageEdit imgEdit;
 private JLabel imgLabel;
 Panel() {
 //imgEdit = new ImageEdit(600, 400);
 //imgLabel = new JLabel(new ImageIcon(imgEdit.getImage()));
 //imgLabel.setBounds(0, 0, 600, 400);
 //add(imgLabel);
 addMouseMotionListener(new MouseMotionListener() {
 @Override
 public void mouseDragged(MouseEvent e) {
 System.out.println("Mouse Dragged");
 //imgEdit.drawDot(e.getX(), e.getY());
 }
 @Override
 public void mouseMoved(MouseEvent e) {
 }
 });
 }
}
answered Dec 29, 2015 at 16:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.