/** * Fetches the EditorKit for the UI. * * @return the component capabilities */ public EditorKit getEditorKit(JTextComponent c) { JEditorPane pane = (JEditorPane) component; return pane.getEditorKit(); }
/** * Return the DefaultSyntaxKit of this target, or null if the target does not * have a DefaultSyntaxKit * * @return kit or null */ public static DefaultSyntaxKit getSyntaxKit(JTextComponent target) { DefaultSyntaxKit kit = null; if (target instanceof JEditorPane) { JEditorPane jEditorPane = (JEditorPane) target; EditorKit k = jEditorPane.getEditorKit(); if (k instanceof DefaultSyntaxKit) { kit = (DefaultSyntaxKit) k; } } return kit; }
/** * Return the DefaultSyntaxKit of this target, or null if the target does not * have a DefaultSyntaxKit * @param target * @return kit or null */ public static DefaultSyntaxKit getSyntaxKit(JTextComponent target) { DefaultSyntaxKit kit = null; if (target instanceof JEditorPane) { JEditorPane jEditorPane = (JEditorPane) target; EditorKit k = jEditorPane.getEditorKit(); if (k instanceof DefaultSyntaxKit) { kit = (DefaultSyntaxKit) k; } } return kit; }
/** Fetches the EditorKit for the UI. * * @return the component capabilities */ public EditorKit getEditorKit(JTextComponent c) { JEditorPane pane = (JEditorPane)getComponent(); return (pane==null) ? null : pane.getEditorKit(); }
private static boolean accept(JTextComponent jTextComponent) { if (!JEditorPane.class.isInstance(jTextComponent)) { return false; } JEditorPane editorPane = (JEditorPane)jTextComponent; return editorPane.getEditorKit().getContentType().startsWith("text/html"); }
public void run() { textPane.setText("<html><body id=\"body\"></body></html>"); ((MessagePanelEditorKit) textPane.getEditorKit()).flush(); } });
JEditorPane pane = new JEditorPane(); String u = "C:/path/to/bullet.png"; HTMLEditorKit htmlEditorKit = (HTMLEditorKit) pane.getEditorKit(); StyleSheet styleSheet = htmlEditorKit.getStyleSheet(); styleSheet.addRule(String.format("ul{list-style-image:url(%s);margin:0px 20px;", u));
/** * Inserts a string to the current html document of the specified editor. * Main usage is to add strings to the current log message. * * @param editor message field * @param html html string to add * @param location document location where the specified string shall be * added */ private void insertHTML(JEditorPane editor, String html, int location) { //assumes editor is already set to "text/html" type HTMLEditorKit kit = (HTMLEditorKit) editor.getEditorKit(); Document doc = editor.getDocument(); StringReader reader = new StringReader(html); try { kit.read(reader, doc, location); } catch (BadLocationException ex) { Logger.getLogger(MessageBox.class.getName()).log( Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(MessageBox.class.getName()).log( Level.SEVERE, null, ex); } }
JEditorPane ed = new JEditorPane(); ed.setContentType("text/html"); HTMLEditorKit kit = (HTMLEditorKit) ed.getEditorKit(); HTMLDocument doc = (HTMLDocument) ed.getDocument(); doc.setBase(new URL("http://fakeurl.com")); kit.setAutoFormSubmission(false); ed.setText("<html>" + "<head>" + "</head>" + "<body>" + " <form action='http://fakeurl.com:1'><input type='submit' value='hello 1' /></form>" + " <form action='http://fakeurl.com:2'><input type='submit' value='hello 2' /></form>" + "</body>"); ed.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { System.err.println(e.getURL().getPort()); } }); UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName()); JFrame f = new JFrame("test frame"); f.setContentPane(ed); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(1024, 768); f.setVisible(true);
/** * Creates new form ShowAbbsDialog * @param parent * @param abbs */ public ShowAbbsDialog(JEditorPane parent, Map<String, String> abbs) { super(SwingUtilities.getWindowAncestor(parent), ModalityType.APPLICATION_MODAL); initComponents(); Object[] abbsList = abbs.keySet().toArray(); Arrays.sort(abbsList); jLstAbbs.setListData(abbsList); this.abbs = abbs; jEdtAbbr.setEditorKit(parent.getEditorKit()); jLstAbbs.setSelectedIndex(0); SwingUtils.addEscapeListener(this); setVisible(true); }
/** * Creates new form ShowAbbsDialog * @param parent * @param abbs */ public ShowAbbsDialog(JEditorPane parent, Map<String, String> abbs) { super(SwingUtilities.getWindowAncestor(parent), ModalityType.APPLICATION_MODAL); initComponents(); Object[] abbsList = abbs.keySet().toArray(); Arrays.sort(abbsList); jLstAbbs.setListData(abbsList); this.abbs = abbs; jEdtAbbr.setEditorKit(parent.getEditorKit()); jLstAbbs.setSelectedIndex(0); SwingUtils.addEscapeListener(this); setVisible(true); }
@Override public void actionPerformed(ActionEvent e) { JTextComponent target = getTextComponent(e); if (target instanceof JEditorPane) { JEditorPane jEditorPane = (JEditorPane) target; DefaultSyntaxKit kit = (DefaultSyntaxKit) jEditorPane.getEditorKit(); boolean status = kit.toggleComponent(jEditorPane, componentName); putValue(SELECTED_KEY, status); } } }
@Override public void actionPerformed(ActionEvent e) { JTextComponent target = getTextComponent(e); if (target instanceof JEditorPane) { JEditorPane jEditorPane = (JEditorPane) target; DefaultSyntaxKit kit = (DefaultSyntaxKit) jEditorPane.getEditorKit(); boolean status = kit.toggleComponent(jEditorPane, componentName); putValue(SELECTED_KEY, status); } } }
private void updateStyle(@NotNull JEditorPane pane) { EditorKit kit = pane.getEditorKit(); if (kit instanceof HTMLEditorKit) { StyleSheet css = ((HTMLEditorKit)kit).getStyleSheet(); css.addRule("body, p {" + "color:#" + ColorUtil.toHex(getForeground()) + ";" + "font-family:" + getFont().getFamily() + ";" + "font-size:" + getFont().getSize() + "pt;" + "white-space:nowrap;}"); } } }
private void jCmbLangsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCmbLangsItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED) { String lang = jCmbLangs.getSelectedItem().toString(); // save the state of the current JEditorPane, as it's Document is about // to be replaced. String oldText = jEdtTest.getText(); // install a new DefaultSyntaxKit on the JEditorPane for the requested language. jEdtTest.setContentType(lang); // Recreate the Toolbar jToolBar1.removeAll(); EditorKit kit = jEdtTest.getEditorKit(); if (kit instanceof DefaultSyntaxKit) { DefaultSyntaxKit defaultSyntaxKit = (DefaultSyntaxKit) kit; defaultSyntaxKit.addToolBarActions(jEdtTest, jToolBar1); } jToolBar1.validate(); try { // setText should not be called (read the JavaDocs). Better use the read // method and create a new document. jEdtTest.read(new StringReader(oldText), lang); } catch (IOException ex) { Logger.getLogger(SyntaxTester.class.getName()).log(Level.SEVERE, null, ex); } } jEdtTest.requestFocusInWindow(); }//GEN-LAST:event_jCmbLangsItemStateChanged
public void run() { // #23486: pane could not be initialized yet. if(pane != null) { Document doc = support.createStyledDocument( pane.getEditorKit()); pane.setDocument (doc); pane.setEditorKit (null); } removeAll(); initialized = false; } }
public void run() { // #23486: pane could not be initialized yet. if(pane != null) { Document doc = support.createStyledDocument( pane.getEditorKit()); pane.setDocument (doc); pane.setEditorKit (null); } removeAll(); initialized = false; } }
TextTransferable(JTextComponent c, int start, int end) { this.c = c; Document doc = c.getDocument(); try { p0 = doc.createPosition(start); p1 = doc.createPosition(end); plainData = c.getSelectedText(); if (c instanceof JEditorPane) { JEditorPane ep = (JEditorPane)c; mimeType = ep.getContentType(); if (mimeType.startsWith("text/plain")) { return; } StringWriter sw = new StringWriter(p1.getOffset() - p0.getOffset()); ep.getEditorKit().write(sw, doc, p0.getOffset(), p1.getOffset() - p0.getOffset()); if (mimeType.startsWith("text/html")) { htmlData = sw.toString(); } else { richText = sw.toString(); } } } catch (BadLocationException ble) { } catch (IOException ioe) { } }
/** * Configures the wrapped JEditorPane to display the HTML body. */ private void configureHtmlText(){ JEditorPane editorPane = (JEditorPane) getComponent(); if (editorPane != null){ editorPane.setDocument(editorPane.getEditorKit().createDefaultDocument()); editorPane.setText(createHtmlText());//text.replaceAll("\n", "<br>")); HTMLDocument doc=(HTMLDocument)editorPane.getDocument(); doc.setParagraphAttributes(0,doc.getLength(),attributeSet,true); //System.out.println(createHtmlText()+" -> "); //System.out.println(editorPane.getText()); } }
@Override public void run() { try { HTMLEditorKit kit = (HTMLEditorKit) historyWidget .getEditorKit(); HTMLDocument doc = (HTMLDocument) historyWidget .getDocument(); // historyWidget.getDocument().insertString(len - offset, // trimMsg(msg), null); // Element root = doc.getDefaultRootElement(); // Element body = root.getElement(1); // doc.insertBeforeEnd(body, msg); int pos = historyWidget.getCaretPosition(); kit.insertHTML(doc, doc.getLength(), msg, 0, 0, null); historyWidget.setCaretPosition( scrollLock ? pos : doc.getLength()); } catch (Exception e) { e.printStackTrace(); } } });