Jump to content
Sign in to follow this  
dzhax

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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")) { }

Share this post


Link to post
Share on other sites

Join the conversation

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

Guest
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.

Loading...
Sign in to follow this  

×
×
  • Create New...