Reputation:
I am trying to make calculator and i have an issue with JTextField. When I click numbers(JButtons) like 1,2,3,4,5 , they appears on JTextField like 54321. So, how can i change this to show 12345 instead of 54321?
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton) e.getSource();
String displayValue = parent.getDisplayValue();
String clickedBtnValue = clickedButton.getText();
parent.setDisplayValue(clickedBtnValue + displayValue);
contentPane = new JPanel();
textField = new JTextField(30);
textField.setAlignmentX(Component.LEFT_ALIGNMENT);
contentPane.add(textField);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setBounds(10, 11, 152, 32);
textField.setColumns(1);
textField.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
textField.addActionListener(eng);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 184, 312);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Upvotes: 1
Views: 112
Reputation: 324118
Did you really think about this BEFORE you asked the question?
You are in control of your code so you should understand what your code is doing:
parent.setDisplayValue(clickedBtnValue + displayValue);
The code is doing exactly what you tell it to do - the button text is appended before the existing text.
If you don't like it displayed that way then you can try:
parent.setDisplayValue(displayValue + clickedBtnValue);
If you want a better solution, you can use the replaceSelection(...)
method as demonstrated in: How to add a shortcut key for a jbutton in java?
Upvotes: 1