highlighting jpanel

hi....

i seem to be having a problem...my program allows me to display a number of images (on jpanels) in a row. but what i want is to hightlight those image panels when the mouse enters it or is selected. how can i go about doing that?

what i've done so far is:

publicvoid mouseEntered(MouseEvent e)

{

//Point p = e.getPoint();

//p.setLocation(p);

JPanel source = (JPanel)e.getSource();

source.setBackground(Color.RED);// this doesnt work since the image has taken all the space of the panel.

};

[773 byte] By [kk123a] at [2007-10-3 3:23:27]
# 1

You can try a couple of things. See what happens when you draw a border around the JPanel when the mouse moves over it

public void mouseEntered(MouseEvent e) {

JPanel source = (JPanel)e.getSource();

source.setBorder(BorderFactory.createLineBorder(Color.RED));

}

public void mouseExited(MouseEvent e) {

JPanel source=(JPanel)e.getSource();

source.setBorder(null);

}

If that doesn't look the way you want it to then you can get a bit fancier and do something like extending JPanel and override paintComponent to draw a red rectangle the same size as the JPanel after the image has been rendered. This would be conditional on some variable that is set by your mouselistener. Rough and dirty, not tested:

public class ImgPanel extends JPanel {

private boolean isSelected;

public void paintComponent(Graphics g) {

super.paintComponent(g);

if(isSelected) {

Graphics2D g2=(Graphics2D)g;

g2.setColor(Color.RED);

g2.setStroke(new BasicStroke(2));

g2.drawRect(0,0,getWidth(),getHeight);

}

}

public void setIsSelected(boolean b) {

isSelected=b;

repaint();

}

]

Not sure if it works so let us know how it turns out. Good luck!

- Travis

Message was edited by:

twb

twba at 2007-7-14 21:16:00 > top of Java-index,Security,Cryptography...
# 2
hey guys! thanx a lot! now it's working... just needed a slight modification to suit my taste ;-)really appreciated your time and assistance :-)
kk123a at 2007-7-14 21:16:00 > top of Java-index,Security,Cryptography...