Text selection conflict between JTextPane and JTextField

Why can't text in JTextPane be programmatically selected if JTextField is present? I think something with focus. thanks.

import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.beans.PropertyChangeListener; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JTextPane; import javax.swing.KeyStroke; public class align extends JFrame { private align() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addPane(this, "one"); pack(); setVisible(true); } public static void main(String[] args) { align t = new align(); } private void addPane(JFrame frame, String name) { JPanel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT)); // if the next line is disabled, then the text is JTextPane is correctly highlighted.,, panel.add(makeField("line1")); JTextPane p = new JTextPane(); p.setText("abcdef"); p.setSelectionStart(2); p.setSelectionEnd(4); p.setFocusable(true); p.requestFocus(); p.requestDefaultFocus(); panel.add(p); frame.getContentPane().add(panel); } private JComponent makeField(String name) { JTextField textArea = new JTextField(); textArea.setText(name); textArea.setEditable(false); return textArea; } } 

EDIT:

Got it to display the selected text, triggering the key event after creating the frame. A better (longer) solution would be to have a read-only text file with custom Highlighter and DocumentListener that supports updating the clipboard to Ctrl-C.

  Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent( new KeyEvent(textPane, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_TAB)); 
+1
java swing
source share
4 answers

Selected text in JTextPane . The trouble is that it does not give any visual indication if the component is not focused. Try tabbing with the component after the GUI is displayed.


So, should I mention this?

No, you have to reconfigure your broken GUI, which uses text selection to determine the text of interest. Selecting text is more for the end user than for the application.

This JTextPane is a component that supports formatting, possibly making the text bold or italic, for example.

+3
source share

Perhaps you should use Highlighter so that the highlight appears in all text components:

 Highlighter.HighlightPainter yellow = new DefaultHighlighter.DefaultHighlightPainter( Color.YELLOW ); try { textPane.getHighlighter().addHighlight(2, 4, yellow); } catch(BadLocationException ble) { System.out.println(ble); } 
+3
source share
 p.getCaret().setSelectionVisible(true); 
+3
source share

Just for fun (after all, on Friday :-) I followed Stanislav's comment, extending DefaultCaret to keep the selection visible for unfocused textComponents.

Main ideas

  • support for two highlight decorations: focused selection, focused selection
  • keep the highlight selection as close as possible to the default LAF, which comes down to reusing selectionPainter (only available for coughing, coughing .. reflection)
  • fool yourself into believing that choice is always visible

     public static class WrappingCaret extends DefaultCaret { private DefaultCaret delegate; private HighlightPainter focusedSelectionPainter; private HighlightPainter unfocusedSelectionPainter; private boolean focusedSelectionVisible; public WrappingCaret(JTextComponent target) { installDelegate((DefaultCaret) target.getCaret()); target.setCaret(this); } private void installDelegate(DefaultCaret delegate) { this.delegate = delegate; setBlinkRate(delegate.getBlinkRate()); } private void installSelectionPainters() { if (delegate instanceof BasicCaret) { installDefaultPainters(); } else { try { Method method = delegate.getClass().getDeclaredMethod( "getSelectionPainter"); method.setAccessible(true); focusedSelectionPainter = (HighlightPainter) method .invoke(delegate); Constructor<?>[] constructors = focusedSelectionPainter .getClass().getDeclaredConstructors(); constructors[0].setAccessible(true); unfocusedSelectionPainter = (HighlightPainter) constructors[0] .newInstance(getUnfocusedSelectionColor()); } catch (Exception e) { installDefaultPainters(); } } } private Color getUnfocusedSelectionColor() { Color first = getComponent().getSelectionColor(); // create a reasonable unfocusedSelectionColor return PaintUtils.setAlpha(first, 125); } private void installDefaultPainters() { focusedSelectionPainter = super.getSelectionPainter(); unfocusedSelectionPainter = new DefaultHighlightPainter( getUnfocusedSelectionColor()); } /** * @inherited <p> */ @Override public void install(JTextComponent c) { super.install(c); installSelectionPainters(); setSelectionVisible(isSelectionVisible()); } /** * @inherited <p> */ @Override public void setSelectionVisible(boolean vis) { focusedSelectionVisible = vis; super.setSelectionVisible(!isSelectionVisible()); super.setSelectionVisible(true); } /** * @inherited <p> */ @Override protected HighlightPainter getSelectionPainter() { return focusedSelectionVisible ? focusedSelectionPainter : unfocusedSelectionPainter; } } 

Enjoy it!

+3
source share

All Articles