5 Replies - 17625 Views - Last Post: 14 September 2007 - 11:36 AM Rate Topic: -----

#1 monkyofdoom  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 23-August 07

How to print a variable in a JLabel

Post icon  Posted 10 September 2007 - 10:05 AM

I was wondering what the best way to make a JLabel display a variable.
I want to make it so it will say "You entered a :" then have the user input after that.
Is This A Good Question/Topic? 0
  • +

Replies To: How to print a variable in a JLabel

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: How to print a variable in a JLabel

Posted 10 September 2007 - 10:29 AM

You can use the setText() method of the JLabel to set what it reads. Just put the text and variable in there and make sure you toString the variable if need be.

JLabel mylabel = new JLabel();
mylabel.setText("You entered a: " + variablevalue);



It is simple as that. :)
Was This Post Helpful? 0
  • +
  • -

#3 monkyofdoom  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 23-August 07

Re: How to print a variable in a JLabel

Posted 10 September 2007 - 10:32 AM

View PostMartyr2, on 10 Sep, 2007 - 10:29 AM, said:

You can use the setText() method of the JLabel to set what it reads. Just put the text and variable in there and make sure you toString the variable if need be.

JLabel mylabel = new JLabel();
mylabel.setText("You entered a: " + variablevalue);



It is simple as that. :)

Alright, that will totally work but I have one problem. In netbeans is there anyway to edit generated code? For some reason it won't let me.
Was This Post Helpful? 1

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: How to print a variable in a JLabel

Posted 10 September 2007 - 10:55 AM

Thank NetBeans that it won't let you edit the generated code (just like any sane IDEs). If you could your changes would disappear or the IDE support for the GUI editing would go tits up.
You have to create an EventHandler (I think NB calles it that way) - probably for the input field that fills the variable you are about to display - and write the code there.

Hm, looks like it can be done something like this:
click on the JTextField or whatever you use to get the input.
On the properties window go to the events tab.
Press the ... button besides keypressed or some event that you find relevant.
Choose add event handler in the appearing dialog.
There ya go, give it a name, and the GUI will let you edit the event handler's body. (Theoretically)
Was This Post Helpful? 0
  • +
  • -

#5 monkyofdoom  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 23-August 07

Re: How to print a variable in a JLabel

Posted 14 September 2007 - 11:16 AM

View Post1lacca, on 10 Sep, 2007 - 10:55 AM, said:

Thank NetBeans that it won't let you edit the generated code (just like any sane IDEs). If you could your changes would disappear or the IDE support for the GUI editing would go tits up.
You have to create an EventHandler (I think NB calles it that way) - probably for the input field that fills the variable you are about to display - and write the code there.

Hm, looks like it can be done something like this:
click on the JTextField or whatever you use to get the input.
On the properties window go to the events tab.
Press the ... button besides keypressed or some event that you find relevant.
Choose add event handler in the appearing dialog.
There ya go, give it a name, and the GUI will let you edit the event handler's body. (Theoretically)

Finally got around to trying this and it works perfect. Thanks a ton! :D
Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: How to print a variable in a JLabel

Posted 14 September 2007 - 11:36 AM

You are welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1