Hello! I am making a bean (by the way I am newbie in java), sort of like a calendar, and I use JLabel inside a for loop to make objects for the dates. The problem I am encountering is I cannot get the proper object I clicked to set the background color (mousePressed, mouseReleased).
I always get the first JLabel added in the panelDate.
Can anybody help me... anyway here is the code from my class constructor... thanks!
panelDate.setLayout(new GridLayout(6,7));
for (int x=1;x<=42;x++)
{
lblDate = new JLabel("" + x, JLabel.CENTER);
lblDate.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.GREEN));
lblDate.setBackground(Color.WHITE);
lblDate.setOpaque(true);
lblDate.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
Point p = m.getPoint();
panelDate.getComponentAt(p).setBackground(new Color(100,250,80));
panelDate.getComponentAt(p).repaint();
}
public void mouseReleased(MouseEvent m)
{
Point p = m.getPoint();
panelDate.getComponentAt(p).setBackground(Color.WHITE);
panelDate.getComponentAt(p).repaint();
}
});
panelDate.add(lblDate);
}
getContentPane().add(panelDate);
pack();
setSize(200,200);
setVisible(true);
KDjLogan
I always get the first JLabel added in the panelDate.
Can anybody help me... anyway here is the code from my class constructor... thanks!
panelDate.setLayout(new GridLayout(6,7));
for (int x=1;x<=42;x++)
{
lblDate = new JLabel("" + x, JLabel.CENTER);
lblDate.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.GREEN));
lblDate.setBackground(Color.WHITE);
lblDate.setOpaque(true);
lblDate.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
Point p = m.getPoint();
panelDate.getComponentAt(p).setBackground(new Color(100,250,80));
panelDate.getComponentAt(p).repaint();
}
public void mouseReleased(MouseEvent m)
{
Point p = m.getPoint();
panelDate.getComponentAt(p).setBackground(Color.WHITE);
panelDate.getComponentAt(p).repaint();
}
});
panelDate.add(lblDate);
}
getContentPane().add(panelDate);
pack();
setSize(200,200);
setVisible(true);
KDjLogan