Solved

Another question about TextField

Posted on 2011-02-23
26
258 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
  • 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
 
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 500 total points
ID: 34965724
just use the setText() method
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 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
 
LVL 92

Expert Comment

by:objects
ID: 34965735
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 87
HashMap Vs TreeMap 12 49
java  and programming certification ? 4 65
Unable to start eclipse ? 17 87
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now