#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    How to create a Right Click Menu for a Text Field in Java?


    I am a novice Java programmer. I was trying to edit a Java application, the application basically is a download manager. It has a text field where in we have to paste the URL to download that particular file. Now my problem is that I am trying to add a mouse event so that I can right click and "paste" the URL into the Text Field. I would also like to add "cut" and "copy" options for the right click. But I have tried many things and failed to achieve what I wanted.

    It would be of great help if anyone could help me get it this done by giving me an example of the code. I would like to know the simplest way to get a "cut", "copy" & "paste" menu by right clicking on a text field.


    Thank you.

    Partial Source code of the download manager application where I am trying to implement this.
    Code:
    public class FTPDownloader extends JFrame implements Observer {
    
    private JTextField addTextField = new JTextField(40); //I want to implement it to this text field.
    
    private DownloadsTableModel tableModel = new DownloadsTableModel();
    
    private JTable table;
    
    private JButton pauseButton = new JButton("Pause");
    
    private JButton resumeButton = new JButton("Resume");
    
    private JButton cancelButton, clearButton;
    
    private JLabel saveFileLabel = new JLabel();
    
    private Download selectedDownload;
    
    private boolean clearing;
    
    public FTPDownloader() {
    setTitle("FTP Download Manager");
    setSize(740, 480);
    setLocationRelativeTo(null); 
    addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    JMenuItem fileExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
    JMenuItem fileOpenMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
    fileExitMenuItem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    });
    fileOpenMenuItem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    actionSaveTo();
    }
    });
    fileMenu.add(fileOpenMenuItem);
    fileMenu.add(fileExitMenuItem);
    menuBar.add(fileMenu);
    setJMenuBar(menuBar);
    
    // Set up add panel.
    JPanel addPanel = new JPanel(new BorderLayout());
    
    JPanel targetPanel = new JPanel(new BorderLayout());
    targetPanel.add(addTextField, BorderLayout.WEST);
    JButton addButton = new JButton("Add Download");
    addButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    actionAdd();
    }
    });
    
    targetPanel.add(addButton, BorderLayout.EAST);
    
    JPanel destinationPanel = new JPanel(new BorderLayout());
    saveFileLabel.setText("File:");
    destinationPanel.add(saveFileLabel, BorderLayout.WEST);
    
    JButton saveFileButton = new JButton("Download To");
    saveFileButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    actionSaveTo();
    }
    });
    destinationPanel.add(saveFileButton, BorderLayout.EAST);
    addPanel.add(destinationPanel, BorderLayout.NORTH);
    addPanel.add(targetPanel, BorderLayout.SOUTH);
    
    // Set up Downloads table.
    
    table = new JTable(tableModel);
    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
    tableSelectionChanged();
    }
    });
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
    ProgressRenderer renderer = new ProgressRenderer(0, 100);
    renderer.setStringPainted(true); // show progress text
    table.setDefaultRenderer(JProgressBar.class, renderer);
    
    table.setRowHeight((int) renderer.getPreferredSize().getHeight());
    
    JPanel downloadsPanel = new JPanel();
    downloadsPanel.setBorder(BorderFactory.createTitledBorder("Downloads"));
    downloadsPanel.setLayout(new BorderLayout());
    downloadsPanel.add(new JScrollPane(table), BorderLayout.CENTER);
    
    JPanel buttonsPanel = new JPanel();
    
    pauseButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    actionPause();
    }
    });
    pauseButton.setEnabled(false);
    buttonsPanel.add(pauseButton);
    
    resumeButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    actionResume();
    }
    });
    resumeButton.setEnabled(false);
    buttonsPanel.add(resumeButton);
    cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    actionCancel();
    }
    });
    cancelButton.setEnabled(false);
    buttonsPanel.add(cancelButton);
    clearButton = new JButton("Clear");
    clearButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    actionClear();
    }
    });
    clearButton.setEnabled(false);
    buttonsPanel.add(clearButton);
    
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(addPanel, BorderLayout.NORTH);
    getContentPane().add(downloadsPanel, BorderLayout.CENTER);
    getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
    }
    
    private void actionSaveTo()
    {
    
    JFileChooser jfchooser = new JFileChooser();
    
    jfchooser.setApproveButtonText("OK");
    jfchooser.setDialogTitle("Save To");
    jfchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    
    int result = jfchooser.showOpenDialog(this);
    File newZipFile = jfchooser.getSelectedFile();
    System.out.println("importProfile:" + newZipFile);
    this.saveFileLabel.setText(newZipFile.getPath());
    
    }
    
    private void actionAdd() {
    URL verifiedUrl = verifyUrl(addTextField.getText());
    if (verifiedUrl != null) {
    tableModel.addDownload(new Download(verifiedUrl, saveFileLabel.getText()));
    addTextField.setText(""); // reset add text field
    } else {
    JOptionPane.showMessageDialog(this, "Invalid Download URL", "Error",
    JOptionPane.ERROR_MESSAGE);
    }
    }
    
    private URL verifyUrl(String url) {
    if (!url.toLowerCase().startsWith("http://"))
    return null;
    
    
    URL verifiedUrl = null;
    try {
    verifiedUrl = new URL(url);
    } catch (Exception e) {
    return null;
    }
    
    if (verifiedUrl.getFile().length() < 2)
    return null;
    
    return verifiedUrl;
    }
    
    private void tableSelectionChanged() {
    if (selectedDownload != null)
    selectedDownload.deleteObserver(FTPDownloader.this);
    
    if (!clearing && table.getSelectedRow() > -1) {
    selectedDownload = tableModel.getDownload(table.getSelectedRow());
    selectedDownload.addObserver(FTPDownloader.this);
    updateButtons();
    }
    }
    
    private void actionPause() {
    selectedDownload.pause();
    updateButtons();
    }
    
    private void actionResume() {
    selectedDownload.resume();
    updateButtons();
    }
    
    private void actionCancel() {
    selectedDownload.cancel();
    updateButtons();
    }
    
    private void actionClear() {
    clearing = true;
    tableModel.clearDownload(table.getSelectedRow());
    clearing = false;
    selectedDownload = null;
    updateButtons();
    }
    
    private void updateButtons() {
    if (selectedDownload != null) {
    int status = selectedDownload.getStatus();
    switch (status) {
    case Download.DOWNLOADING:
    pauseButton.setEnabled(true);
    resumeButton.setEnabled(false);
    cancelButton.setEnabled(true);
    clearButton.setEnabled(false);
    break;
    case Download.PAUSED:
    pauseButton.setEnabled(false);
    resumeButton.setEnabled(true);
    cancelButton.setEnabled(true);
    clearButton.setEnabled(false);
    break;
    case Download.ERROR:
    pauseButton.setEnabled(false);
    resumeButton.setEnabled(true);
    cancelButton.setEnabled(false);
    clearButton.setEnabled(true);
    break;
    default: // COMPLETE or CANCELLED
    pauseButton.setEnabled(false);
    resumeButton.setEnabled(false);
    cancelButton.setEnabled(false);
    clearButton.setEnabled(true);
    }
    } else {
    pauseButton.setEnabled(false);
    resumeButton.setEnabled(false);
    cancelButton.setEnabled(false);
    clearButton.setEnabled(false);
    }
    }
    
    public void update(Observable o, Object arg) {
    // Update buttons if the selected download has changed.
    if (selectedDownload != null && selectedDownload.equals(o))
    updateButtons();
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    If you want to work on changing the way a textfield works, I suggest you work with a small, simple program with only a textfield in a frame and get rid of the rest of the non-related code.
    Last edited by NormR; July 18th, 2013 at 07:46 AM.

IMN logo majestic logo threadwatch logo seochat tools logo