Append value to JTextField?

情到浓时终转凉″ 提交于 2020-01-25 14:27:09

问题


I want to append a value into a JTextField in Java. Something like:

String btn1="1"; 
textField.appendText(btn1);

回答1:


I think you'll find setText is the answer. Just combine the current value with the new value:

textField.setText(textField.getText() + newStringHere); 



回答2:


If you text field is not editable, you could use:

textField.replaceSelection("...");

If it is editable you might use:

textField.setCaretPosition( textField.getDocument().getLength() );
textField.replaceSelection("...");

This would be slightly more efficient (than using setText()) because you are just appending text directly to the Document and would more resemble a JTextArea.append(...).

It would only result in a single DocumentEvent - insertUpdate().

You can also access the Document directly and do the insert:

Document doc = textField.getDocument();
doc.insertString(...);

but I find this more work because you also have to catch the BadLocationException.

Simple example, that also demonstrate the use of Key Bindings:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(50, 50) );
            buttonPanel.add( button );

            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(KeyStroke.getKeyStroke(text), text);
            inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
            button.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
//      UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );

        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/28940433/append-value-to-jtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!