Results 1 to 10 of 10
- 07-01-2008, 02:09 PM #1
- 07-01-2008, 04:13 PM #2
Suggestions: Read a java text book. Do a search online for sample code.
- 07-01-2008, 08:51 PM #3
You can find this in TextComponentDemo.java on Text Component Features.
- 07-01-2008, 11:25 PM #4
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 thatJava Code:JTextArea textArea=new JTextArea();
actionPerformed(ActionEvent e) for copy command:Java Code://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 paste command:Java Code://copy command if(e.getActionCommand().equals("Copy")){ String selection =textArea.getSelectedText(); if(selection==null){ return; } StringSelection clipString=new StringSelection(selection); clipboard.setContents(clipString, clipString); }
Java Code://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-01-2008, 11:27 PM #5
The selection option for JTextArea is :
Java Code:textArea.selectAll(); String selectedText=textArea.getSelectedText();
- 07-02-2008, 02:25 AM #6
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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
Java Code:Action cutAction = textArea.getActionMap().get(DefaultEditorKit.cutAction); cutItem = editMenu.add(cutAction); cutItem.setText("Cut");Last edited by Eku; 07-02-2008 at 02:27 AM.
Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 07-02-2008, 11:11 AM #7
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:)
- 07-02-2008, 02:01 PM #8
Would the cursor be in the component that has the focus?
- 07-02-2008, 08:16 PM #9
TextAction was designed for this. The nested classes in DefaultEditorKit extend it.
Java Code: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, 10:15 AM #10
Similar Threads
-
How To Edit/Add JSP Pages in NetBeans IDE
By JavaForums in forum NetBeansReplies: 2Last Post: 02-17-2009, 11:14 AM -
React to menu action and checkbox menu
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:50 PM -
how to create Popup Menu with Sub Menu while right-clicking the JTree Node??
By Kabiraa in forum AWT / SwingReplies: 7Last Post: 05-09-2008, 07:54 AM -
TextArea additable and uneditable (copy/past problem)
By qwerty55 in forum Advanced JavaReplies: 0Last Post: 01-19-2008, 11:41 PM -
how to edit lines.
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks