Jump to content

Java Noob: Value of string to define the name of an object to use


Recommended Posts

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 by dzhax
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...