1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
package com.explodingpixels.border;
import com.explodingpixels.widgets.WindowUtils;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
public class FocusStateMatteBorder extends MatteBorder {
private final Color fFocusedColor;
private final Color fUnfocusedColor;
private final JComponent fComponentToTrackFocusOf;
public FocusStateMatteBorder(int top, int left, int bottom, int right,
Color focusedColor, Color unfocusedColor,
JComponent componentToTrackFocusOf) {
super(top, left, bottom, right, focusedColor);
fFocusedColor = focusedColor;
fUnfocusedColor = unfocusedColor;
fComponentToTrackFocusOf = componentToTrackFocusOf;
updateColor(true);
WindowUtils.installWeakWindowFocusListener(fComponentToTrackFocusOf, createWindowFocusListener());
}
private void updateColor(boolean focused) {
color = focused ? fFocusedColor : fUnfocusedColor;
fComponentToTrackFocusOf.repaint();
}
private WindowFocusListener createWindowFocusListener() {
return new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
updateColor(true);
}
public void windowLostFocus(WindowEvent e) {
updateColor(false);
}
};
}
}
|