Die Suche nach einer Zeichenkette in einem JTextPane ist relativ einfach zu bewerkstelligen – ein Textfeld, ein Button und ein wenig Code genügen. Der Screenshot unseres Testprogramms sieht so aus:
Die Suche arbeitet nicht case-sensitiv, d.h. eine Groß- oder Kleinschreibung des Suchwortes wird nicht beachtet.
Bitte beachtet beim Nachbauen, das Ihr hierfür intelliJ benötigt.
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 |
/* * Herkunft/Origin: http://java-crypto.bplaced.net/ * Programmierer/Programmer: Michael Fehr * Copyright/Copyright: frei verwendbares Programm (Public Domain) * Copyright: This is free and unencumbered software released into the public domain. * Lizenttext/Licence: <http://unlicense.org> * getestet mit/tested with: Java Runtime Environment 11.0.5 x64 * verwendete IDE/used IDE: intelliJ IDEA 2019.3.1 * Datum/Date (dd.mm.jjjj): 08.01.2020 * Funktion: TextPane Search = * sucht den Suchtext im Dokument * Function: TextPane Search = * searches text in the document * * Hinweis/Notice * Sie benoetigen intelliJ um das Programm uebersetzen und ausfuehren zu koennen * You need intelliJ to build and run the program */ import javax.swing.*; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultStyledDocument; import javax.swing.text.StyleContext; import javax.swing.text.StyledDocument; import javax.swing.text.rtf.RTFEditorKit; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; public class TextPane { private JPanel panelMain; private JPanel panelButtons; private JButton buttonLoad; private JScrollPane scrollPane; private JTextPane textPane; private JButton buttonSave; private JButton buttonSearch; private JTextField tfSearch; public TextPane() { textPane.setContentType("text/rtf"); textPane.setEditorKit(new RTFEditorKit()); buttonLoad.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { loadRtf("textpane_rtf.rtf"); } }); buttonSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { saveRtf("textpane_rtf.rtf"); } }); buttonSearch.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { searchText(); } }); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setContentPane(new TextPane().panelMain); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setTitle("TextPane Search"); frame.setSize(550, 350); frame.setVisible(true); } public void loadRtf(String filename) { RTFEditorKit RTF_KIT = new RTFEditorKit(); textPane.setContentType("text/rtf"); InputStream inputStream; try { inputStream = new FileInputStream(filename); final DefaultStyledDocument styledDocument = new DefaultStyledDocument(new StyleContext()); RTF_KIT.read(inputStream, styledDocument, 0); textPane.setDocument(styledDocument); // delete added last line String content = textPane.getDocument().getText(0, textPane.getDocument().getLength()); int lastLineBreak = content.lastIndexOf('\n'); textPane.getDocument().remove(lastLineBreak, textPane.getDocument().getLength() - lastLineBreak); } catch (IOException | BadLocationException e) { e.printStackTrace(); } textPane.requestFocus(); } public void saveRtf(String filename) { try { FileOutputStream fos = new FileOutputStream(filename); RTFEditorKit kit = (RTFEditorKit) textPane.getEditorKit(); StyledDocument doc = textPane.getStyledDocument(); int len = doc.getLength(); kit.write(fos, doc, 0, len); fos.close(); } catch (IOException | BadLocationException e) { e.printStackTrace(); } textPane.requestFocus(); } public void searchText() { int length = textPane.getDocument().getLength(); int caret = textPane.getCaretPosition(); String text = ""; try { text = textPane.getDocument().getText(0, length).toLowerCase(); } catch (BadLocationException e) { e.printStackTrace(); } int startFrom = caret + 1; if (startFrom == textPane.getSelectionEnd()) { startFrom = -1; } String searchWord = tfSearch.getText().toLowerCase(); int searchIndex = text.indexOf(searchWord, startFrom); if (searchIndex != -1) { textPane.select(searchIndex, searchIndex + searchWord.length()); } else { textPane.setSelectionStart(0); textPane.setSelectionEnd(0); } textPane.requestFocus(); } } |
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 |
<?xml version="1.0" encoding="UTF-8" ?> - <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="TextPane"> - <grid id="27dc6" binding="panelMain" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="10" left="10" bottom="10" right="10" /> - <constraints> <xy x="20" y="20" width="543" height="400" /> </constraints> <properties /> <border type="none" /> - <children> - <grid id="69623" binding="panelButtons" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0" /> - <constraints> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false" /> </constraints> <properties /> <border type="none" /> - <children> - <component id="b1559" class="javax.swing.JButton" binding="buttonLoad"> - <constraints> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false" /> </constraints> - <properties> <text value="load" /> </properties> </component> - <component id="1e556" class="javax.swing.JButton" binding="buttonSave"> - <constraints> <grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false" /> </constraints> - <properties> <text value="save" /> </properties> </component> - <component id="a6ffb" class="javax.swing.JButton" binding="buttonSearch"> - <constraints> <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false" /> </constraints> - <properties> <text value="search" /> </properties> </component> - <component id="452e" class="javax.swing.JTextField" binding="tfSearch"> - <constraints> - <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> <preferred-size width="150" height="-1" /> </grid> </constraints> <properties /> </component> </children> </grid> - <scrollpane id="5bf0e" binding="scrollPane"> - <constraints> <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false" /> </constraints> <properties /> <border type="none" /> - <children> - <component id="100bc" class="javax.swing.JTextPane" binding="textPane"> <constraints /> <properties /> </component> </children> </scrollpane> </children> </grid> </form> |
Alle Quellcodes zur JTextPane findet Ihr zum Download in meinem Github-Repository, welches Ihr über diesen Link erreicht: https://github.com/java-crypto/JTextPane. Alle Programme sind unter Java 11 lauffähig (vermutlich auch unter Java 8) und wurden mit intelliJ IDEA entwickelt, welches für das eigene „Spielen“ notwendig ist.
Die Lizenz zum obigen Beispiel findet Ihr auf der eigenen Lizenz-Seite.
Letzte Bearbeitung: 08.01.2020