1
+ /* Copyright (C) 2018 Barış Meral
2
+
3
+ This program is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General Public License for more details.
12
+
13
+ You should have received a copy of the GNU General Public License
14
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
15
+ */
16
+
17
+ package dialogs ;
18
+
19
+ import javax .swing .*;
20
+ import javax .swing .border .LineBorder ;
21
+ import javax .swing .border .TitledBorder ;
22
+ import javax .swing .event .ListSelectionEvent ;
23
+ import javax .swing .event .ListSelectionListener ;
24
+ import java .awt .*;
25
+ import java .awt .event .*;
26
+ import dialogs .*;
27
+
28
+ /**
29
+ * @author Baris Meral
30
+ * @version 1.1
31
+ * @see java.awt.event.ActionListener
32
+ * @see java.util.EventListener
33
+ * @see javax.swing.event.ListSelectionListener
34
+ * <b>its usage is as follows</b>
35
+ * <pre>
36
+ * {@code
37
+ *
38
+ * JTexarea textarea = new JTextarea(); // or JTextField or JLabel
39
+ *
40
+ * JFontDialog fontDialog = new JfontDialog(textarea);
41
+ *
42
+ * }
43
+ * </pre>
44
+ *
45
+ *
46
+ */
47
+ public class JFontDialog implements ListSelectionListener ,ActionListener {
48
+
49
+ private static JFrame dialogFrame ;
50
+ private static JPanel fontNamePanel ,fontTypePanel ,fontSizePanel ,buttonPanel ,samplePanel ;
51
+ private static JList <String > fontNameList ,fontTypeList ,fontSizeList ;
52
+ private static JScrollPane fontPane ,sizePane ;
53
+ private static JLabel sampleLabel = new JLabel ("Sample..." ),fontLabel ,typeLabel ,sizeLabel ;
54
+ private static JButton okButton ,cancelButton ;
55
+ private static JSeparator separator ;
56
+ private static String [] graphicsEnvironment = GraphicsEnvironment .getLocalGraphicsEnvironment ().getAvailableFontFamilyNames ();
57
+ String [] fontTypeArray ,fontSizeArray ;
58
+ private static Font oldFont = null ;
59
+ private static boolean cancel =false ;
60
+
61
+ /**
62
+ * @param textArea
63
+ *
64
+ */
65
+ public JFontDialog (JTextArea textArea ){
66
+
67
+ createDialog (textArea );
68
+
69
+ oldFont = textArea .getFont ();
70
+ }
71
+
72
+ /**
73
+ * <b>Created Dialog Window</b>
74
+ * @param textArea
75
+ */
76
+ private void createDialog (JTextArea textArea ){
77
+
78
+ dialogFrame = new JFrame ("Font Dialog" );
79
+ dialogFrame .setLocationRelativeTo (null );
80
+ dialogFrame .setDefaultCloseOperation (JFrame .DISPOSE_ON_CLOSE );
81
+ dialogFrame .setSize (430 ,440 );
82
+ dialogFrame .setResizable (false );
83
+ dialogFrame .setLayout (null );
84
+
85
+ setLists ();
86
+ createButton ();
87
+ setPanels ();
88
+
89
+ dialogFrame .add (fontNamePanel );
90
+ dialogFrame .add (fontTypePanel );
91
+ dialogFrame .add (fontSizePanel );
92
+ dialogFrame .add (samplePanel );
93
+ dialogFrame .add (separator );
94
+ dialogFrame .add (buttonPanel );
95
+ dialogFrame .setVisible (true );
96
+
97
+ dialogFrame .addWindowListener (new WindowAdapter () {
98
+ @ Override
99
+ public void windowClosed (WindowEvent e ) {
100
+
101
+ if (cancel ==true ){
102
+ textArea .setFont (oldFont );
103
+ }
104
+ else if (cancel ==false ){
105
+ textArea .setFont (getFont ());
106
+ }
107
+ }
108
+ });
109
+ }
110
+
111
+ /**
112
+ * <b>Created and Set JList Components</b>
113
+ */
114
+ private void setLists (){
115
+
116
+ fontSizeArray = new String [29 ];
117
+ for (int i = 6 ,j =0 ; i <=62 ;i +=2 ,j ++)
118
+ fontSizeArray [j ]=String .valueOf (i );
119
+
120
+ fontSizeList = new JList <>(fontSizeArray );
121
+ fontSizeList .setSelectionMode (0 );
122
+ fontSizeList .setSelectedIndex (16 );
123
+ fontSizeList .addListSelectionListener (this );
124
+ fontSizeList .setFont (new Font ("Arial" ,Font .CENTER_BASELINE ,16 ));
125
+
126
+
127
+ graphicsEnvironment [0 ] = "Adobe" ;
128
+ fontNameList = new JList <>(graphicsEnvironment );
129
+ fontNameList .setSelectionMode (0 );
130
+ fontNameList .setSelectedIndex (3 );
131
+ fontNameList .addListSelectionListener (this );
132
+ fontNameList .setFont (new Font ("Arial" ,Font .CENTER_BASELINE ,16 ));
133
+
134
+ fontTypeArray = new String []{"Plain" , "Bold" , "Italic" };
135
+ fontTypeList = new JList <>(fontTypeArray );
136
+ fontTypeList .setBorder (new LineBorder (Color .BLACK ,1 ));
137
+ fontTypeList .setSelectionMode (0 );
138
+ fontTypeList .setSelectedIndex (1 );
139
+ fontTypeList .addListSelectionListener (this );
140
+ fontTypeList .setFont (new Font ("Arial" ,Font .CENTER_BASELINE ,16 ));
141
+
142
+ }
143
+
144
+ /**
145
+ * <b>Created and sey Panels for Components</b>
146
+ *
147
+ */
148
+ private void setPanels (){
149
+
150
+ fontLabel = new JLabel ("Fonts: " );
151
+ fontLabel .setFont (new Font ("Arial" ,Font .CENTER_BASELINE ,14 ));
152
+ fontNamePanel = new JPanel (new BorderLayout ());
153
+ fontPane = new JScrollPane (fontNameList );
154
+ fontNamePanel .add (fontLabel ,BorderLayout .NORTH );
155
+ fontNamePanel .add (fontPane ,BorderLayout .CENTER );
156
+ fontNamePanel .setBounds (30 ,20 ,220 ,200 );
157
+
158
+ typeLabel = new JLabel ("Type: " );
159
+ typeLabel .setFont (new Font ("Arial" ,Font .CENTER_BASELINE ,14 ));
160
+ fontTypePanel = new JPanel (new BorderLayout ());
161
+ fontTypePanel .add (fontTypeList ,BorderLayout .CENTER );
162
+ fontTypePanel .add (typeLabel ,BorderLayout .NORTH );
163
+ fontTypePanel .setBounds (270 ,20 ,50 ,100 );
164
+
165
+ sizeLabel = new JLabel ("Sizes: " );
166
+ sizeLabel .setFont (new Font ("Arial" ,Font .CENTER_BASELINE ,14 ));
167
+ fontSizePanel = new JPanel (new BorderLayout ());
168
+ sizePane = new JScrollPane (fontSizeList );
169
+ fontSizePanel .add (sizePane ,BorderLayout .CENTER );
170
+ fontSizePanel .add (sizeLabel ,BorderLayout .NORTH );
171
+ fontSizePanel .setBounds (340 ,20 ,50 ,200 );
172
+
173
+ samplePanel = new JPanel ();
174
+ samplePanel .setBorder (new TitledBorder (new LineBorder (Color .BLACK ,1 ),"Sample" ));
175
+ samplePanel .add (sampleLabel );
176
+ samplePanel .setBounds (20 ,240 ,375 ,100 );
177
+
178
+ separator = new JSeparator (JSeparator .HORIZONTAL );
179
+ separator .setForeground (Color .gray );
180
+ separator .setBounds (0 ,350 ,430 ,2 );
181
+
182
+ buttonPanel = new JPanel (new BorderLayout ());
183
+ buttonPanel .add (okButton ,BorderLayout .EAST );
184
+ buttonPanel .add (cancelButton ,BorderLayout .WEST );
185
+ buttonPanel .setBounds (200 ,360 ,200 ,25 );
186
+
187
+
188
+ }
189
+
190
+ /**
191
+ * <b>Created Buttons </b>
192
+ */
193
+ private void createButton (){
194
+
195
+ okButton = new JButton ("ok" );
196
+ okButton .setFont (new Font ("Arial" ,Font .CENTER_BASELINE ,18 ));
197
+ okButton .addActionListener (this ::actionPerformed );
198
+
199
+ cancelButton = new JButton ("cancel" );
200
+ cancelButton .setFont (new Font ("Arial" ,Font .CENTER_BASELINE ,18 ));
201
+ cancelButton .addActionListener (this ::actionPerformed );
202
+
203
+ }
204
+
205
+ /**
206
+ *
207
+ * @return selected Font
208
+ */
209
+ public static Font getFont (){
210
+ return sampleLabel .getFont ();
211
+ }
212
+
213
+ @ Override
214
+ public void valueChanged (ListSelectionEvent e ) {
215
+
216
+ sampleLabel .setFont (new Font (fontNameList .getSelectedValue (),fontTypeList .getSelectedIndex (),Integer .valueOf (fontSizeList .getSelectedValue ())));
217
+ }
218
+
219
+ @ Override
220
+ public void actionPerformed (ActionEvent e ) {
221
+
222
+
223
+ if (e .getSource ()==okButton ) {
224
+ cancel =false ;
225
+ dialogFrame .dispose ();
226
+ }
227
+
228
+ else if (e .getSource ()==cancelButton ){
229
+ cancel =true ;
230
+ dialogFrame .dispose ();
231
+ }
232
+
233
+
234
+ }
235
+
236
+
237
+ }
0 commit comments