Home > Blockchain >  Java swing button that behaves like buttons in old phones while typing
Java swing button that behaves like buttons in old phones while typing

Time:12-15

I want to write a program that in Java Swing where I have let's say 3 JButtons with labels "abc", "def", "ghi" and a JTextField. If I click the "abc" button 1 time it should insert "a" letter to the text field and if I click it 2 times quickly it should add "b" letter and 3 times "c" letter. The gap between every click while clicking, for example 3 times quickly, can't be longer than 1.5. The program should behave like those good old phones where it was an effort to write a message to someone.

Here is my method. Right now it just displays the number of time I clicked. When I click 3 times it prints:

1
2
3

instead of just:

3

public void mouseClicked(MouseEvent event) {
    Object obj = event.getSource();
    if (obj instanceof JButton) {
        JButton button = (JButton) obj;
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime >= 1500) {
            clickCount = 0;
        }
        clickCount  ;
        lastClickTime = currentTime;
        System.out.println(clickCount);
    }
}

I tried to do it in many ways but I end up getting the same result. Any help will be appreciated.

CodePudding user response:

Try the following.
(Explanations after the code.)

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CliCount extends MouseAdapter {
    private JTextField  txtFld;

    @Override // java.awt.event.MouseAdapter
    public void mouseClicked(MouseEvent e) {
        String text = txtFld.getText();
        String actionCommand = ((JButton) e.getSource()).getActionCommand();
        int count = e.getClickCount();
        if (count > 3) {
            count = 3;
        }
        int index = count - 1;
        String letter = actionCommand.substring(index, index   1);
        String newText;
        if (count == 1) {
            newText = text;
        }
        else {
            newText = text.substring(0, text.length() - 1);
        }
        txtFld.setText(newText   letter);
    }

    private void buildAndDisplayGui() {
        JFrame frame = new JFrame("Phone");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createTxtFld(), BorderLayout.CENTER);
        frame.add(createButtons(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JButton createButton(String text) {
        JButton button = new JButton(text);
        button.addMouseListener(this);
        return button;
    }

    private JPanel createButtons() {
        JPanel panel = new JPanel();
        panel.add(createButton("abc"));
        panel.add(createButton("def"));
        panel.add(createButton("ghi"));
        return panel;
    }

    private JPanel createTxtFld() {
        JPanel panel = new JPanel();
        txtFld = new JTextField(20);
        panel.add(txtFld);
        return panel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new CliCount().buildAndDisplayGui());
    }
}

Method mouseClicked is called each time the mouse is clicked. The click count depends on the time between consecutive clicks. Hence if you click the mouse twice so that it is considered as a double click, method mouseClicked will be called twice — once with click count equal to one and then again with click count equal to two.

In the above code, if the click count is one, the first letter on the button that was clicked is appended to the text in txtFld.

If the click count is two, the last letter of the text in txtFld is removed and replaced by the second letter on the button that was clicked.

Similarly when the click count is three. Note that if the count is greater than three then it is set to three.

  • Related