|
| 1 | +/* |
| 2 | + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * - Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * - Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * - Neither the name of Oracle or the names of its |
| 16 | + * contributors may be used to endorse or promote products derived |
| 17 | + * from this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +package components; |
| 33 | + |
| 34 | +// see https://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#TextSamplerDemo |
| 35 | +/* |
| 36 | + * TextSamplerDemo.java requires the following files: |
| 37 | + * TextSamplerDemoHelp.html (which references images/dukeWaveRed.gif) |
| 38 | + * images/Pig.gif |
| 39 | + * images/sound.gif |
| 40 | + */ |
| 41 | + |
| 42 | +import javax.swing.*; |
| 43 | +import javax.swing.text.*; |
| 44 | + |
| 45 | +import java.awt.*; //for layout managers and more |
| 46 | +import java.awt.event.*; //for action events |
| 47 | + |
| 48 | +import java.net.URL; |
| 49 | +import java.io.IOException; |
| 50 | + |
| 51 | +public class TextSamplerDemo extends JPanel |
| 52 | + implements ActionListener { |
| 53 | + private String newline = "\n"; |
| 54 | + protected static final String textFieldString = "JTextField"; |
| 55 | + protected static final String passwordFieldString = "JPasswordField"; |
| 56 | + protected static final String ftfString = "JFormattedTextField"; |
| 57 | + protected static final String buttonString = "JButton"; |
| 58 | + |
| 59 | + protected JLabel actionLabel; |
| 60 | + |
| 61 | + public TextSamplerDemo() { |
| 62 | + setLayout(new BorderLayout()); |
| 63 | + |
| 64 | + //Create a regular text field. |
| 65 | + JTextField textField = new JTextField(10); |
| 66 | + textField.setActionCommand(textFieldString); |
| 67 | + textField.addActionListener(this); |
| 68 | + |
| 69 | + //Create a password field. |
| 70 | + JPasswordField passwordField = new JPasswordField(10); |
| 71 | + passwordField.setActionCommand(passwordFieldString); |
| 72 | + passwordField.addActionListener(this); |
| 73 | + |
| 74 | + //Create a formatted text field. |
| 75 | + JFormattedTextField ftf = new JFormattedTextField( |
| 76 | + java.util.Calendar.getInstance().getTime()); |
| 77 | + ftf.setActionCommand(textFieldString); |
| 78 | + ftf.addActionListener(this); |
| 79 | + |
| 80 | + //Create some labels for the fields. |
| 81 | + JLabel textFieldLabel = new JLabel(textFieldString + ": "); |
| 82 | + textFieldLabel.setLabelFor(textField); |
| 83 | + JLabel passwordFieldLabel = new JLabel(passwordFieldString + ": "); |
| 84 | + passwordFieldLabel.setLabelFor(passwordField); |
| 85 | + JLabel ftfLabel = new JLabel(ftfString + ": "); |
| 86 | + ftfLabel.setLabelFor(ftf); |
| 87 | + |
| 88 | + //Create a label to put messages during an action event. |
| 89 | + actionLabel = new JLabel("Type text in a field and press Enter."); |
| 90 | + actionLabel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0)); |
| 91 | + |
| 92 | + //Lay out the text controls and the labels. |
| 93 | + JPanel textControlsPane = new JPanel(); |
| 94 | + GridBagLayout gridbag = new GridBagLayout(); |
| 95 | + GridBagConstraints c = new GridBagConstraints(); |
| 96 | + |
| 97 | + textControlsPane.setLayout(gridbag); |
| 98 | + |
| 99 | + JLabel[] labels = {textFieldLabel, passwordFieldLabel, ftfLabel}; |
| 100 | + JTextField[] textFields = {textField, passwordField, ftf}; |
| 101 | + addLabelTextRows(labels, textFields, gridbag, textControlsPane); |
| 102 | + |
| 103 | + c.gridwidth = GridBagConstraints.REMAINDER; //last |
| 104 | + c.anchor = GridBagConstraints.WEST; |
| 105 | + c.weightx = 1.0; |
| 106 | + textControlsPane.add(actionLabel, c); |
| 107 | + textControlsPane.setBorder( |
| 108 | + BorderFactory.createCompoundBorder( |
| 109 | + BorderFactory.createTitledBorder("Text Fields"), |
| 110 | + BorderFactory.createEmptyBorder(5,5,5,5))); |
| 111 | + |
| 112 | + //Create a text area. |
| 113 | + JTextArea textArea = new JTextArea( |
| 114 | + "This is an editable JTextArea. " + |
| 115 | + "A text area is a \"plain\" text component, " + |
| 116 | + "which means that although it can display text " + |
| 117 | + "in any font, all of the text is in the same font." |
| 118 | + ); |
| 119 | + textArea.setFont(new Font("Serif", Font.ITALIC, 16)); |
| 120 | + textArea.setLineWrap(true); |
| 121 | + textArea.setWrapStyleWord(true); |
| 122 | + JScrollPane areaScrollPane = new JScrollPane(textArea); |
| 123 | + areaScrollPane.setVerticalScrollBarPolicy( |
| 124 | + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
| 125 | + areaScrollPane.setPreferredSize(new Dimension(250, 250)); |
| 126 | + areaScrollPane.setBorder( |
| 127 | + BorderFactory.createCompoundBorder( |
| 128 | + BorderFactory.createCompoundBorder( |
| 129 | + BorderFactory.createTitledBorder("Plain Text"), |
| 130 | + BorderFactory.createEmptyBorder(5,5,5,5)), |
| 131 | + areaScrollPane.getBorder())); |
| 132 | + |
| 133 | + //Create an editor pane. |
| 134 | + JEditorPane editorPane = createEditorPane(); |
| 135 | + JScrollPane editorScrollPane = new JScrollPane(editorPane); |
| 136 | + editorScrollPane.setVerticalScrollBarPolicy( |
| 137 | + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
| 138 | + editorScrollPane.setPreferredSize(new Dimension(250, 145)); |
| 139 | + editorScrollPane.setMinimumSize(new Dimension(10, 10)); |
| 140 | + |
| 141 | + //Create a text pane. |
| 142 | + JTextPane textPane = createTextPane(); |
| 143 | + JScrollPane paneScrollPane = new JScrollPane(textPane); |
| 144 | + paneScrollPane.setVerticalScrollBarPolicy( |
| 145 | + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
| 146 | + paneScrollPane.setPreferredSize(new Dimension(250, 155)); |
| 147 | + paneScrollPane.setMinimumSize(new Dimension(10, 10)); |
| 148 | + |
| 149 | + //Put the editor pane and the text pane in a split pane. |
| 150 | + JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, |
| 151 | + editorScrollPane, |
| 152 | + paneScrollPane); |
| 153 | + splitPane.setOneTouchExpandable(true); |
| 154 | + splitPane.setResizeWeight(0.5); |
| 155 | + JPanel rightPane = new JPanel(new GridLayout(1,0)); |
| 156 | + rightPane.add(splitPane); |
| 157 | + rightPane.setBorder(BorderFactory.createCompoundBorder( |
| 158 | + BorderFactory.createTitledBorder("Styled Text"), |
| 159 | + BorderFactory.createEmptyBorder(5,5,5,5))); |
| 160 | + |
| 161 | + |
| 162 | + //Put everything together. |
| 163 | + JPanel leftPane = new JPanel(new BorderLayout()); |
| 164 | + leftPane.add(textControlsPane, |
| 165 | + BorderLayout.PAGE_START); |
| 166 | + leftPane.add(areaScrollPane, |
| 167 | + BorderLayout.CENTER); |
| 168 | + |
| 169 | + add(leftPane, BorderLayout.LINE_START); |
| 170 | + add(rightPane, BorderLayout.LINE_END); |
| 171 | + } |
| 172 | + |
| 173 | + private void addLabelTextRows(JLabel[] labels, |
| 174 | + JTextField[] textFields, |
| 175 | + GridBagLayout gridbag, |
| 176 | + Container container) { |
| 177 | + GridBagConstraints c = new GridBagConstraints(); |
| 178 | + c.anchor = GridBagConstraints.EAST; |
| 179 | + int numLabels = labels.length; |
| 180 | + |
| 181 | + for (int i = 0; i < numLabels; i++) { |
| 182 | + c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last |
| 183 | + c.fill = GridBagConstraints.NONE; //reset to default |
| 184 | + c.weightx = 0.0; //reset to default |
| 185 | + container.add(labels[i], c); |
| 186 | + |
| 187 | + c.gridwidth = GridBagConstraints.REMAINDER; //end row |
| 188 | + c.fill = GridBagConstraints.HORIZONTAL; |
| 189 | + c.weightx = 1.0; |
| 190 | + container.add(textFields[i], c); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public void actionPerformed(ActionEvent e) { |
| 195 | + String prefix = "You typed \""; |
| 196 | + if (textFieldString.equals(e.getActionCommand())) { |
| 197 | + JTextField source = (JTextField)e.getSource(); |
| 198 | + actionLabel.setText(prefix + source.getText() + "\""); |
| 199 | + } else if (passwordFieldString.equals(e.getActionCommand())) { |
| 200 | + JPasswordField source = (JPasswordField)e.getSource(); |
| 201 | + actionLabel.setText(prefix + new String(source.getPassword()) |
| 202 | + + "\""); |
| 203 | + } else if (buttonString.equals(e.getActionCommand())) { |
| 204 | + Toolkit.getDefaultToolkit().beep(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private JEditorPane createEditorPane() { |
| 209 | + JEditorPane editorPane = new JEditorPane() { |
| 210 | + public Document getDocument() { |
| 211 | + return super.getDocument(); |
| 212 | + } |
| 213 | + |
| 214 | + public void setDocument(Document doc) { |
| 215 | + super.setDocument(doc); |
| 216 | + } |
| 217 | + }; |
| 218 | + |
| 219 | + editorPane.setEditable(false); |
| 220 | + java.net.URL helpURL = TextSamplerDemo.class.getResource( |
| 221 | + "TextSamplerDemoHelp.html"); |
| 222 | + if (helpURL != null) { |
| 223 | + try { |
| 224 | + editorPane.setPage(helpURL); |
| 225 | + } catch (IOException e) { |
| 226 | + System.err.println("Attempted to read a bad URL: " + helpURL); |
| 227 | + } |
| 228 | + } else { |
| 229 | + System.err.println("Couldn't find file: TextSampleDemoHelp.html"); |
| 230 | + } |
| 231 | + |
| 232 | + return editorPane; |
| 233 | + } |
| 234 | + |
| 235 | + private JTextPane createTextPane() { |
| 236 | + String[] initString = |
| 237 | + { "This is an editable JTextPane, ", //regular |
| 238 | + "another ", //italic |
| 239 | + "styled ", //bold |
| 240 | + "text ", //small |
| 241 | + "component, ", //large |
| 242 | + "which supports embedded components..." + newline,//regular |
| 243 | + " " + newline, //button |
| 244 | + "...and embedded icons..." + newline, //regular |
| 245 | + " ", //icon |
| 246 | + newline + "JTextPane is a subclass of JEditorPane that " + |
| 247 | + "uses a StyledEditorKit and StyledDocument, and provides " + |
| 248 | + "cover methods for interacting with those objects." |
| 249 | + }; |
| 250 | + |
| 251 | + String[] initStyles = |
| 252 | + { "regular", "italic", "bold", "small", "large", |
| 253 | + "regular", "button", "regular", "icon", |
| 254 | + "regular" |
| 255 | + }; |
| 256 | + |
| 257 | + JTextPane textPane = new JTextPane(); |
| 258 | + StyledDocument doc = textPane.getStyledDocument(); |
| 259 | + addStylesToDocument(doc); |
| 260 | + |
| 261 | + try { |
| 262 | + for (int i=0; i < initString.length; i++) { |
| 263 | + doc.insertString(doc.getLength(), initString[i], |
| 264 | + doc.getStyle(initStyles[i])); |
| 265 | + } |
| 266 | + } catch (BadLocationException ble) { |
| 267 | + System.err.println("Couldn't insert initial text into text pane."); |
| 268 | + } |
| 269 | + |
| 270 | + return textPane; |
| 271 | + } |
| 272 | + |
| 273 | + protected void addStylesToDocument(StyledDocument doc) { |
| 274 | + //Initialize some styles. |
| 275 | + Style def = StyleContext.getDefaultStyleContext(). |
| 276 | + getStyle(StyleContext.DEFAULT_STYLE); |
| 277 | + |
| 278 | + Style regular = doc.addStyle("regular", def); |
| 279 | + StyleConstants.setFontFamily(def, "SansSerif"); |
| 280 | + |
| 281 | + Style s = doc.addStyle("italic", regular); |
| 282 | + StyleConstants.setItalic(s, true); |
| 283 | + |
| 284 | + s = doc.addStyle("bold", regular); |
| 285 | + StyleConstants.setBold(s, true); |
| 286 | + |
| 287 | + s = doc.addStyle("small", regular); |
| 288 | + StyleConstants.setFontSize(s, 10); |
| 289 | + |
| 290 | + s = doc.addStyle("large", regular); |
| 291 | + StyleConstants.setFontSize(s, 16); |
| 292 | + |
| 293 | + s = doc.addStyle("icon", regular); |
| 294 | + StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER); |
| 295 | + ImageIcon pigIcon = createImageIcon("images/Pig.gif", |
| 296 | + "a cute pig"); |
| 297 | + if (pigIcon != null) { |
| 298 | + StyleConstants.setIcon(s, pigIcon); |
| 299 | + } |
| 300 | + |
| 301 | + s = doc.addStyle("button", regular); |
| 302 | + StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER); |
| 303 | + ImageIcon soundIcon = createImageIcon("images/sound.gif", |
| 304 | + "sound icon"); |
| 305 | + JButton button = new JButton(); |
| 306 | + if (soundIcon != null) { |
| 307 | + button.setIcon(soundIcon); |
| 308 | + } else { |
| 309 | + button.setText("BEEP"); |
| 310 | + } |
| 311 | + button.setCursor(Cursor.getDefaultCursor()); |
| 312 | + button.setMargin(new Insets(0,0,0,0)); |
| 313 | + button.setActionCommand(buttonString); |
| 314 | + button.addActionListener(this); |
| 315 | + StyleConstants.setComponent(s, button); |
| 316 | + } |
| 317 | + |
| 318 | + /** Returns an ImageIcon, or null if the path was invalid. */ |
| 319 | + protected static ImageIcon createImageIcon(String path, |
| 320 | + String description) { |
| 321 | + if (true) |
| 322 | + return null; |
| 323 | + java.net.URL imgURL = TextSamplerDemo.class.getResource(path); |
| 324 | + if (imgURL != null) { |
| 325 | + return new ImageIcon(imgURL, description); |
| 326 | + } else { |
| 327 | + System.err.println("Couldn't find file: " + path); |
| 328 | + return null; |
| 329 | + } |
| 330 | + } |
| 331 | + |
| 332 | + /** |
| 333 | + * Create the GUI and show it. For thread safety, |
| 334 | + * this method should be invoked from the |
| 335 | + * event dispatch thread. |
| 336 | + */ |
| 337 | + private static void createAndShowGUI() { |
| 338 | + //Create and set up the window. |
| 339 | + JFrame frame = new JFrame("TextSamplerDemo"); |
| 340 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 341 | + |
| 342 | + //Add content to the window. |
| 343 | + frame.add(new TextSamplerDemo()); |
| 344 | + |
| 345 | + //Display the window. |
| 346 | + frame.pack(); |
| 347 | + frame.setVisible(true); |
| 348 | + } |
| 349 | + |
| 350 | + public static void main(String[] args) { |
| 351 | + //Schedule a job for the event dispatching thread: |
| 352 | + //creating and showing this application's GUI. |
| 353 | + SwingUtilities.invokeLater(new Runnable() { |
| 354 | + public void run() { |
| 355 | + //Turn off metal's use of bold fonts |
| 356 | + UIManager.put("swing.boldMetal", Boolean.FALSE); |
| 357 | + createAndShowGUI(); |
| 358 | + } |
| 359 | + }); |
| 360 | + } |
| 361 | +} |
0 commit comments