1

I'm trying to make a 'delete' button that deletes either a) a single character in a text-area if pressed and released in quick succession, or b) all of the text if pressed and held down for more than 2 seconds without release.

Is this possible in Java?

Braden Best
  • 8,830
  • 3
  • 31
  • 43
  • Hi, welcome to StackOverflow! Please post some code and show us what have you tried. – msanford Dec 17 '15 at 18:28
  • 4
    On buttonDown, delete the character and start a 2-second Swing timer; On buttonUp, kill the timer. If the timer ever calls the registered ActionListener (it is invoked before the buttonUp kills it), delete the rest of the text.. – FredK Dec 17 '15 at 18:44
  • As @msanford suggested, this question is in need of serious improvement. I did what I could with the title/contents, but it's up to you to comply with the guidelines. Try [this one](http://stackoverflow.com/help/how-to-ask), for instance. – Braden Best Dec 17 '15 at 18:44
  • @FredK Seems like a solid answer to me. Would you mind expanding on it and posting it as an answer? – Braden Best Dec 17 '15 at 18:45

2 Answers2

0

In order to be able to detect long key presses from the keyboard input, you need to understand and use 2 concepts:
1. KeyListener.
2. How to get current time.
Once you understand both, just compare the times between keyPressed and keyReleased and call the proper delete action.

Community
  • 1
  • 1
Leet-Falcon
  • 2,107
  • 2
  • 15
  • 23
0

Alternativly to a Swing-Timer (watch here for example) you could use a simple SwingWorker to realize the delay. In general you should not execute a delay, i.e. by Thread.sleep(1000), on the Swing EDT, since this would block the gui (for further information ...). Furthermore you should use a MouseListener to capture other informations that you need (stop the timer when mouse is released or exits the buttona area). Here is a very short example:

public class JButtonTest extends JFrame {

    public static void main(String[] args) {
        JButtonTest x = new JButtonTest();
        JButton button = new JButton("Delete");
        button.addMouseListener(new MouseAdapter() {
            private static final long DELTA = 2000;
            private SwingWorker<Void, Void> waitingWorker;
            private Long timer;

            @Override
            public void mousePressed(MouseEvent e) {
                timer = System.currentTimeMillis();
                System.out.println("delete single char");//DO single delete here
                if (waitingWorker != null && !waitingWorker.isDone())
                    waitingWorker.cancel(true);
                waitingWorker = new SwingWorker<Void, Void>() {
                    @Override
                    protected Void doInBackground() throws Exception {
                        Thread.sleep(DELTA);
                        return null;
                    }
                    @Override
                    protected void done() {
                        if (timer != null && System.currentTimeMillis() >= timer + DELTA)
                            System.out.println("delete all text");//DO text delete here
                    }
                };
                waitingWorker.execute();
            }  
            @Override
            public void mouseReleased(MouseEvent e) {
                timer = null;
            }
            @Override
            public void mouseExited(MouseEvent e) {
                timer = null;
            }
        });

        x.add(button);
        x.setSize(100, 100);
        x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        x.setVisible(true);

    }

}