Java Code Examples for javax.swing.JEditorPane#requestFocus()

The following examples show how to use javax.swing.JEditorPane#requestFocus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void performOpen(JEditorPane[] panes) {
    if (panes == null || panes.length == 0) {
        StatusDisplayer.getDefault().setStatusText(Bundle.ERR_ShellConsoleClosed());
        return;
    }
    JEditorPane p = panes[0];
    Rng[] fragments = theHandle.getFragments();
    
    int to = fragments[0].start;
    p.requestFocus();
    int pos = Math.min(p.getDocument().getLength() - 1, to);
    p.setCaretPosition(pos);
    try {
        Rectangle r = p.modelToView(pos);
        p.scrollRectToVisible(r);
    } catch (BadLocationException ex) {
    }
}
 
Example 2
Source File: EditorPanel.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
public EditorPanel(int width, int height) {
	super();
	
    editorPane = new JEditorPane();
    editorPane.setEditorKit(new BaseEditorKit());

    // must set a monospaced font, otherwise the caret position will be incorrect !!
    // but 'monospaced' font is ugly and I use 'Courier New'; you must install on Linux 'ttf-mscorefonts-installer'
    // (Installer for Microsoft TrueType core fonts)
    editorPane.setFont(new Font("Courier New", Font.PLAIN, 12));
    CurrentLineHighlighter.install(editorPane);

    scrollPane = new EditorScrollPane(width, height, editorPane, true, null);
    
    setLayout(new BorderLayout());
    add(scrollPane, BorderLayout.CENTER);
    
    editorPane.requestFocus();
}
 
Example 3
Source File: EditorActions.java    From blog-codes with Apache License 2.0 4 votes vote down vote up
/**
 * 
 */
public void actionPerformed(ActionEvent e)
{
	if (e.getSource() instanceof mxGraphComponent)
	{
		mxGraphComponent graphComponent = (mxGraphComponent) e
				.getSource();
		Component editorComponent = null;

		if (graphComponent.getCellEditor() instanceof mxCellEditor)
		{
			editorComponent = ((mxCellEditor) graphComponent
					.getCellEditor()).getEditor();
		}

		if (editorComponent instanceof JEditorPane)
		{
			JEditorPane editorPane = (JEditorPane) editorComponent;
			int start = editorPane.getSelectionStart();
			int ende = editorPane.getSelectionEnd();
			String text = editorPane.getSelectedText();

			if (text == null)
			{
				text = "";
			}

			try
			{
				HTMLEditorKit editorKit = new HTMLEditorKit();
				HTMLDocument document = (HTMLDocument) editorPane
						.getDocument();
				document.remove(start, (ende - start));
				editorKit.insertHTML(document, start, ((bold) ? "<b>"
						: "<i>") + text + ((bold) ? "</b>" : "</i>"),
						0, 0, (bold) ? HTML.Tag.B : HTML.Tag.I);
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
			}

			editorPane.requestFocus();
			editorPane.select(start, ende);
		}
		else
		{
			mxIGraphModel model = graphComponent.getGraph().getModel();
			model.beginUpdate();
			try
			{
				graphComponent.stopEditing(false);
				graphComponent.getGraph().toggleCellStyleFlags(
						mxConstants.STYLE_FONTSTYLE,
						(bold) ? mxConstants.FONT_BOLD
								: mxConstants.FONT_ITALIC);
			}
			finally
			{
				model.endUpdate();
			}
		}
	}
}