LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2008, 01:30 AM   #1
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
GUI random text label


Hello every one. I am trying to create a java application with gui and I faced some problems. What I am trying to do is to add a random generated number into a text label. I am using netbeans IDE. So on the Pane I add a text label and edit its text to: "The number is: " at this point I want a random generated number to be added after :. I know how to create a random numbers generator but my problem actually is the text label. How can I add a variable after : on a text label? I tried to do it by editing the automatic generated code of neatbeans but is not editable. Any suggestions?

Thanks in advance.
 
Old 02-28-2008, 02:11 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Is the label a String? If so, use the valueOf static function with the number you generated and add it to the string using + and assign it back to the label with =:
Code:
String label = "label: ";
label = label + String.valueOf(1.0);
ta0kira

Edit: Sorry, I forgot you can't edit the code. There has to be some way to edit it, though, otherwise it would be a useless IDE.

Last edited by ta0kira; 02-28-2008 at 02:14 PM.
 
Old 02-29-2008, 02:16 AM   #3
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
I can't edit the generated code for the GUI.. I can add actions and things like that. But I can't edit a particular generated part of the code concerning the GUI. On way of do it is to add a button and when the user push the button then:
Code:
label.setText("The number is: " +num);
But what I am actually trying to do is a Math game with interface. So a question of the format:

If you get 25% discount for a t-shirt which it cost $60 how much will you pay?
Code:
label.setText("If you get " +num1 "% discount for a t-shirt which it cost $" +num2 " how much will you pay?);
Note: num1 and num2 are random generated numbers. So I need and I know how to generate 2 numbers. Add them on the label each time is executed. Calculate the result and finally check the user input if it's right to give him points.

The problem is that I can edit directly the text of the label and I don't want a user to click on a button to see the text because this is horrible :P. I want the text to be generated with the random numbers each time the Pane is executed.

Code:
 _________________________________________________________________________________
|                                                                                 |
| If you get 25% discount for a t-shirt which it cost $60 how much will you pay?  |
|          ______             _____________                                       |
| Answer: |______|           |Next Question|                                      |
|                             -------------                                       |
|_________________________________________________________________________________|
Something like that
 
Old 02-29-2008, 06:32 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you can add a button to create an action then you must be able to modify some sort of canvas- or label- update function to make that change for you. You can't access the actual label object from the function that generates the number? Maybe you need to make the number accessible from the label, and modify the label's or the container's update function so that it pulls the value and puts it in the label.
ta0kira

PS In what context do you generate the number?

Last edited by ta0kira; 02-29-2008 at 06:35 PM.
 
Old 03-01-2008, 12:33 AM   #5
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Lightbulb

Finaly I have done it!! To anyone else interested in:

Code:
private void formWindowOpened(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:
        jLabel1.setText("the number is: " + String.ValueOf(num1));
}
What I have actually done is to add an even when the window opens.

Thanks for the help ta0kira anw

Stupid GUI's and IDE's ;p

Last edited by Cyhaxor; 03-01-2008 at 12:36 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
A good simple text display widget in GTK (not Label) vharishankar Programming 3 05-18-2006 05:44 AM
C++ text stream random access Orkie Programming 6 10-26-2005 02:48 PM
very strange swap partition label and empty text console whencat Fedora 4 08-07-2005 09:16 AM
button-widget's label text is cut in half xideum Linux - Software 0 10-02-2004 02:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration