?
Solved

URGENT: Deleting a character

Posted on 2003-03-27
4
Medium Priority
?
181 Views
Last Modified: 2010-03-31
Hey guyz,

I have a JTextField with a JButton beside it called "Delete"

Whenever the user presses the Delete button, wherever the cursor was just before the user pressed the Delete button, the character to the left of that cursor position should get deleted.

How do I do that?

Thanks heaps.
0
Comment
Question by:java_boy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 80 total points
ID: 8223131
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class DeleteDemo extends JFrame implements ActionListener
{
     JButton delButton;
     JTextField textField;
     
     public DeleteDemo()
     {
          super("Delete Demo");
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          delButton=new JButton("Delete");
          textField=new JTextField("Enter text here!");
          getContentPane().setLayout(new GridLayout(1,2));
          getContentPane().add(delButton);
          getContentPane().add(textField);
          delButton.addActionListener(this);
          pack();
          setVisible(true);
         
     }
     
     public void actionPerformed(ActionEvent e)
     {
          int pos;
          String text,left,right;
          if(e.getSource()==delButton)
          {
               pos=textField.getSelectionStart();
               if(pos>0)
               {
                    text=textField.getText();
                    left=text.substring(0,pos-1);
                    right=text.substring(pos);
                    textField.setText(left+right);
               }
          }
     }
     public static void main(String[] args)
     {
          DeleteDemo dd=new DeleteDemo();
     }
}

0
 

Expert Comment

by:CleanupPing
ID: 9058448
java_boy:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9734958
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept mstrjev's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question