Hi everyone,
I have quite a problem. I have a text pane and three menu items which are cut copy and paste. In my textpane i have some text and an embedded icon. when i select all the contents of the textpane and click cut all the contents of the textpane dissapears which is correct but here is the kicker in that when i click paste only the text is pasted in the textpane but the embedded icon seems to have dissapeared. This is also the same with the copy action

I am actually using the textpane's cut, copy and paste api method's.

Can someone please show me some codings or tell me what i am doing wrong

My e-mail is<EMAIL SNIPPED>

Thank You

Yours Sincerely

Richard West

hi everyone,
Nobody knows how to implement this?

Richard West

import java.awt.MenuItem;
import java.awt.Point;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.TransferHandler;
import javax.swing.text.JTextComponent;

public class RightMouseMenu extends PopupMenu
{
	private JTextComponent jTextComponent;
	private String cut;
	private String copy;
	private String paste;

	
	public RightMouseMenu(JTextComponent jTextComponent)
	{
		this(jTextComponent,"Taglia", "Copia","Incolla");
	}
	
	public RightMouseMenu(JTextComponent jTextComponent, String cut, String copy, String paste)
	{
		super();
		this.jTextComponent = jTextComponent;
		this.cut = cut;
		this.copy = copy;
		this.paste = paste;
		jTextComponent.add(this);
		
		MyListner myListner = new MyListner();
		jTextComponent.addMouseListener(myListner);
		addActionListener(myListner);
	}
	
	private void resetItem()
	{
		removeAll();
		
		boolean isTestSel = jTextComponent.getSelectedText()!=null;
		boolean isEditable = jTextComponent.isEditable();

		MenuItem cutItem = new MenuItem(cut);
		cutItem.setEnabled(isTestSel);
		add(cutItem);
		
		MenuItem copyItem = new MenuItem(copy);
		copyItem.setEnabled(isTestSel && isEditable);
		add(copyItem);
		
		MenuItem pasteItem = new MenuItem(paste);
		pasteItem.setEnabled(isEditable);
		add(pasteItem);
	}

	private void copy()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		TransferHandler transferHandler = jTextComponent.getTransferHandler();
		transferHandler.exportToClipboard(jTextComponent, clipboard, TransferHandler.COPY);
	}
	
	private void paste()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		TransferHandler transferHandler = jTextComponent.getTransferHandler();
		transferHandler.importData(jTextComponent, clipboard.getContents(null));
	}

	private class MyListner extends MouseAdapter implements ActionListener
	{
		@Override
		public void mousePressed(MouseEvent e) 
		{
			if(e.getButton() ==3)
			{
				resetItem();
				Point point = jTextComponent.getMousePosition();
				if(point!=null)
					show(jTextComponent,point.x,point.y);
			}
		}
		
		public void actionPerformed(ActionEvent e)
		{
			String source = e.getActionCommand();
			if(source.equals(copy))
				copy();
		
			else if(source.equals(paste))
				paste();
			
			else if(source.equals(cut))
			{
				copy();
				jTextComponent.replaceSelection("");
			}
		}
	}
}

You just replied to 5 years old thread. Was it worth it?

You just replied to 5 years old thread. Was it worth it?

here is a fuller version.
bye

import java.awt.MenuItem;
import java.awt.Point;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.TransferHandler;
import javax.swing.text.JTextComponent;

public class RightMouseMenu extends PopupMenu
{
	private JTextComponent jTextComponent;
	private String cut;
	private String copy;
	private String paste;
	private String delete;
	private String selectAll;

	
	public RightMouseMenu(JTextComponent jTextComponent)
	{
		this(jTextComponent,"Taglia", "Copia","Incolla","Elimina","Seleziona Tutto");
	}
	
	public RightMouseMenu(JTextComponent jTextComponent, String cut, String copy, String paste, String delete, String selectAll)
	{
		super();
		this.jTextComponent = jTextComponent;
		this.cut = cut;
		this.copy = copy;
		this.paste = paste;
		this.delete = delete;
		this.selectAll = selectAll;
		jTextComponent.add(this);
		
		MyListner myListner = new MyListner();
		jTextComponent.addMouseListener(myListner);
		addActionListener(myListner);
	}

	private void resetItem()
	{
		removeAll();
		
		boolean isTestSel = jTextComponent.getSelectedText()!=null;
		boolean isEditable = jTextComponent.isEditable();

		addMenuItem(cut, isTestSel && isEditable);
		addMenuItem(copy, isTestSel);
		addMenuItem(paste, isEditable);
		addMenuItem(delete, isEditable);
		addSeparator();
		addMenuItem(selectAll, jTextComponent.isEnabled());
	}
	
	
	private void addMenuItem(String label, boolean isEnabled)
	{
		MenuItem menuItem = new MenuItem(label);
		menuItem.setEnabled(isEnabled);
		add(menuItem);
	}

	private void copy()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		TransferHandler transferHandler = jTextComponent.getTransferHandler();
		transferHandler.exportToClipboard(jTextComponent, clipboard, TransferHandler.COPY);
	}
	
	private void paste()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		TransferHandler transferHandler = jTextComponent.getTransferHandler();
		transferHandler.importData(jTextComponent, clipboard.getContents(null));
	}

	private class MyListner extends MouseAdapter implements ActionListener
	{
		@Override
		public void mousePressed(MouseEvent e) 
		{
			if(e.getButton() ==3)
			{
				resetItem();
				Point point = jTextComponent.getMousePosition();
				if(point!=null)
					show(jTextComponent,point.x,point.y);
			}
		}
		
		public void actionPerformed(ActionEvent e)
		{
			String source = e.getActionCommand();
			if(source.equals(copy))
				copy();
		
			else if(source.equals(paste))
				paste();
			
			else if(source.equals(cut))
			{
				copy();
				jTextComponent.replaceSelection("");
			}
			
			else if(source.equals(delete))
				jTextComponent.replaceSelection("");
			
			else if(source.equals(selectAll))
				jTextComponent.selectAll();
		}
	}
}
commented: For lack of respect -1

You just replied to 5 years old thread. Was it worth it?

The thread is still on the server and is open, so anyone can respond. The answer will be helpful to those of the network the solution of that problem. Your comment is not smart.

The thread is still on the server and is open, so anyone can respond. The answer will be helpful to those of the network the solution of that problem. Your comment is not smart.

Respond yes, But give away the answer no. This is not a forum where the memebers do other people's work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.