Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Another question about TextField

Posted on 2011-02-23
26
Medium Priority
?
295 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:gbcbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
  • 5
26 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34965543
You can make keylistener and listen to every character as it is entered
0
 
LVL 92

Expert Comment

by:objects
ID: 34965554
You can use the getText() method to get the value of a text field at any time
0
 
LVL 92

Expert Comment

by:objects
ID: 34965566
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 47

Expert Comment

by:for_yan
ID: 34965580
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
 

Author Comment

by:gbcbr
ID: 34965638
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34965686
So what is the task: as soon as the user enters one value to populate another?
0
 

Author Comment

by:gbcbr
ID: 34965711
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 34965724
just use the setText() method
0
 
LVL 92

Assisted Solution

by:objects
objects earned 2000 total points
ID: 34965732
> and make calculation like (delta = s1 - bidPx)

delta = Double.parseDouble(s1.getText()) - bidPx;
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34965734
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
 

Author Comment

by:gbcbr
ID: 34970235
>>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
 
LVL 47

Expert Comment

by:for_yan
ID: 34972162
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
 

Author Comment

by:gbcbr
ID: 34972285
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
 

Author Comment

by:gbcbr
ID: 34972319
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34972427
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
 

Author Comment

by:gbcbr
ID: 34972654
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34972701
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
 

Author Comment

by:gbcbr
ID: 34972733
>>JTextField jTextField1  =  new JTextField(30);
why 30?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34972889
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
 

Author Comment

by:gbcbr
ID: 34973016
I need maximum 7 characters 1,35675 or 112,345, so I can make JTextField jTextField1  =  new JTextField(7); ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34973146
well, make it 10, not a problem to have it a little bit bigger
0
 

Author Comment

by:gbcbr
ID: 34973240
>> 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
 
LVL 47

Expert Comment

by:for_yan
ID: 34973388
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
 

Author Comment

by:gbcbr
ID: 34973449
tomorrow, I'll go home now. Thank you for your advices.
0
 

Author Closing Comment

by:gbcbr
ID: 34992617
I solve it just  by using setText() method with conversion from Sting to double and vise versa.
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question