dzhax Posted June 29, 2013 Share Posted June 29, 2013 (edited) I have a program I am working on and I am not the best at java so I am probably not doing this correctly. I have a string that holds the name of an object I want to use (Example: txtCaller) I want to then take txtCaller and copy, cut, or paste the text that is currently selected in that object to the clipboard. Essentially im adding the cut, copy, paste on right click feature to my program. Here is some snippets of code: public class gui extends javax.swing.JFrame { public JPopupMenu popup; String rightClickedField, rightClickedValue, copiedTxt; public gui() { popup = new JPopupMenu(); ActionListener menuListener = new ActionListener() { public void actionPerformed(ActionEvent event) { if(event.getActionCommand() == "Cut"){ System.out.println("Action: Cut " + rightClickedValue + " from " + rightClickedField); } if(event.getActionCommand() == "Copy"){ System.out.println("Action: Copied " + rightClickedValue + " from " + rightClickedField); } if(event.getActionCommand() == "Paste"){ System.out.println("Action: Pasted to " + rightClickedField); } } }; JMenuItem item; popup.add(item = new JMenuItem("Cut")); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Copy")); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Paste")); item.addActionListener(menuListener); initComponents(); } private void txtCallerMouseClicked(java.awt.event.MouseEvent evt) { if(evt.getButton() == 3){ System.out.println("Right Click Detected - Initiating Popup Menu"); popup.show(gui.this, evt.getX() + 120, evt.getY() + 45); rightClickedField = "txtCaller"; rightClickedValue = txtCaller.getSelectedText(); } } Edited June 29, 2013 by dzhax Link to comment Share on other sites More sharing options...
davej Posted August 19, 2013 Share Posted August 19, 2013 Yeah, Java is pretty dead around here. I haven't looked at Java code in over a year. I hope I don't forget it. Link to comment Share on other sites More sharing options...
astralaaron Posted October 10, 2013 Share Posted October 10, 2013 I think I know what you are asking.... to compare strings in Java, use equals() instead of if(event.getActionCommand() == "something") { } try if(event.getActionCommand().equals("something")) { } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now