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 post
Share on other sites
  • 1 month later...
  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...