Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] .setText() Won't show variable?

Started by The_Game Sep 16, 2010 at 3:05 PM 6 replies 4.1k views
Original Post
The_Game
The_Game
Like the subject above says, I'm trying to get it so that I get a number to show up in the text box I've made but it isn't working.

I've already got it to convert a double into a string, and if I put any sort of text into the .setText() it will show up when I want it too but it won't do that for variables. Is there something I'm missing?
rip-off
rip-off
Show us the code. Are you sure there is some interesting text in your variable?
The_Game
The_Game

//The variables I'm using

double dTotalCost = 5.45;
String sTotalCost;
JButton jCompute;
JTextField tTotalCost = new JTextField(10);


//Converting dTotalCost to sTotalCost so I can output it in the textbox
public class ConvertDoubleToString
{
String sTotalCost = Double.toString(dTotalCost);
}

//Says What to do when button pressed
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Compute"))
tTotalCost.setText(sTotalCost);
}


This is just what I think the relevant code is, the whole thing is pretty big so I figured you wouldn't want to all of it.
rip-off
rip-off
It depends on your overall structure, but that seems a little complex. In particular, you have aliasing between the outer classes and the inner class "ConvertDoubleToString": they both have a field called "sTotalCost". While the compiler will figure out the correct one to use based on context, I suspect you think it is using one where it is actually using the other. The inner class doesn't do what you suspect.

It is best to generate such data on the fly. This way you don't have to worry about the String and double getting out of sync.
//The variables I'm usingdouble totalCost = 5.45;JButton computeButton;JTextField costText = new JTextField(10);public void actionPerformed(ActionEvent e) {    if (e.getActionCommand().equals("Compute"))    {        String text = Double.toString(totalCost);        costText.setText(text);    }}

Also you should be aware that hungarian notation is not considered normal or necessary in Java (or anywhere really nowadays). Note that the names I've given above hint at the types (where appropriate), but don't encode the types - its more about stating the purpose of the variable.

As a simple example, imagine you change from using double to "int" for the cost (expressed in cents or pennies or whatever). This is a realistic example of a change you might have to make, doubles are not perfectly accurate and you will suffer from rounding errors. Given that you stay within their range, integers are perfectly accurate (BigNums can be used when the range doesn't suffice).

To change the above code to use integers for the cost we might do this:
//The variables I'm usingint totalCost = 545;JButton computeButton;JTextField costText = new JTextField(10);public void actionPerformed(ActionEvent e) {    if (e.getActionCommand().equals("Compute"))    {        int cent = totalCost % 100;        int currency = totalCost / 100;        // not locale safe, but this is a simple example...        costText.setText(currency + "." + cent);     }}

Note how we didn't need to change the variable's name just because its type was changed.

Remember that writing in an Object Oriented language like Java you will more often than not be dealing with class types rather than primitives, and as you demonstrate yourself it can be difficult to meaningfully encode class names while keeping the code readable.

Plus modern IDEs will assist you, just by hovering over a variable it will tell you its type anyway.
The_Game
The_Game
Okay, it's showing up now, but I have 3 questions ask to make sure I understand.

By creating the ConvertDoubleToString class, it was interfering with the textBox getting the right variable, correct?

It was giving sTotalCost a new value of null instead of putting in my double (dTotalCost), right? I just want to make sure I understand why it's wrong so I don't do it again.

Lastly, how do you make those little boxes full of code in your post?
rip-off
rip-off
Quote:

By creating the ConvertDoubleToString class, it was interfering with the textBox getting the right variable, correct?

It was giving sTotalCost a new value of null instead of putting in my double (dTotalCost), right? I just want to make sure I understand why it's wrong so I don't do it again.

The ConvertDoubleToString class created a new type. The type contained a single String member, which would be initialised on instantiation with the value of "dTotalCost" of the outer instance.

It probably didn't do anything because it was probably never instantiated. If it had been instantiated, it would not have assigned anything to the value of "sTotalCost" in the outer class. Even this would have been a one-off event, not sufficient to keep the text in the inner class in sync with changes in the value of the double in the outer.

Inner classes are just that - inner classes. They have some special rules with regards to access control (public, protected, private) and that non-static inner classes can reference their owning instance variables implicitly, but other than that they are just classes. You could have deleted the inner class and your original program would have behaved identically.
The_Game
The_Game
I figure that I could just ask another question on this thread instead of making a new one, taking up more space.

As I said earlier, I want dTotalCost to show up in a TextBox, which it does, but I also want any result I get to end after only 2 decimal points. Any idea on how to do this?

I really hate pesting people on these forums about my programming, but I don't know of any better resource.
rip-off
rip-off
Quote:

I figure that I could just ask another question on this thread instead of making a new one, taking up more space.

I wouldn't worry about that. Certainly, you shouldn't be re-using threads to ask unrelated questions. But in this case the follow-up question is fairly related.

Quote:

As I said earlier, I want dTotalCost to show up in a TextBox, which it does, but I also want any result I get to end after only 2 decimal points. Any idea on how to do this?

There are a couple of ways. You could use the technique I mentioned above, which stores the value in integers. You could use the DecimalFormat class. You could use String.format(), which was demonstrated at the end of your previous thread.
Quote:

I really hate pesting people on these forums about my programming, but I don't know of any better resource.

Google is your friend, and your first port of call. A google search for java 2 decimal points gives lots of results - it actually shows a gamedev.net thread as the very first one for me, but that might be Google spying on me...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.