public TextComponentPopMenu(JTextComponent textComponent) { this.textComponent = textComponent; if (textComponent != null) { popMenu = createPopMenu(); textComponent.addMouseListener(new TextMouseListener()); } }
/** * Add a working right-click menu with Cut, Copy, Paste, Delete and Select All to the given text * component. */ public TextMenu(JTextComponent component) { // Save the text component we'll decorate with our right-click menu of clipboard text commands this.component = component; component.addMouseListener( new MyMouseListener()); // Find out when the user right-clicks in the text component // Make the right-click menu cutItem = new JMenuItem("Cut"); // Make the menu items copyItem = new JMenuItem("Copy"); pasteItem = new JMenuItem("Paste"); deleteItem = new JMenuItem("Delete"); selectAllItem = new JMenuItem("Select All"); menu = new JPopupMenu(); // Make the menu menu.add(cutItem); // Add the menu items to the menu menu.add(copyItem); menu.add(pasteItem); menu.add(deleteItem); menu.addSeparator(); menu.add(selectAllItem); MyActionListener listener = new MyActionListener(); // Make the object Java will call when the user clicks cutItem.addActionListener(listener); // Register the menu items with the object copyItem.addActionListener(listener); pasteItem.addActionListener(listener); deleteItem.addActionListener(listener); selectAllItem.addActionListener(listener); }
// Installs CurrentLineHilighter for the given JTextComponent public static void install(JTextComponent c) { try { Object obj = c.getHighlighter().addHighlight(0, 0, painter); c.putClientProperty(LINE_HIGHLIGHT, obj); c.addCaretListener(caretListener); c.addMouseListener(mouseListener); } catch (BadLocationException ex) { } }
@Override public void installUI(JComponent c) { if (c instanceof JTextComponent) { if (fl != null) { ((JTextComponent) c).addFocusListener(fl); } if (ml != null) { ((JTextComponent) c).addMouseListener(ml); } } super.installUI(c); }
/** Выделение всего текста в компоненте при получении им фокуса. */ public void setTextSelectionOnFocus() { if (!isSetTextSelectionOnFocus) { textComponent.addMouseListener( new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); focusGainedByMouseClick = e.getButton() == MouseEvent.BUTTON1; focusGainedByMouseClick &= !hasFocus; } @Override public void mouseReleased(MouseEvent e) { super.mouseReleased(e); if (focusGainedByMouseClick) if (textComponent.getSelectionStart() == textComponent.getSelectionEnd()) textComponent.selectAll(); focusGainedByMouseClick = false; } }); textComponent.addFocusListener( new FocusAdapter() { @Override public void focusGained(FocusEvent e) { super.focusGained(e); if (!(focusGainedByMouseClick || hasFocus)) textComponent.selectAll(); hasFocus = true; } @Override public void focusLost(FocusEvent e) { super.focusLost(e); hasFocus = e.isTemporary(); } }); isSetTextSelectionOnFocus = true; } }
@Override public JComponent layoutDialogContent() { final JPanel contentpane = new JPanel(new MigLayout("ins 0,wrap 2", "[fill,grow]")); messageArea = new JTextPane(); messageArea.setBorder(null); messageArea.setBackground(null); messageArea.setOpaque(false); messageArea.setText(message); messageArea.setEditable(false); messageArea.putClientProperty("Synthetica.opaque", Boolean.FALSE); contentpane.add("span 2", messageArea); contentpane.add(new JLabel(_AWU.T.PASSWORDDIALOG_PASSWORDCHANGE_OLDPASSWORD())); if (BinaryLogic.containsAll(flagMask, Dialog.STYLE_LARGE)) { input1 = new JPasswordField(); input1.addKeyListener(this); input1.addMouseListener(this); contentpane.add(new JScrollPane(input1), "height 20:60:n,pushy,growy,w 250"); } else { input1 = new JPasswordField(); input1.setBorder(BorderFactory.createEtchedBorder()); input1.addKeyListener(this); input1.addMouseListener(this); contentpane.add(input1, "pushy,growy,w 250"); } contentpane.add(new JLabel(_AWU.T.PASSWORDDIALOG_PASSWORDCHANGE_NEWPASSWORD())); if (BinaryLogic.containsAll(flagMask, Dialog.STYLE_LARGE)) { input2 = new JPasswordField(); input2.addKeyListener(this); input2.addMouseListener(this); contentpane.add(new JScrollPane(input2), "height 20:60:n,pushy,growy,w 250"); } else { input2 = new JPasswordField(); input2.setBorder(BorderFactory.createEtchedBorder()); input2.addKeyListener(this); input2.addMouseListener(this); contentpane.add(input2, "pushy,growy,w 250"); } contentpane.add(new JLabel(_AWU.T.PASSWORDDIALOG_PASSWORDCHANGE_NEWPASSWORD_REPEAT())); if (BinaryLogic.containsAll(flagMask, Dialog.STYLE_LARGE)) { input3 = new JPasswordField(); input3.addKeyListener(this); input3.addMouseListener(this); contentpane.add(new JScrollPane(input3), "height 20:60:n,pushy,growy,w 250"); } else { input3 = new JPasswordField(); input3.setBorder(BorderFactory.createEtchedBorder()); input3.addKeyListener(this); input3.addMouseListener(this); contentpane.add(input3, "pushy,growy,w 250"); } return contentpane; }
public CutCopyPastePopup(JTextComponent target) { this.target = target; JMenuItem mi = new JMenuItem("Cut"); mi.addActionListener(this); mi.setActionCommand("cut"); add(mi); mi = new JMenuItem("Copy"); mi.addActionListener(this); mi.setActionCommand("copy"); add(mi); mi = new JMenuItem("Paste"); mi.addActionListener(this); mi.setActionCommand("paste"); add(mi); add(new JSeparator()); mi = new JMenuItem("Select all"); mi.addActionListener(this); mi.setActionCommand("selectall"); add(mi); target.addMouseListener( new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } @Override public void mousePressed(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { show(e.getComponent(), e.getX(), e.getY()); } } }); }
public EditMenuFeature(JTextComponent textcomp) { this.textcomp = textcomp; initPopupMenu(); initUndoFeature(); textcomp.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3 && !popup.isVisible()) { showPopup(e.getX(), e.getY()); } } public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3 && !popup.isVisible()) { showPopup(e.getX(), e.getY()); } } }); }
/** Build the interface. */ private void buildUI() { if (colorEnabled) { JTextPane text = new JTextPane(); this.textComponent = text; } else { JTextArea text = new JTextArea(); this.textComponent = text; text.setLineWrap(true); } textComponent.addMouseListener(this); textComponent.setFont(getMonospaceFont()); textComponent.setEditable(false); DefaultCaret caret = (DefaultCaret) textComponent.getCaret(); caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); document = textComponent.getDocument(); document.addDocumentListener(new LimitLinesDocumentListener(numLines, true)); JScrollPane scrollText = new JScrollPane(textComponent); scrollText.setBorder(null); scrollText.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); add(scrollText, BorderLayout.CENTER); }