|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-01-2008, 03:09 PM
|
 |
Member
|
|
Join Date: May 2008
Location: India,Kolkatta
Posts: 44
|
|
|
[SOLVED] Cut,Past,copy in edit menu
Hi all,
i just want to create an edit menu which will contain cut,copy paste,selectall option, and which will work.
Please suggest
__________________
Thanks & Regards
Subroto Bhattacharjee To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-01-2008, 05:13 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,487
|
|
|
Suggestions: Read a java text book. Do a search online for sample code.
|
|

07-01-2008, 09:51 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
You can find this in TextComponentDemo.java on Text Component Features.
|
|

07-02-2008, 12:25 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 153
|
|
For my diploma work at university i made java editor,so to each of JMenutItem pass with ActionListener,I attach the code in that case if you use JTextArea
actionPerformed(ActionEvent e) for cut command:
Assuming that
JTextArea textArea=new JTextArea();
//cut command
if(e.getActionCommand().equals("Cut")){
String selection=textArea.getSelectedText();
if(selection==null){
return;
}
StringSelection clipString=new StringSelection(selection);
clipboard.setContents(clipString,clipString);
textArea.replaceSelection("");
}
actionPerformed(ActionEvent e) for copy command:
//copy command
if(e.getActionCommand().equals("Copy")){
String selection =textArea.getSelectedText();
if(selection==null){
return;
}
StringSelection clipString=new StringSelection(selection);
clipboard.setContents(clipString, clipString);
}
actionPerformed(ActionEvent e) for paste command:
//paste command
if(e.getActionCommand().equals("Paste")){
Transferable clip_data=clipboard.getContents(this);
try{
String clip_string=(String)clip_data.getTransferData(DataFlavor.stringFlavor);
textArea.replaceSelection(clip_string);
}catch(Exception excpt){
}
}
|
|

07-02-2008, 12:27 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 153
|
|
The selection option for JTextArea is :
textArea.selectAll();
String selectedText=textArea.getSelectedText();
|
|

07-02-2008, 03:25 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 199
|
|
there is already a function for cut-copy-paste in java. you just need to call it ^_^
I also made a javapad almost like notepad but can compile and run java ^_^
But cant handle errors T_T
Here is a sample code from my javapad, it is for cut
Action cutAction = textArea.getActionMap().get(DefaultEditorKit.cutAction);
cutItem = editMenu.add(cutAction);
cutItem.setText("Cut");
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
Last edited by Eku : 07-02-2008 at 03:27 AM.
|
|

07-02-2008, 12:11 PM
|
 |
Member
|
|
Join Date: May 2008
Location: India,Kolkatta
Posts: 44
|
|
|
Ya i know how to create Edit option for TextArea.
but i want to do something different.
For example if there is a 10 fields, then this TextArea concept will not work cause we dont know, in which field my cursor is and since it contains 10 fields, so it is not possible to check all the fields .
Please suggest.. me for creating an edit menu in the JFrame so that it can be implemented in all the fields.
__________________
Thanks & Regards
Subroto Bhattacharjee To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-02-2008, 03:01 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,487
|
|
|
Would the cursor be in the component that has the focus?
|
|

07-02-2008, 09:16 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
TextAction was designed for this. The nested classes in DefaultEditorKit extend it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextFocus {
private JMenuBar getMenuBar() {
JMenu menu = new JMenu("edit");
//DefaultEditorKit kit = new DefaultEditorKit();
//Action[] actions = kit.getActions();
//for(int i = 0; i < actions.length; i++)
// System.out.println(actions[i].getValue(Action.NAME));
Action action = new DefaultEditorKit.CutAction();
action.putValue(Action.NAME, "cut");
menu.add(action);
action = new DefaultEditorKit.CopyAction();
action.putValue(Action.NAME, "copy");
menu.add(action);
action = new DefaultEditorKit.PasteAction();
action.putValue(Action.NAME, "paste");
menu.add(action);
menu.add(new SelectAction());
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
return menuBar;
}
private class SelectAction extends TextAction {
SelectAction() {
super(DefaultEditorKit.selectAllAction);
super.putValue(NAME, "selectAll");
}
public void actionPerformed(ActionEvent e) {
getFocusedComponent().selectAll();
}
}
private JPanel getContent() {
String[] strs = {
"red", "green", "yellow", "blue"
};
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
for(int i = 0; i < 4; i++) {
JTextField tf = new JTextField(strs[i], 12);
gbc.gridwidth = ((i+1) % 2 == 0) ? gbc.REMAINDER : 1;
panel.add(tf, gbc);
}
return panel;
}
public static void main(String[] args) {
TextFocus test = new TextFocus();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(test.getMenuBar());
f.add(test.getContent());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
|
|

07-03-2008, 11:15 AM
|
 |
Member
|
|
Join Date: May 2008
Location: India,Kolkatta
Posts: 44
|
|
|
Thanks Hardwired,
This code really works.It helped me alot.
__________________
Thanks & Regards
Subroto Bhattacharjee To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|