Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

String to label

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
suns0ft
Asked:
suns0ft
  • 4
  • 4
  • 3
  • +3
1 Solution
 
objectsCommented:
Just create a label, and then add that label to your component hierarchy:

JLabel label = new JLabel(mystring);
0
 
wide_awakeCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
glottisCommented:
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
 
suns0ftAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> new SomeText();

You mean: new SomeTest () ;

Cheers,

Mayank.
0
 
glottisCommented:
:) OOpppps
0
 
suns0ftAuthor Commented:
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
 
glottisCommented:
<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
 
wide_awakeCommented:
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
 
CleanupPingCommented:
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
 
suns0ftAuthor Commented:
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
 
objectsCommented:
0
 
suns0ftAuthor Commented:
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
 
objectsCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now