?
Solved

String to label

Posted on 2003-03-19
15
Medium Priority
?
475 Views
Last Modified: 2010-03-31
Hey all. I wrote a small program that generates a random phrase by taking a word from each of 3 string arrays, puts them together and displays them (I was bored ;)). That worked fine, no problems. I decided seeing as I had never done anything GUI in java, minus applets, I would try and make it into a GUI application. Only problem is I cant find a way to either display the string in the GUI windows, or convert the string to a label so I can. Any help appreciated :D
0
Comment
Question by:suns0ft
[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
  • 4
  • 4
  • 3
  • +3
15 Comments
 
LVL 92

Accepted Solution

by:
objects earned 80 total points
ID: 8171905
Just create a label, and then add that label to your component hierarchy:

JLabel label = new JLabel(mystring);
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8171909
Label l = new Label("hello");

or,

Label m = new Label(arr1[rnd.nextInt(arr1.length)] + " " + arr2[rnd.nextInt(arr2.length)] + " " + arr3[rnd.nextInt(arr2.length)]);



0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 2

Expert Comment

by:glottis
ID: 8171913
u mean u are not able to use JLabel ?

public class SomeTest extends JFrame {
 private JLabel label;
 public SomeText() {
  super("Some Test");
  setSize(100, 100);
 
  label = new JLabel("My Label");
  getContentPane().add(label);
 
  show();
 }
 
 public static void main(String[] args) {
  new SomeText();
 }
}

u could go through the demos directory and see all sort of neat things, like the SwingSet
0
 

Author Comment

by:suns0ft
ID: 8171942
Well, I must say that was a rather fast response.. thanks! And I am pretty much a java beginner. Im a first year in an australian uni, and the semester only just started. Anyways, I'll go home, see what works and pick an answer, thanks again!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8171971
>> new SomeText();

You mean: new SomeTest () ;

Cheers,

Mayank.
0
 
LVL 2

Expert Comment

by:glottis
ID: 8172187
:) OOpppps
0
 

Author Comment

by:suns0ft
ID: 8173038
ok, I couldnt get it to work at uni, and being an intelligent guy, I forgot to bring a copy home, whoops... but anyway, Im not sure I understand whats going on, I couldnt get the program to do anything other then give the phrase at the command line, not in the window :(. I tried

Label m = new Label(arr1[rnd.nextInt(arr1.length)] + " " + arr2[rnd.nextInt(arr2.length)] + " " + arr3[rnd.nextInt(arr2.length)]);

but it wouldnt even run, gave me a lot of errors, probably my fault, so I used

JLabel label = new JLabel(mystring);

which worked... well, I could compile it anyway, and I know that label holds my phrase. But I couldnt get the label to update in the window. I will post my code tomorrow if anyone want to see it. But keep in mind I am a beginer at this (we dont actually learn this stuff for another month in lectures), so can anything be explained as basically as possible? Thanks.



0
 
LVL 2

Expert Comment

by:glottis
ID: 8173166
<snip from objects>
http://java.sun.com/docs/books/tutorial/uiswing/index.html 
<snap>

Good place to learn.

you are using AWT for the GUI i wouold recomend using swing. In coding the slight difference you would see is the 'J'. like AWT: Label label = new Label(); and Swing: JLabel label = new JLabel();

You say you were unable to update in window ? what is actually hapening ?
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8174892
sunsoft - with the code

Label m = new Label(arr1[rnd.nextInt(arr1.length)] + " " + arr2[rnd.nextInt(arr2.length)] + " " + arr3[rnd.nextInt(arr2.length)]);

you have to do some setup before it will work.  Basically, arr1, arr2, and arr3 must contain your string arrays from which to choose words.

you also have to import java.util.Random, then do this:

Random rnd = new Random();

To make your label show in a window, include this in your main() method:

JFrame f = new JFrame("Label test");
f.setSize(300,300); // width, height
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(m); // the m you initialized above (either as a Label or JLabel)
f.setVisible(true);


-Mark.
0
 

Expert Comment

by:CleanupPing
ID: 9058588
suns0ft:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 

Author Comment

by:suns0ft
ID: 9059648
Thanks for all the help, I know I have left this far too long, but things were... hectic... around here. Anway turned out I was a bit of a tool with my syntax and although it compiled, it wouldnt run properly, nor did it give me any sort of exception. Odd. Anway, I fixed that and seeing as objects gave the right answer (the first one anyway, thanks to everyone for their help), have some points :D
0
 
LVL 92

Expert Comment

by:objects
ID: 9063003
0
 

Author Comment

by:suns0ft
ID: 9064108
Hey you're australian? What do you think of the Aust. Bach of Computing? Did it actually give you the skills you needed or am I wasting my time? Lol.
0
 
LVL 92

Expert Comment

by:objects
ID: 9066427
I did a BSc at ANU many years ago. The skills I learnt then seemed to have held up over the years, though things are probably vastly different these days.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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