Results 1 to 15 of 15
Thread: cut/copy/paste in JTextField
- 09-24-2011, 09:22 PM #1
cut/copy/paste in JTextField
here r some problems...
when i'm writing tf.cut(); before tf.copy(); then only cut method works not copy. & same happens in vice versa.
also paste method is not working.
see bold part of the program.
can anyone please help...??
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class Assign1 implements ActionListener { JFrame fr; JTextField tf; JButton b1,b2,b3; Assign1() { fr=new JFrame(); fr.setLayout(null); tf=new JTextField(); b1=new JButton("cut"); b2=new JButton("copy"); b3=new JButton("paste"); tf.setBounds(20,35,200,30); b1.setBounds(20,80,55,50); b2.setBounds(80,80,65,50); b3.setBounds(160,80,75,50); fr.add(b1); fr.add(b2); fr.add(b3); fr.add(tf); fr.setSize(250,250); fr.setVisible(true); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent arg0) { [B]tf.setSelectionStart(5); tf.setSelectionEnd(10); tf.cut(); tf=new JTextField(); tf.copy(); tf=new JTextField(); tf.getSelectedText(); tf.paste(); tf=new JTextField(); [/B] } public static void main(String[] args) { new Assign1(); } }Last edited by Fubarable; 09-24-2011 at 09:36 PM. Reason: Moderator edit: code tags added to improve readability
-
Re: cut/copy/paste in JTextField
Moderator edit: code tags added to original post to allow code to retain formatting and be readable.
- Why are you creating new JTextFields and assigning them to tf? What is the purpose of creating a textfield that is never displayed and contains no text?
- And when you fix the above issue, a next issue is do you know what cut does with the selected text when its called? The API will answer this for you.
- 09-24-2011, 09:42 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: cut/copy/paste in JTextField
Creating new text fields won't do anything. The text field is not automatically added to the GUI.
I would suggest for your test you create 2 text fields that are visible on the GUI. Then you cut/copy/paste from the first text field to the second text field.
Edit:
Fubarable is the faster typist :)Last edited by camickr; 09-24-2011 at 09:44 PM.
- 09-24-2011, 09:52 PM #4
Re: cut/copy/paste in JTextField
that sounds gud camickr...
and also how this paste will work...??
-
Re: cut/copy/paste in JTextField
- 09-24-2011, 09:57 PM #6
Re: cut/copy/paste in JTextField
yes i got it...
thanks for ur support....
- 09-24-2011, 10:11 PM #7
Re: cut/copy/paste in JTextField
oops...
paste method is working but what about cut and copy.
still if i'm writing cut method first then copy is also working like cut. and same in case of vice versa...
what to do with with that...?
-
Re: cut/copy/paste in JTextField
Again, please elaborate. Show us your latest code, preferably a smallest compilable and runnable example that demonstrates your problem, that we can run, and that contains no extraneous code unrelated to the problem at hand -- an SSCCE. Also, please use code tags when posting code:
[code]
your code goes here
[/code]Last edited by Fubarable; 09-24-2011 at 10:28 PM.
- 09-25-2011, 05:22 AM #9
Re: cut/copy/paste in JTextField
this is the code.
i don't understand how this paste method is working or works...
please help.
i want, when i click on cut it simply cut it from tf1; when i click on copy, it only copy the text from tf1. & on clicking on paste it will paste it in tf2 TextField.
<code>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Assign1 implements ActionListener
{
JFrame fr;
JTextField tf1,tf2;
JButton b1,b2,b3;
Assign1()
{
fr=new JFrame();
fr.setLayout(null);
tf1=new JTextField();
tf2=new JTextField();
b1=new JButton("cut");
b2=new JButton("copy");
b3=new JButton("paste");
tf1.setBounds(20,35,100,30);
tf2.setBounds(130,35,100,30);
b1.setBounds(20,80,55,50);
b2.setBounds(80,80,65,50);
b3.setBounds(150,80,70,50);
fr.add(b1);
fr.add(b2);
fr.add(b3);
fr.add(tf1);
fr.add(tf2);
fr.setSize(250,250);
fr.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae) {
tf1.setSelectionStart(5);
tf1.setSelectionEnd(10);
tf1.cut();
tf1.copy();
tf2.getSelectedText();
tf2.paste();
}
public static void main(String[] args) {
new Assign1();
}
}
</code>Last edited by gauravmanral; 09-25-2011 at 05:30 AM.
-
Re: cut/copy/paste in JTextField
Please edit your post above and place the code tags in as I described in post number 8 in this thread.
Also, why are you calling copy() after calling cut()?
- 09-25-2011, 05:31 AM #11
Re: cut/copy/paste in JTextField
i want to work all 3 methods separately that's why i called copy method too.
- 09-25-2011, 05:33 AM #12
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: cut/copy/paste in JTextField
Then you need 3 separate ActionListeners. For example to do the copy you can use something like:i want, when i click on cut it simply cut it from tf1; when i click on copy, it only copy the text from tf1. & on clicking on paste it will paste it in tf2 TextField.
Also learn how to use LayoutManagers. You should not be using a null layout to position components on a GUI.Java Code:b1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { tf1.copy(); } });Last edited by camickr; 09-25-2011 at 05:37 AM.
-
Re: cut/copy/paste in JTextField
-
Re: cut/copy/paste in JTextField
Myself, I'd use AbstractActions, but the result is the same...
But again to use code tags here on this forum, you need to use square brackets [], not angle brackets <>. For the benefit of future readers of this forum, please re-edit your previous posts to add the appropriate tags. I've already done so for your first post; please do it for the rest.Java Code:import java.awt.GridLayout; import java.awt.event.ActionEvent; import javax.swing.*; @SuppressWarnings("serial") public class CutCopyPaste extends JPanel { private JTextField srcField = new JTextField(10); private JTextField destField = new JTextField(10); public CutCopyPaste() { JPanel topPanel = new JPanel(); topPanel.add(srcField); topPanel.add(destField); JPanel bottomPanel = new JPanel(new GridLayout(1, 0, 10, 0)); bottomPanel.add(new JButton(new CutAction())); bottomPanel.add(new JButton(new CopyAction())); bottomPanel.add(new JButton(new PasteAction())); int eb = 5; setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb)); setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); add(topPanel); add(bottomPanel); } private class CutAction extends AbstractAction { public CutAction() { super("Cut"); } @Override public void actionPerformed(ActionEvent e) { srcField.cut(); } } private class CopyAction extends AbstractAction { public CopyAction() { super("Copy"); } @Override public void actionPerformed(ActionEvent e) { srcField.copy(); } } private class PasteAction extends AbstractAction { public PasteAction() { super("Paste"); } @Override public void actionPerformed(ActionEvent e) { destField.paste(); } } private static void createAndShowGui() { CutCopyPaste mainPanel = new CutCopyPaste(); JFrame frame = new JFrame("CutCopyPaste"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
- 09-25-2011, 07:07 AM #15
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
JTable copy and paste problem on Mac
By willlotr in forum AWT / SwingReplies: 4Last Post: 04-07-2011, 07:15 PM -
Copy, Cut and Paste Problems!
By bones in forum New To JavaReplies: 4Last Post: 07-17-2009, 10:18 AM -
Cut, copy and Paste in JTextPane
By Gudradain in forum AWT / SwingReplies: 1Last Post: 01-03-2009, 06:43 AM -
Problems with copy paste
By fredand44 in forum EclipseReplies: 0Last Post: 12-17-2008, 04:14 PM -
How to copy and paste data with the clipboard
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:35 PM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks