MouseListener Help Java?

Any Component can have a MouseListener JLabel is nice for a colored rectangle, as long as you make it opaque.

Any Component can have a MouseListener. JLabel is nice for a colored rectangle, as long as you make it opaque. Addendum: Having recommended MouseAdapter elsewhere, I should mention that one instance is enough.

Addendum: This update adds the mouse listener in the ColorLabel constructor. Import java.awt. Color; import java.awt.

Dimension; import java.awt. EventQueue; import java.awt. GridLayout; import java.awt.event.

MouseAdapter; import java.awt.event. MouseEvent; import java.util. Random; import javax.swing.

JFrame; import javax.swing. JLabel; /** @see http://stackoverflow.com/questions/5136859 */ public class ColorLabel extends JLabel { private static final int N = 10; private static final Random random = new Random(); private static final MouseAdapter listener = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { ColorLabel label = (ColorLabel) e.getSource(); label. SetBackground(new Color(random.nextInt())); } }; public ColorLabel() { this.

SetOpaque(true); this. SetBackground(new Color(random.nextInt())); this. SetPreferredSize(new Dimension(32, 32)); this.

AddMouseListener(listener); } private void displayGrid() { JFrame f = new JFrame("ColorGrid"); f. SetLayout(new GridLayout(N, N)); f. SetDefaultCloseOperation(JFrame.

EXIT_ON_CLOSE); for (int I = 0; I SetVisible(true); } public static void main(String args) { EventQueue. InvokeLater(new Runnable() { @Override public void run() { new ColorLabel().displayGrid(); } }); } }.

This example was a really big help in giving me a better idea of how to do this, however, when executed this code provides a 10x10 grid of randomly colored squares. How would I edit it to display rectangles? – astroDude1 Feb 28 at 2:28 Try changing the label's preferred size.

– trashgod Feb 28 at 3:04.

Instead of having a JPanel that you draw your grid of colors on, how about you have a grid of buttons. You override the drawing mechanism for the button so that it just renders as it's current color. Then you have functionality built in to listen for clicks to occur in a specific section of your grid.

There's a related example here: stackoverflow. Com/questions/3420431 – trashgod Feb 28 at 0:47 Good example. Thanks!

– astroDude1 Feb 28 at 2:27.

This is what I came up with. Note: I'm still studying Java in University, so this might not be the exact way to do this but it worked when I did it. Public class ColorGrid extends JPanel implements MouseListener { this.

AddMouseListener(this); addMouseListener(this); That's the first part, the second part is to have these methods in your code. Public void mouseClicked(MouseEvent arg0) { } public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { } Then, depending on what you want, (i.e. Mouse clicked or pressed), just type in: repaint(); Hope this helped.

1 Using MouseAdapter may simplify your code. – trashgod Feb 28 at 0:46 Very helpful info. Thank you!

– astroDude1 Feb 28 at 2:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions