001: package snow.utils.gui;
002:
003: /**
004: * A JLabel, which has the same foreground color like a textfield,
005: * and which has a background color, which is the average
006: * of Label.background and TextField.background.
007: *
008: * This means a better contrast, but still a small visual difference
009: * between TextFields and JContrastLabels, ..depending on the set theme.
010: */
011:
012: import java.awt.*;
013: import javax.swing.*;
014:
015: public class JContrastLabel extends JLabel {
016:
017: private boolean readyForSpecialUpdates = false;
018:
019: // Needed, as some updateUI() calls are too early
020: // for the special UI updates, cause some components
021: // could not be existing yet.
022:
023: public JContrastLabel(String text, Icon icon,
024: int horizontalAlignment) {
025: super (text, icon, horizontalAlignment);
026: EventQueue.invokeLater(new Runnable() {
027: public void run() {
028: readyForSpecialUpdates = true;
029: }
030: });
031: this .updateSpecialUI();
032: }
033:
034: public JContrastLabel(String text, int horizontalAlignment) {
035: super (text, null, horizontalAlignment);
036: EventQueue.invokeLater(new Runnable() {
037: public void run() {
038: readyForSpecialUpdates = true;
039: }
040: });
041: this .updateSpecialUI();
042: }
043:
044: public JContrastLabel(String text) {
045: super (text, null, LEADING);
046: EventQueue.invokeLater(new Runnable() {
047: public void run() {
048: readyForSpecialUpdates = true;
049: }
050: });
051: this .updateSpecialUI();
052: }
053:
054: public JContrastLabel(Icon image, int horizontalAlignment) {
055: super (null, image, horizontalAlignment);
056: EventQueue.invokeLater(new Runnable() {
057: public void run() {
058: readyForSpecialUpdates = true;
059: }
060: });
061: this .updateSpecialUI();
062: }
063:
064: public JContrastLabel(Icon image) {
065: super (null, image, CENTER);
066: EventQueue.invokeLater(new Runnable() {
067: public void run() {
068: readyForSpecialUpdates = true;
069: }
070: });
071: this .updateSpecialUI();
072: }
073:
074: public JContrastLabel() {
075: super ("", null, LEADING);
076: EventQueue.invokeLater(new Runnable() {
077: public void run() {
078: readyForSpecialUpdates = true;
079: }
080: });
081: this .updateSpecialUI();
082: }
083:
084: public void updateUI() {
085: super .updateUI();
086: if (readyForSpecialUpdates) {
087: this .updateSpecialUI();
088: }
089: }
090:
091: public void updateSpecialUI() {
092: this .setForeground(UIManager.getColor("TextField.foreground"));
093: final Color bg1 = UIManager.getColor("TextField.background");
094: final Color bg2 = UIManager.getColor("Label.background");
095: final int r = (bg1.getRed() + bg2.getRed()) / 2;
096: final int g = (bg1.getGreen() + bg2.getGreen()) / 2;
097: final int b = (bg1.getBlue() + bg2.getBlue()) / 2;
098: this .setBackground(new Color(r, g, b));
099: }
100:
101: } // JContrastLabel
|