private void jCmbLangsItemStateChanged(java.awt.event.ItemEvent evt) { 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 = jEditor.getText(); // install a new DefaultSyntaxKit on the JEditorPane for the requested language. jEditor.setContentType(lang); // Recreate the Toolbar jToolBar1.removeAll(); EditorKit kit = jEditor.getEditorKit(); if (kit instanceof DefaultSyntaxKit) { DefaultSyntaxKit defaultSyntaxKit = (DefaultSyntaxKit) kit; defaultSyntaxKit.addToolBarActions(jEditor, jToolBar1); } jToolBar1.validate(); try { // setText should not be called (read the JavaDocs). Better use the read // method and create a new document. jEditor.read(new StringReader(oldText), lang); } catch (IOException ex) { Logger.getLogger(SyntaxTester.class.getName()).log(Level.SEVERE, null, ex); } } jEditor.requestFocusInWindow(); }
public CKSyntaxPaneTest() { JFrame f = new JFrame("Party"); // CKSytaxPaneTest.class.getName()); final Container c = f.getContentPane(); c.setLayout(new BorderLayout()); // DefaultSyntaxKit.initKit(); final JEditorPane codeEditor = new JEditorPane(); JScrollPane scrPane = new JScrollPane(codeEditor); // codeEditor.setContentType("text/python"); codeEditor.setEditorKit(new PythonSyntaxKit()); // codeEditor.setText("public static void main(String[] args) {\n}"); // toolbar is part of the editor kit--ausome! JToolBar jToolBar1 = new javax.swing.JToolBar(); jToolBar1.setRollover(true); jToolBar1.setFocusable(false); EditorKit kit = codeEditor.getEditorKit(); if (kit instanceof DefaultSyntaxKit) { DefaultSyntaxKit defaultSyntaxKit = (DefaultSyntaxKit) kit; defaultSyntaxKit.addToolBarActions(codeEditor, jToolBar1); } jToolBar1.validate(); c.add(jToolBar1, BorderLayout.PAGE_START); c.add(scrPane, BorderLayout.CENTER); // c.add(codeEditor, BorderLayout.CENTER); // create console for output? JTextPane tpane = new JTextPane(); c.add(tpane, BorderLayout.LINE_START); c.doLayout(); f.setSize(800, 600); f.setVisible(true); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }