MouseListener & MouseMotionListener
Hey,
I'm new at Java and have never programmed before but recently a friend of mine inspired me so I'm really interested and willing to learn. I have been looking at java for 2 weeks or so now.
Anyways, my question is:
Do the mouse events have to be used with a JButton or something like a JLabel?
For example; could I just have a plain background and still handle events? i.e. if(event.getSource()==COORDINATES);
Can't really explain it to well myself lol but in simple terms, can I handle an event when the mouse enters/clicks/drags on a COORDINATION?
Thanks in advance
Re: MouseListener & MouseMotionListener
You can use mouse event with any object that allows you to add the listener (for example anything that extends Component - JPanel, JButton, etc...) I'm not sure what you are asking about with regards to COORDINATION, but for a plain background you can use a JPanel without anything added or drawn, add the listener, and deal with mouse events appropriately.
Re: MouseListener & MouseMotionListener
Quote:
Originally Posted by
copeg
You can use mouse event with any object that allows you to add the listener (for example anything that extends Component - JPanel, JButton, etc...) I'm not sure what you are asking about with regards to COORDINATION, but for a plain background you can use a JPanel without anything added or drawn, add the listener, and deal with mouse events appropriately.
Thanks for the reply,
Basically because I only ever used FlowLayout(); I'm not sure on how to use any other layout or even JPanel, i know how to create one etc but i don't quite understand it which is why I just use FlowLayout();. I also don't know how to create my OWN buttons or labels, i just use JButton or JLabel etc.
I found a way of adding a background image to my window and it doesn't require FlowLayou(); so I don't know how to add JButtons to it etc. So basically what I was asking was, could I create a background picture and add stuff like BUTTON1, BUTTON2 as TEXT ON MY background picture then find the coordinates on the background picture of where i want to handle the events?
Do you know what i mean?
Re: MouseListener & MouseMotionListener
Yep. Create your own JPanel and over-ride it's paintComponent method. This will allow you to draw anything you want onto that JPanel. You can still add other components (such as buttons and labels) onto that JPanel, and their ActionListeners/MouseListeners are un-effected by the custom painting.
Code :
// example JPanel with a background image, and some buttons
public class BackgroundPanel extends JPanel implements ActionListener
{
private Image background;
private JButton button;
public BackgroundImage(Image background)
{
this.background = background;
this.button = new JButton("Push me!");
this.button.addActionListener(this);
}
public void paintComponent(Graphics g)
{
g.drawImage(background, 0, 0, Color.white, this);
}
public void ActionPerformed(ActionEvent e)
{
if (e.getSource() == this.button)
{
JOptionPane.showMessageDialog(this, "You pushed the button!");
}
}
}
Re: MouseListener & MouseMotionListener
Quote:
Originally Posted by
helloworld922
Yep. Create your own JPanel and over-ride it's paintComponent method. This will allow you to draw anything you want onto that JPanel. You can still add other components (such as buttons and labels) onto that JPanel, and their ActionListeners/MouseListeners are un-effected by the custom painting.
Code :
// example JPanel with a background image, and some buttons
public class BackgroundPanel extends JPanel implements ActionListener
{
private Image background;
private JButton button;
public BackgroundImage(Image background)
{
this.background = background;
this.button = new JButton("Push me!");
this.button.addActionListener(this);
}
public void paintComponent(Graphics g)
{
g.drawImage(background, 0, 0, Color.white, this);
}
public void ActionPerformed(ActionEvent e)
{
if (e.getSource() == this.button)
{
JOptionPane.showMessageDialog(this, "You pushed the button!");
}
}
}
Thanks a lot, I really appreciate it. But what I really need to know is, if I can make any random space on my background picture CLICKABLE? so it handles an event when I click it.
Re: MouseListener & MouseMotionListener
Add a MouseListener to the JPanel and check the coordinates from MouseEvent#getPoint().
That said, this sounds like a bad approach. Why do you think you need to do that?
db
Re: MouseListener & MouseMotionListener
Quote:
Originally Posted by
Darryl.Burke
Add a MouseListener to the JPanel and check the coordinates from MouseEvent#getPoint().
That said, this sounds like a bad approach. Why do you think you need to do that?
db
This is what I mean finally someone understood me :P
Basically, I only ever used FlowLayout and I don't know how to add my own background image to it. I sort of know how to do it on a JPanel but can't add JButtons to it etc. So I wondered if I could just draw out the buttons on a background image and make stuff clickable just by the coords. :P
Re: MouseListener & MouseMotionListener
As helloworld922 has already told you at #4, performing custom painting doesn't preclude adding components to the panel.
Maybe you're trying to run before you've learned to walk. I recommend the Swing tutorials on the Sun site (link on next line)
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
db
Re: MouseListener & MouseMotionListener
Quote:
Originally Posted by
Darryl.Burke
As helloworld922 has already told you at #4, performing custom painting doesn't preclude adding components to the panel.
Maybe you're trying to run before you've learned to walk. I recommend the Swing tutorials on the Sun site (link on next line)
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
db
Thanks for the useful links (Y)
Re: MouseListener & MouseMotionListener
There are no such things to be used while you are on swing.As I have seen and used Jlabels ,they can be easily updated using the Action listeners and action events.