简体   繁体   中英

Add & Remove character to text field with one button

I want to make an add & remove ± operator with one button.

So at the beginning ' - ' is supposed to be not available. When I click on ' - ', it should get added in the first line of the text field. If it is already present and we click on it again, it should get removed. How do I do this?

This is the code I have written so far:

private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {                                      
    jTextField.setText( "-" + jTextField.getText());
}

If there is no text, what text is to be added?

Try this out:

    private void jButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {
            if(jTextField.getText().length() != 0)
            {
                jTextField.setText("");
            }
            else
            {
                //Set the text from wherever you are receiving it
            }
    }

When your field is empty, you can set the text to whatever you want, and if it is not, it will be made empty.

did not test this, but it should work:

b.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        if (textField.getText().charAt(0) == '-') {
            String toSet = textField.getText();
            toSet = toSet.substring(0, 0).replace("-", "");
            textField.setText(toSet);
        } else {
            textField.setText("-" + textField.getText());
        }

    }

});

feel free to ask if you have questions

try this:

private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {
        String text = jTextField.getText();
        if (text != null && text.indexOf("-") == 0) {
            text = "+" + text.substring(1, text.length());
        } 
        else if (text != null && text.indexOf("+") == 0) {
            text = "-" + text.substring(1, text.length());
        }
        else {
            text = "-"+(text != null ? text : "");
        }
        jTextField.setText(text);
    }

I'd add on a new ActionListener to the button and define it's code with an @Override statement. This should do the trick:

button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String fieldText = textField.getText();

                if (fieldText.substring(0,1).equals("-")){
                    textField.setText(fieldText.substring(1, fieldText.length()));
                } else {
                    textField.setText("-" + fieldText);
                }
            }
        });

A different take on the idea which is a little more efficient, as you're not create so many temporary String objects

btn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Document doc = field.getDocument();
        try {
            if (doc.getLength() > 0 && doc.getText(0, 1).equals("-")) {
                doc.remove(0, 1);
            } else {
                doc.insertString(0, "-", null);               
            }
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM