-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleStatusBar.java
More file actions
292 lines (245 loc) · 8.54 KB
/
Copy pathSimpleStatusBar.java
File metadata and controls
292 lines (245 loc) · 8.54 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* ====================================================================
* Simple StatusBar
* ====================================================================
*
* Description :
* A Swing graphical application displaying a simple window with:
* - a custom status bar;
* - a button to display the time of a user action;
* - a button to close the application.
*
* The status bar is composed of two areas:
* - a left area containing status messages;
* - a right area containing additional information,
* separated by graphic separators.
*
* ====================================================================
*/
//import java.awt.event.WindowListener;
//import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
//import java.util.Calendar;
//import java.util.Date;
//import java.text.SimpleDateFormat;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.net.URL;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class SimpleStatusBar
{
/*****************************
* CUSTOM PRIVATE PROPERTIES *
****************************/
private static final int FRAME_SIZE_X = 600;
private static final int FRAME_SIZE_Y = 500;
private static final int btnShow_LOCATION_X = 420;
private static final int btnShow_LOCATION_Y = 15;
private static final int btnShow_SIZE_X = 140;
private static final int btnShow_SIZE_Y = 40;
private static final int btnQuit_LOCATION_X = 420;
private static final int btnQuit_LOCATION_Y = 70;
private static final int btnQuit_SIZE_X = 140;
private static final int btnQuit_SIZE_Y = 40;
/*************************************
* JStatusBar - METHODS & PROPERTIES *
************************************/
private static class JStatusBar extends JPanel {
private static final long serialVersionUID = 1L;
private JPanel leftPanel;
private JPanel rightPanel;
public JStatusBar() {
createPartControl();
}
private static class SeparatorPanel extends JPanel {
private static final long serialVersionUID = 1L;
private final Color leftColor;
private final Color rightColor;
public SeparatorPanel(Color leftColor, Color rightColor) {
this.leftColor = leftColor;
this.rightColor = rightColor;
setOpaque(false);
setPreferredSize(new Dimension(2, 18));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(leftColor);
g.drawLine(0, 0, 0, getHeight() - 1);
g.setColor(rightColor);
g.drawLine(1, 0, 1, getHeight() - 1);
}
}
private void createPartControl() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(10, 23));
leftPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 3));
leftPanel.setOpaque(false);
add(leftPanel, BorderLayout.WEST);
rightPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 5, 3));
rightPanel.setOpaque(false);
add(rightPanel, BorderLayout.EAST);
}
public void setLeftComponent(JComponent component) {
leftPanel.add(component);
}
public void addRightComponent(JComponent component) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
panel.add(new SeparatorPanel(Color.GRAY, Color.WHITE));
panel.add(component);
rightPanel.add(panel);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int y = 0;
g.setColor(new Color(156, 154, 140));
g.drawLine(0, y, getWidth(), y);
y++;
g.setColor(new Color(196, 194, 183));
g.drawLine(0, y, getWidth(), y);
y++;
g.setColor(new Color(218, 215, 201));
g.drawLine(0, y, getWidth(), y);
y++;
g.setColor(new Color(233, 231, 217));
g.drawLine(0, y, getWidth(), y);
y = getHeight() - 3;
g.setColor(new Color(233, 232, 218));
g.drawLine(0, y, getWidth(), y);
y++;
g.setColor(new Color(233, 231, 216));
g.drawLine(0, y, getWidth(), y);
y++;
g.setColor(new Color(221, 221, 220));
g.drawLine(0, y, getWidth(), y);
}
}
/***********************
* GUI CREATOR SECTION *
**********************/
private static void createGui()
{
final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd/MM/yyyy");
final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");
// variables
Image icon = null;
JButton btnShow;
JButton btnQuit;
JFrame frame;
frame = new JFrame();
frame.setTitle("Simple Status Bar");
//places the window in the center of the user's screen
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
frame.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
frame.setLocation((screenWidth - FRAME_SIZE_X) / 2, (screenHeight - FRAME_SIZE_Y) / 2);
//idem
//frame.pack();
//frame.setLocationRelativeTo(null);
try {
//URL iconURL = SimpleStatusBar.class.getResource("SimpleStatusBar.png");
URL iconURL = SimpleStatusBar.class.getResource("/SimpleStatusBar.png");
if (iconURL != null) {
//Image icon = ImageIO.read(iconURL);
icon = ImageIO.read(iconURL);
} else {
System.err.println("Resource '/SimpleStatusBar.png' not found.");
}
} catch (IOException e) {
e.printStackTrace();
}
if (icon != null)
frame.setIconImage(icon);
//frame.setLayout(null);
frame.setLayout(new BorderLayout());
frame.setResizable(false);
//frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.NORMAL);
// Create the NORTH JPanel with 2 buttons
JPanel northPanel = new JPanel(null);
northPanel.setPreferredSize(new Dimension(600, 120));
final JStatusBar statusBar = new JStatusBar();
JLabel leftLabel = new JLabel("Your application is running.");
statusBar.setLeftComponent(leftLabel);
final JLabel timeLabel = new JLabel();
timeLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(timeLabel);
timeLabel.setText(" ");
final JLabel dateLabel = new JLabel();
dateLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(dateLabel);
//dateLabel.setText(LocalDate.now().toString());
dateLabel.setText(LocalDate.now().format(DATE_FORMAT));
// Create the "show" button
btnShow = new JButton("Show");
btnShow.setToolTipText("Display the current system time");
btnShow.setBounds(btnShow_LOCATION_X, btnShow_LOCATION_Y, btnShow_SIZE_X, btnShow_SIZE_Y);
btnShow.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
leftLabel.setText("Button pressed at " + LocalTime.now().format(TIME_FORMAT) + ".");
//Calendar currentCalendar = Calendar.getInstance();
//Date currentTime = currentCalendar.getTime();
//Date currentTime = new Date();
//SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
//leftLabel.setText("Button pressed at " + timeFormat.format(currentTime) + ".");
////timeLabel.setText(timeFormat.format(currentTime));
}
});
northPanel.add(btnShow);
// Create the "exit" button
btnQuit = new JButton("Exit");
btnQuit.setToolTipText("Close the application");
btnQuit.setBounds(btnQuit_LOCATION_X, btnQuit_LOCATION_Y, btnQuit_SIZE_X, btnQuit_SIZE_Y);
btnQuit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
//frame.dispose();
//System.exit(0);
}
});
northPanel.add(btnQuit);
//WindowListener listener = new WindowAdapter() {
// @Override
// public void windowClosing(WindowEvent e) {
// //System.out.println("Closing window event");
// frame.dispose();
// System.exit(0);
// }
//};
//frame.addWindowListener(listener);
frame.add(northPanel, BorderLayout.NORTH);
frame.add(statusBar, BorderLayout.SOUTH);
frame.setVisible(true);
}
public static void main(String[] args)
{
//SwingUtilities.invokeLater(() -> createGui());
SwingUtilities.invokeLater(SimpleStatusBar::createGui);
}
}