I'm trying to add a mouseListener to each Jlabel I create then pass them onto paintComponent.
private List<JLabel> createTiles (){
int x = 10;
int y = 0;
List<JLabel> tiles = new ArrayList<>();
//Number of tiles painted
for (Integer i : gameModelHandle.numbersToDigits()) {
JLabel tile = new JLabel("" + i, JLabel.CENTER);
tile.setSize(new Dimension(140, 140));
tile.setFont(tile.getFont().deriveFont(45f));
tile.setLocation(x, y);
tile.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
GameModel.currentSum(i);
changeColour();
}
@Override
public void mouseReleased(MouseEvent e) {
if (getBounds().intersects(tile.getBounds())){
int number = gameModelHandle.combineDigits(i, Integer.parseInt(tile.getText()));
GameModel.currentSum(number);
}
}
});
tiles.add(tile);
x+=150;
}
return tiles;
}
As you can see, the number of tiles is never the same which is why I can't create them manually; I have a loop for that.
I paint them like so :
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.removeAll();
for (JLabel i : createTiles()) {
g.setColor(tileColours[activeColourIndex]);
g.fillRoundRect(i.getX(), i.getY(), i.getWidth(), i.getHeight(), 20, 20);
this.add(i);
}
revalidate();
}
And it works, the problem is that my mouseListeners don't seem to work at all. I tried debugging and never got to the mouseClick method.
Any input or guidance would be appreciated.
You don't need to create JLabel
s inside your paintComponent
over and over. Create it once and keep reusing the list of labels for updating them. I was able to create a working example and it seems to be working fine.
The mouse handler is working too.
Here is what I have. Take a look. I had to remove unnecessary parts and tweak the code for compiling and getting it up and running.
public class PaintComponentMouseListener extends JPanel {
private static final long serialVersionUID = -4235693652966961963L;
private List<JLabel> tiles;
private List<JLabel> createTiles() {
if(tiles == null) {
tiles = new ArrayList<>();
int x = 10;
int y = 0;
// Number of tiles painted
for(int i=1;i<=9;i++) {
String btn = "" + i;
JLabel tile = new JLabel(btn, JLabel.CENTER);
tile.setSize(new Dimension(140, 140));
tile.setFont(tile.getFont().deriveFont(45f));
tile.setLocation(x, y);
tile.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked on button: " + btn);
}
});
tiles.add(tile);
x += 150;
}
}
return tiles;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.removeAll();
for (JLabel i : createTiles()) {
g.setColor(Color.CYAN);
g.fillRoundRect(i.getX(), i.getY(), i.getWidth(), i.getHeight(), 20, 20);
this.add(i);
}
revalidate();
}
private void start() {
JFrame frame = new JFrame("Hello");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3, 3));
frame.setContentPane(this);
frame.setSize(500, 500);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(()->{
new PaintComponentMouseListener().start();
});
}
}
Hope this helps!