Color Not Applied In JTextPane If Disabled
Posted by blogmeister on
July 29, 2012
So I had a JTextPane that displays text in HTML format. The color issue arises when I disable the JTextPane. A workaround to this is to use the setEditable() method instead of setEnabled().
Then again, another issue arises. You will be able to select the text even though you cannot insert or remove characters in the JTextPane. If you do not want the JTextPane to have a selection feature if it is not editable, then only way to make it work is to override its protected method processMouseEvent().
|
1 2 3 4 5 6 7 |
JTextPane txtpane = new JTextPane() { @Override protected void processMouseEvent(MouseEvent me) { if (isEditable()) super.processMouseEvent(me); } }; |
This should do the trick.
Share the post "Color Not Applied In JTextPane If Disabled"
tags: color, disable, editable, jtextpane
No Comments
Convert Mouse Position To Caret Position In A JTextComponent
Posted by blogmeister on
January 20, 2011
Converting a mouse position to the caret position of a JTextComponent can be done by using the JTextComponent method viewToModel(). This can be pretty useful if you need to set the caret position of the component when you do a right click of the mouse.
The component can apply to JTextArea, JEditorPane and JTextPane respectively.
|
1 2 |
Point p = mouseEvent.getPoint(); textpane.setCaretPosition(textpane.viewToModel(p)); |
Share the post "Convert Mouse Position To Caret Position In A JTextComponent"
tags: caret, jeditorpane, jtextarea, jtextcomponent, jtextpane, mouse, position
No Comments
Highlight Matches In JTextPane
Posted by blogmeister on
December 3, 2010
The image in this post shows how the highlight matching code works with a JTextPane. But actually, the code uses a JTextComponent so either a JEditorPane, JTextArea or JTextPane will work with this code.
Type in the word that you want to find, then press the FIND button and it will highlight all words matching it. The highlight matching is case insensitive so if you want to make it case-sensitive, just remove the part of the code where toLowerCase() is called.
To use the class, just do it like this:
|
1 2 3 |
WordEditorSearch wes = new WordEditorSearch(null, false); wes.setTextComponent(textComponentObject); wes.setVisible(true); |
And here is the source code for the WordEditorSearch class.
|
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 135 136 137 138 139 140 141 142 143 |
public class WordEditorSearch extends javax.swing.JDialog { private JTextComponent textComp; public WordEditorSearch(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { setVisible(false); } }; KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); getRootPane().registerKeyboardAction(al, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); } public void setTextComponent(JTextComponent textComp) { this.textComp = textComp; } @SuppressWarnings("unchecked") private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Text to find"); setMinimumSize(new java.awt.Dimension(125, 23)); setResizable(false); jPanel1.setPreferredSize(new java.awt.Dimension(300, 64)); jPanel1.setLayout(new java.awt.GridBagLayout()); jLabel1.setDisplayedMnemonic('t'); jLabel1.setLabelFor(jTextField1); jLabel1.setText("Text To Find:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0); jPanel1.add(jLabel1, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5); jPanel1.add(jTextField1, gridBagConstraints); jButton1.setMnemonic('f'); jButton1.setText("Find"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5); jPanel1.add(jButton1, gridBagConstraints); getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); pack(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (jTextField1.getText().length() == 0) removeHighlights(textComp); else highlight(textComp, jTextField1.getText()); } private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JTextField jTextField1; // An instance of the private subclass of the default highlight painter Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(UIManager.getColor("Table.selectionBackground")); // A private subclass of the default highlight painter class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter { public MyHighlightPainter(Color color) { super(color); } } // Creates highlights around all occurrences of pattern in textComp private void highlight(JTextComponent textComp, String pattern) { // First remove all old highlights removeHighlights(textComp); Highlighter hilite = textComp.getHighlighter(); Document doc = textComp.getDocument(); String text = null; try { text = doc.getText(0, doc.getLength()); } catch (BadLocationException ex) { Logger.getLogger(WordEditorSearch.class.getName()).log(Level.SEVERE, null, ex); } int pos = 0; // Search for pattern while ((pos = text.toLowerCase().indexOf(pattern.toLowerCase(), pos)) >= 0) { try { // Create highlighter using private painter and apply around pattern hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter); } catch (BadLocationException ex) { Logger.getLogger(WordEditorSearch.class.getName()).log(Level.SEVERE, null, ex); } pos += pattern.length(); } } // Removes only our private highlights private void removeHighlights(JTextComponent textComp) { Highlighter hilite = textComp.getHighlighter(); Highlighter.Highlight[] hilites = hilite.getHighlights(); for (int i=0; i<hilites.length; i++) { if (hilites[i].getPainter() instanceof MyHighlightPainter) { hilite.removeHighlight(hilites[i]); } } } @Override public void setVisible(boolean vis) { super.setVisible(vis); if (vis) jTextField1.setText(""); else removeHighlights(textComp); } } |











