Another question about TextField

Please advice if it it's possible to enter text into TextField and entered value will be available as a variable without dialog entering button OK
gbcbrAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
just use the setText() method
0
 
for_yanCommented:
You can make keylistener and listen to every character as it is entered
0
 
objectsCommented:
You can use the getText() method to get the value of a text field at any time
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
objectsCommented:
you can also use a DocumentListener to listen for changes in a text field
http://download.oracle.com/javase/tutorial/uiswing/events/documentlistener.html

0
 
for_yanCommented:
If you implement KeyListener in your class and
addKeyListener to your TextField then in the keyTyped  method you can collect
all your keys into string

You can aslo addActionListener - then when user enters carrigae return your
actionPerformed method will be trggered and you can read input

No OK button is necessary
0
 
gbcbrAuthor Commented:
The simplified main task is:
I have to have two fields
1. manually entered value into TextField (s1 = 1.35555;)
2. current rate from incomingQuote (bidPx = incomingQuote.getBidOpen();)
and make calculation like (delta = s1 - bidPx)
0
 
for_yanCommented:
So what is the task: as soon as the user enters one value to populate another?
0
 
gbcbrAuthor Commented:
no, it's two separated questions. I have to have permanently two variables from two different textfield:
1. first I will put manually;
2. second has to come from incomingQuote message.
and after I will decide what to do with them.
0
 
objectsConnect With a Mentor Commented:
> and make calculation like (delta = s1 - bidPx)

delta = Double.parseDouble(s1.getText()) - bidPx;
0
 
for_yanCommented:
The one which comes from your message and if user does not need to
enter or modify it - you can keep it as a Label or as non-editable TextField and use setText() to modifyb its
contents

Another one - addActionListener to it and
as soon as user hits return in actionListener you will be able to read your
input. You still need some indicator that user finished input
0
 
gbcbrAuthor Commented:
>>Another one - addActionListener to it and
as soon as user hits return in actionListener you will be able to read your
input. You still need some indicator that user finished input
I try to follow this way, so I make
if (message instanceof MarketDataSnapshot) {

            incomingQuote = (MarketDataSnapshot)message;
            symbol = incomingQuote.getInstrument().getSymbol();
            bidPx = incomingQuote.getBidOpen();
            askPx = incomingQuote.getAskOpen();
            dcc.setPMDS();


            try {

                if ("EUR/USD".equalsIgnoreCase(symbol.trim())) {
                    bid = bidPx;
                    eurusd_bid = String.valueOf(bid);
                    dcc.jTextField1();

Open in new window

public static PMDS setPMDS() {
        return pmds;
    }

    public DecisionCenterControl() {
        try {
            jbInit();
            this.mFxcmGateway = DBFXConnect.getGateway();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void jbInit() throws Exception {
...............
        jTextField1.setBounds(new Rectangle(505, 40, 95, 40));
        jTextField1.addActionListener(new ActionListener() {
            public void eurUSD_bid(){
                try {
                    jTextField1();
//              jTextField1.setText(eurusd_bid);
            } catch (Exception f) {
                        f.printStackTrace();   
    }
        }
                public void actionPerformed(ActionEvent actionEvent) {
                }
            });
.................
    public void jTextField1() throws Exception {

        jTextField1.setText(eurusd_bid);
    }

Open in new window

and I have NPE at >>  dcc.jTextField1()
0
 
for_yanCommented:
I'm not sure I understand what you are doing.
What means dcc.jTextField1() - ?

What I thought you'd addActionListener at around creation of your textfiled.

jTextField1.addActionListener(this)

and this is your actionPerformed:

pubic void actionPerformed(ActionEvent e){

if(e.getSource().equals(jTextField1)){
String s = jTextField1.getText();
//do something with user inpout

}
}

As far as I can see your code is doing some other things
and it is not quite obvious how you can use jTextField1() - as if it is a method?



0
 
gbcbrAuthor Commented:
Honestly, I'm also don't understand many things which I'm doing. I just try to extrapolate some samples which I have to the similar situations in my app.
You suggested to use ActionListener and I tried to do it as I understood this.
Looks ugly, but this is my trial.
If it's wrong, please advice how to make it in correct way.
0
 
gbcbrAuthor Commented:
this is last edition
public class DecisionCenterControl extends JFrame {
    
  private static final DecisionCenterControl INSTANCE = new DecisionCenterControl();

  public static DecisionCenterControl getInstance() {
      return INSTANCE;
  }
.............

        jTextField1.addActionListener(new ActionListener() {
            public void eurUSD_bid(){
                try {
                    eurUSD_bid_TextField(eurusd_bid);
            } catch (Exception f) {
                        f.printStackTrace();   
    }
        }
                public void actionPerformed(ActionEvent actionEvent) {
                }
            });
.................
    public void eurUSD_bid_TextField(String eurusd_bid) throws Exception {

        jTextField1.setText(eurusd_bid);
    }

Open in new window

if ("EUR/USD".equalsIgnoreCase(symbol.trim())) {
                    bid = bidPx;
                    eurusd_bid = String.valueOf(bid);
                    DecisionCenterControl.getInstance().eurUSD_bid_TextField(eurusd_bid);

Open in new window

0
 
for_yanCommented:
I suggest to do it the way I showed above - this is the simplest and with minimum
confusion caused by anomyomous classes, etc.

In your class declaration you write

public class MyClass implements   ActionListener {
...

then at some point preparing your GUI you create TextField, like that:

JTextField jTextField1  =  new JTextField(30);

then at this point you add ActionListener

jTextField1.addActionListener(this);


then somewhere below in your class you write method
which oulines what you do when ActionEvent happens:

public void actionPerformed(ActionListener ae){

if(ae.getSource().equals(jTextField1)){  <--- you chek that event ciomes from your text fileds
String s = jTextField1.getText();

do something with this string

}

This is the most staright forward way to write event-driven program




}

0
 
gbcbrAuthor Commented:
you mean I have to change by this way
public class DecisionCenterControl extends JFrame {
and make it
public class DecisionCenterControl extends JFrame implements  ActionListener {
0
 
for_yanCommented:
Yes, at least this is the most straightforward way of programming listeners - the simplest
to understand - you implement the listener in your  class
and add required listener methods to that class - requires a little bit more typing
than anonymous classes, but much easier to understand and follow in the code.
At least for ,learning it is much simpler, in future you may
turn to anonymous classses if you want
0
 
gbcbrAuthor Commented:
>>JTextField jTextField1  =  new JTextField(30);
why 30?
0
 
for_yanCommented:
30 - is the number of characters which this textfiled will hold.
If you have long line there - make it bigger.
This is not crucial - it will still alwyas ebe scrollable, but when you create it
make it approximately the size of the text you'll be having there
0
 
gbcbrAuthor Commented:
I need maximum 7 characters 1,35675 or 112,345, so I can make JTextField jTextField1  =  new JTextField(7); ?
0
 
for_yanCommented:
well, make it 10, not a problem to have it a little bit bigger
0
 
gbcbrAuthor Commented:
>> if(ae.getSource().equals(jTextField1)){  <--- you chek that event ciomes from your text fileds
String s = jTextField1.getText();

do something with this string

}

I don't exactly understood this part, especially because in this edition it generates error:
method getSource and equals not found.
And also what I have to put into action area - jTextField1.setText(eurusd_bid); ?
0
 
for_yanCommented:
in the Action you could put some text the into the textbox, but the first thing you should usually
retrieve text, because that is when the action is tiggered - whn someone eneters sometuihing
there - it makes sense to se waht wa entered - that is the purpose of the action - otherwise you need
to understand what is going on.

if  it is "ae" is an action event, then ae.getSource().equals(jTextField1) should work.
Check the code
0
 
gbcbrAuthor Commented:
tomorrow, I'll go home now. Thank you for your advices.
0
 
gbcbrAuthor Commented:
I solve it just  by using setText() method with conversion from Sting to double and vise versa.
0
All Courses

From novice to tech pro — start learning today.