|
| 1 | +package a8_javax.swing; |
1 | 2 |
|
| 3 | +import javax.swing.*; |
| 4 | +import javax.swing.text.*; |
| 5 | +import java.awt.*; |
| 6 | + |
| 7 | +/* A comprehensive demonstration of the Element interface and its methods. Purpose: |
| 8 | + * - This program demonstrates how to use the Element interface in Java Swing |
| 9 | + * to access and manipulate the structure and attributes of styled text components. |
| 10 | + * - All methods described in the specification are implemented and explained. */ |
| 11 | +public class c101_ElementExample { |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + SwingUtilities.invokeLater(c101_ElementExample::createAndShowGUI); |
| 15 | + } |
| 16 | + |
| 17 | + /* Creates and displays the GUI for demonstrating the Element interface. */ |
| 18 | + private static void createAndShowGUI() { |
| 19 | + // Step 1: Create the JFrame |
| 20 | + JFrame frame = new JFrame("Element Interface Demonstration"); |
| 21 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 22 | + frame.setSize(1200, 800); |
| 23 | + frame.setLayout(new BorderLayout()); |
| 24 | + |
| 25 | + // Step 2: Create a JTextPane and retrieve its styled document |
| 26 | + JTextPane textPane = new JTextPane(); |
| 27 | + textPane.setText("This is an example document.\nNew paragraph starts here.\nAnd another paragraph."); |
| 28 | + textPane.setFont(new Font("Monospaced", Font.PLAIN, 14)); |
| 29 | + StyledDocument styledDocument = textPane.getStyledDocument(); |
| 30 | + |
| 31 | + // Step 3: Logging/Text Output Area |
| 32 | + JTextArea logArea = new JTextArea(15, 50); |
| 33 | + logArea.setEditable(false); |
| 34 | + logArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); |
| 35 | + JScrollPane logScrollPane = new JScrollPane(logArea); |
| 36 | + logScrollPane.setBorder(BorderFactory.createTitledBorder("Logs")); |
| 37 | + |
| 38 | + // Step 4: Add buttons to demonstrate each Element method |
| 39 | + JPanel controlPanel = new JPanel(new GridLayout(5, 2, 10, 10)); |
| 40 | + |
| 41 | + // Method #1: getAttributes() - Fetch the attributes of an element |
| 42 | + JButton attributesButton = new JButton("Get Attributes"); |
| 43 | + attributesButton.addActionListener(e -> { |
| 44 | + Element rootElement = styledDocument.getDefaultRootElement(); // Access root |
| 45 | + AttributeSet attributes = rootElement.getAttributes(); // Fetch attributes |
| 46 | + logArea.append("[Method #1] Attributes: " + attributes + "\n"); |
| 47 | + }); |
| 48 | + |
| 49 | + // Method #2: getDocument() - Fetch the document associated with the element |
| 50 | + JButton documentButton = new JButton("Get Document"); |
| 51 | + documentButton.addActionListener(e -> { |
| 52 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 53 | + Document doc = rootElement.getDocument(); // Get document |
| 54 | + logArea.append("[Method #2] Associated Document: " + doc + "\n"); |
| 55 | + }); |
| 56 | + |
| 57 | + // Method #3: getElement(int index) - Fetch a child element by index |
| 58 | + JButton getElementButton = new JButton("Get Child Element"); |
| 59 | + getElementButton.addActionListener(e -> { |
| 60 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 61 | + if (rootElement.getElementCount() > 0) { |
| 62 | + Element child = rootElement.getElement(0); // Access first child element |
| 63 | + logArea.append("[Method #3] First Child: " + child + "\n"); |
| 64 | + } else { |
| 65 | + logArea.append("[Method #3] No child elements found.\n"); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + // Method #4: getElementCount() - Get the number of child elements |
| 70 | + JButton elementCountButton = new JButton("Get Element Count"); |
| 71 | + elementCountButton.addActionListener(e -> { |
| 72 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 73 | + int elementCount = rootElement.getElementCount(); // Get number of child elements |
| 74 | + logArea.append("[Method #4] Number of Child Elements: " + elementCount + "\n"); |
| 75 | + }); |
| 76 | + |
| 77 | + // Method #5: getElementIndex(int offset) - Get child element index by offset |
| 78 | + JButton elementIndexButton = new JButton("Get Element Index"); |
| 79 | + elementIndexButton.addActionListener(e -> { |
| 80 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 81 | + int index = rootElement.getElementIndex(15); // Use offset of 15 |
| 82 | + logArea.append("[Method #5] Element Index at Offset 15: " + index + "\n"); |
| 83 | + }); |
| 84 | + |
| 85 | + // Method #6: getEndOffset() - Get the end offset of an element |
| 86 | + JButton endOffsetButton = new JButton("Get End Offset"); |
| 87 | + endOffsetButton.addActionListener(e -> { |
| 88 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 89 | + int endOffset = rootElement.getEndOffset(); // Get end offset |
| 90 | + logArea.append("[Method #6] End Offset: " + endOffset + "\n"); |
| 91 | + }); |
| 92 | + |
| 93 | + // Method #7: getStartOffset() - Get the start offset of an element |
| 94 | + JButton startOffsetButton = new JButton("Get Start Offset"); |
| 95 | + startOffsetButton.addActionListener(e -> { |
| 96 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 97 | + int startOffset = rootElement.getStartOffset(); // Get start offset |
| 98 | + logArea.append("[Method #7] Start Offset: " + startOffset + "\n"); |
| 99 | + }); |
| 100 | + |
| 101 | + // Method #8: getParentElement() - Get the parent of an element |
| 102 | + JButton parentElementButton = new JButton("Get Parent Element"); |
| 103 | + parentElementButton.addActionListener(e -> { |
| 104 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 105 | + Element child = rootElement.getElement(0); // Access a child element |
| 106 | + Element parent = child.getParentElement(); // Get parent |
| 107 | + logArea.append("[Method #8] Parent Element: " + parent + "\n"); |
| 108 | + }); |
| 109 | + |
| 110 | + // Method #9: isLeaf() - Check if an element is a leaf |
| 111 | + JButton isLeafButton = new JButton("Is Leaf Element"); |
| 112 | + isLeafButton.addActionListener(e -> { |
| 113 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 114 | + boolean isLeaf = rootElement.isLeaf(); // Check if root is a leaf |
| 115 | + logArea.append("[Method #9] Is Root Element a Leaf? " + isLeaf + "\n"); |
| 116 | + }); |
| 117 | + |
| 118 | + // Method #10: getName() - Get the name of the element |
| 119 | + JButton getNameButton = new JButton("Get Element Name"); |
| 120 | + getNameButton.addActionListener(e -> { |
| 121 | + Element rootElement = styledDocument.getDefaultRootElement(); |
| 122 | + String name = rootElement.getName(); // Get element name |
| 123 | + logArea.append("[Method #10] Element Name: " + name + "\n"); |
| 124 | + }); |
| 125 | + |
| 126 | + // Add all buttons to the control panel |
| 127 | + controlPanel.add(attributesButton); |
| 128 | + controlPanel.add(documentButton); |
| 129 | + controlPanel.add(getElementButton); |
| 130 | + controlPanel.add(elementCountButton); |
| 131 | + controlPanel.add(elementIndexButton); |
| 132 | + controlPanel.add(endOffsetButton); |
| 133 | + controlPanel.add(startOffsetButton); |
| 134 | + controlPanel.add(parentElementButton); |
| 135 | + controlPanel.add(isLeafButton); |
| 136 | + controlPanel.add(getNameButton); |
| 137 | + |
| 138 | + // Step 5: Add components to the JFrame |
| 139 | + frame.add(new JScrollPane(textPane), BorderLayout.CENTER); // Center: Main TextPane |
| 140 | + frame.add(logScrollPane, BorderLayout.SOUTH); // Bottom: Log Area |
| 141 | + frame.add(controlPanel, BorderLayout.NORTH); // Top: Buttons |
| 142 | + |
| 143 | + // Step 6: Display the JFrame |
| 144 | + frame.setVisible(true); |
| 145 | + } |
| 146 | +} |
0 commit comments