KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > JContrastLabel


1 package snow.utils.gui;
2
3
4
5  /**
6   * A JLabel, which has the same foreground color like a textfield,
7   * and which has a background color, which is the average
8   * of Label.background and TextField.background.
9   *
10   * This means a better contrast, but still a small visual difference
11   * between TextFields and JContrastLabels, ..depending on the set theme.
12   */

13
14 import java.awt.*;
15 import javax.swing.*;
16
17
18 public class JContrastLabel extends JLabel
19 {
20
21
22   private boolean readyForSpecialUpdates = false;
23   // Needed, as some updateUI() calls are too early
24
// for the special UI updates, cause some components
25
// could not be existing yet.
26

27
28   public JContrastLabel(String JavaDoc text, Icon icon, int horizontalAlignment)
29   {
30     super(text,icon,horizontalAlignment);
31     EventQueue.invokeLater( new Runnable JavaDoc()
32      {
33         public void run()
34         {
35           readyForSpecialUpdates = true;
36         }
37      });
38     this.updateSpecialUI();
39   }
40
41   public JContrastLabel(String JavaDoc text, int horizontalAlignment)
42   {
43     super(text, null, horizontalAlignment);
44     EventQueue.invokeLater( new Runnable JavaDoc()
45      {
46         public void run()
47         {
48           readyForSpecialUpdates = true;
49         }
50      });
51     this.updateSpecialUI();
52   }
53
54   public JContrastLabel(String JavaDoc text)
55   {
56     super(text, null, LEADING);
57     EventQueue.invokeLater( new Runnable JavaDoc()
58      {
59         public void run()
60         {
61           readyForSpecialUpdates = true;
62         }
63      });
64     this.updateSpecialUI();
65   }
66
67   public JContrastLabel(Icon image, int horizontalAlignment)
68   {
69     super(null, image, horizontalAlignment);
70     EventQueue.invokeLater( new Runnable JavaDoc()
71      {
72         public void run()
73         {
74           readyForSpecialUpdates = true;
75         }
76      });
77     this.updateSpecialUI();
78   }
79
80   public JContrastLabel(Icon image)
81   {
82     super(null, image, CENTER);
83     EventQueue.invokeLater( new Runnable JavaDoc()
84      {
85         public void run()
86         {
87           readyForSpecialUpdates = true;
88         }
89      });
90     this.updateSpecialUI();
91   }
92
93   public JContrastLabel()
94   {
95     super("", null, LEADING);
96     EventQueue.invokeLater( new Runnable JavaDoc()
97      {
98         public void run()
99         {
100           readyForSpecialUpdates = true;
101         }
102      });
103     this.updateSpecialUI();
104   }
105
106
107
108   public void updateUI()
109   {
110     super.updateUI();
111     if( readyForSpecialUpdates )
112      {
113        this.updateSpecialUI();
114      }
115   }
116
117
118
119   public void updateSpecialUI()
120   {
121     this.setForeground( UIManager.getColor("TextField.foreground"));
122     final Color bg1 = UIManager.getColor("TextField.background");
123     final Color bg2 = UIManager.getColor("Label.background");
124     final int r = (bg1.getRed() + bg2.getRed())/2;
125     final int g = (bg1.getGreen() + bg2.getGreen())/2;
126     final int b = (bg1.getBlue() + bg2.getBlue())/2;
127     this.setBackground( new Color(r,g,b) );
128   }
129
130
131
132 } // JContrastLabel
Popular Tags