?
Solved

URGENT: Deleting a character

Posted on 2003-03-27
4
Medium Priority
?
182 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
3 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

571 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