Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing JLabel thru Point

Status
Not open for further replies.

Pumzy

Programmer
Nov 20, 2003
13
JP
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
 
The javadocs:
Returns the x,y position of the event relative to the source component.
So I guess the source-component is the lable, and since it's small, x and y will be small, and a small x,y , relative to the panel, will mostly be the first label.

Doesn't:
Code:
public void mousePressed(MouseEvent m)
{
        lblDate.setBackground(new Color(100,250,80));
        lblDate.repaint();
}
work?

seeking a job as java-programmer in Berlin:
 
please remember in the future that this forum is meant for J2EE issues, not J2SE ( forum269 ).

Cheers


--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top