-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDisabledGlassPane.java
More file actions
113 lines (96 loc) · 2.77 KB
/
DisabledGlassPane.java
File metadata and controls
113 lines (96 loc) · 2.77 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
/*
* Simple implementation of a Glass Pane that will capture and ignore all
* events as well paint the glass pane to give the frame a "disabled" look.
*
* The background color of the glass pane should use a color with an
* alpha value to create the disabled look.
*/
public class DisabledGlassPane extends JComponent
implements KeyListener
{
private final static Border MESSAGE_BORDER = new EmptyBorder(10, 10, 10, 10);
private JLabel message = new JLabel();
public DisabledGlassPane()
{
// Set glass pane properties
setOpaque( false );
Color base = UIManager.getColor("inactiveCaptionBorder");
Color background = new Color(base.getRed(), base.getGreen(), base.getBlue(), 128);
setBackground( background );
setLayout( new GridBagLayout() );
// Add a message label to the glass pane
add(message, new GridBagConstraints());
message.setOpaque(true);
message.setBorder(MESSAGE_BORDER);
// Disable Mouse, Key and Focus events for the glass pane
addMouseListener( new MouseAdapter() {} );
addMouseMotionListener( new MouseMotionAdapter() {} );
addKeyListener( this );
setFocusTraversalKeysEnabled(false);
}
/*
* The component is transparent but we want to paint the background
* to give it the disabled look.
*/
@Override
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getSize().width, getSize().height);
}
/*
* The background color of the message label will be the same as the
* background of the glass pane without the alpha value
*/
@Override
public void setBackground(Color background)
{
super.setBackground( background );
Color messageBackground = new Color(background.getRGB());
message.setBackground( messageBackground );
}
//
// Implement the KeyListener to consume events
//
public void keyPressed(KeyEvent e)
{
e.consume();
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e)
{
e.consume();
}
/*
* Make the glass pane visible and change the cursor to the wait cursor
*
* A message can be displayed and it will be centered on the frame.
*/
public void activate(String text)
{
if (text != null && text.length() > 0)
{
message.setVisible( true );
message.setText( text );
message.setForeground( getForeground() );
}
else
message.setVisible( false );
setVisible( true );
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
requestFocusInWindow();
}
/*
* Hide the glass pane and restore the cursor
*/
public void deactivate()
{
setCursor(null);
setVisible( false );
}
}