-1

ok so thats what ive got

jTextField1.setBounds(136, 24, 17, 17);
jTextField1.setEditable(false);
jTextField1.setText("x");
jTextField1.setBorder(javax.swing.BorderFactory.createEmptyBorder());
jTextField1.setHorizontalAlignment(SwingConstants.CENTER);
jTextField1.setFont(new Font("Dialog", Font.PLAIN, 16));
jTextField1.setBackground(Color.WHITE);
jTextField1.setOpaque(false);
cp.add(jTextField1);

[...]

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "+") {
plus = true; // [...]

If e.getActionCommand() = + i want to change the text of the jTextField. Is that possible? What alternatives are there? thx in advance

  • Yes it's possible, simply call `setText(...)` on your JTextField, and that's it. But also don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Nov 19 '16 at 22:23
  • 1
    Please make titles more .. useful. The code itself contains the answer, as it uses `setText` - therefore, trivially and redundantly with respect to the title, the text *can* be changed. – user2864740 Nov 19 '16 at 22:25
  • 1) `jTextField1.setBounds(136, 24, 17, 17);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Nov 19 '16 at 22:40

1 Answers1

-1

EDIT: Well you already used the setText() method of the JTextField. To be able to access the JTextField from within the ActionPerformed() Method of the ActionListener the JTextField is made global.

The ActionLister and its code could look like this:

    package jTextField;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class SetTextOfJTextField extends JFrame implements ActionListener{

    boolean plus = false;
    JPanel panel = new JPanel();
    JTextField jTextField1; 

    public static void main(String[] args){
        new SetTextOfJTextField();  
    }

    public SetTextOfJTextField(){

        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


    jTextField1 = new JTextField(10);

    jTextField1.setEditable(false);
    jTextField1.setText("x");
    jTextField1.setBorder(javax.swing.BorderFactory.createEmptyBorder());

    jTextField1.setBackground(Color.WHITE);

    jTextField1.addActionListener(this);
    jTextField1.setEditable(true); // this is necessary so the text can be changed by the user!

    JButton button = new JButton("OK");
    button.addActionListener(this);


    /* configure JFrame / Jpanel*/
    panel.add(jTextField1);
    panel.add(button);
    this.add(panel);
    this.pack();
    this.setSize(200,200);
    this.setVisible(true);
    }


    public void actionPerformed(ActionEvent e) {
        System.out.println("ouch! dont click me!");
        if (jTextField1.getText().equals("+")) {
            plus = true;
            System.out.println("received plus!");
            // change text of jTextField1
            jTextField1.setText("hi i am new text!");
        } 
        else{
            jTextField1.setText("");
        }
   }
   }

Now run the code. If you enter a + sign in the Textfield and press OK the Text will change. Press the OK button once more and the text will be reset. Happy coding!