AllanOcelot Posted March 28, 2011 Share Posted March 28, 2011 hey guys, whats up? come in come in, please do. JEEZ!, take off those shoes, god knows where you have been.:confused: Well lets get started. Do we have any Java programmers here? Im making a roleplaying app for oblivion, its going to create a completely random and unique backstory for the `Serious Roleplayer'. Im doing this of course using strings, mashing them together. The best way aha. Im having an issue, and I was hoping someone could help. Ive got my string set up, lets say its name. Ive got it to generate random names. Good, great. But when the user clicks the button, it writes the result to a JLabel, we will call it `Scroll.,' Now, heres my BIG issue, If I can get this figured out, I can release the application. How, in gods name, do I get my button to create a random result every time it is clicked? If someone can anser this and help me get it working, im offering a £5 reward. Its not much, but it could be a lot more depending on how happy I am feeling aha. So come on in, or if you know a friend who knows some java, link em to this topic., and let them earn some money.:laugh: Link to comment Share on other sites More sharing options...
evilneko Posted March 29, 2011 Share Posted March 29, 2011 Java. Ugh. Anyway, you didn't do something silly like code everything right inside your listener, right? I'd really have to see the code... Link to comment Share on other sites More sharing options...
AllanOcelot Posted March 29, 2011 Author Share Posted March 29, 2011 No no, im not THAT bad. But I am new. I shall glady give you payment if you could help me with this little problem. :) here is a stripped down version of the `name masher together randomiser,' as I call it. public static String[] name1 = {"a","b","c","d","e","f","g","h","i","j","k","l","n","o","p","q","r","s", "t","u","v","w","x","y","z"}; public static String[] name2 = {"oh noes","its not working","sad face"}; public static int name1length = name1.length; public static int name2length = name2.length; public static int rand1 = (int)(Math.random()*name1length); public static int rand2 = (int)(Math.random()*name2length); public static String phrase = name1[rand1] + " " + name2[rand2]; public void buttonNameActionPerformed(java.awt.event.ActionEvent evt) { scroll.setText(phrase); In my mind I want to put all of those methods that make up the name generation into like...hmm a small box? if you understand. and have a `small box,' for every random trait - e.g eye colour, home town, age ect. I hope you get what I mean. I think I explained it well, but its hard to put it into terms. I think the correct term is encapsulation? that word has been thrown at me a lot.:thumbsup: Link to comment Share on other sites More sharing options...
TheTerminator2004 Posted March 29, 2011 Share Posted March 29, 2011 (edited) I'm going to assume this is running on the Event Dispatch thread (if you're not using multiple threads, then you don't need to worry about this), if it's not, you'll need to use SwingUtilities.InvokeAndWait() to update the button. If you haven't messed around with threads, that shouldn't be a problem. AWT and Swing are probably the two worst GUI libraries I've ever had the misfortune of having to use in any language ever, so there could be any number of silly reasons why it's not working. You could try and force the button to redraw by calling repaint(), validate(), or pack() on the container it's contained in (presumably a JPanel or JFrame or something) right after you update the string, and see if any of those works. In my mind I want to put all of those methods that make up the name generation into like...hmm a small box? if you understand.and have a `small box,' for every random trait - e.g eye colour, home town, age ect.I hope you get what I mean. I think I explained it well, but its hard to put it into terms.I think the correct term is encapsulation? that word has been thrown at me a lot.:thumbsup: What do you mean by a 'small box'? You can create a separate class with all your random generation methods in, if that's what you mean. Could even make it static, if you don't plan to use it for anything else. Something like this: static class RandomGen { public static String[] name1 = { "a", "b", "etc" }; //more name variables etc public static String GenName() { String name = new String(); name = //name generation code here return name; } public static String GenHomeTown() { //etc } } Encapsulation is when you make a variable within a class private, and then use a method to access it - meaning you can change the variable etc within the class, without having to worry too much about other classes accessing it. eg: private String something; public String getSomething() { return something; } public void setSomething(String str) { something = str; } Edited March 29, 2011 by The_Terminator Link to comment Share on other sites More sharing options...
evilneko Posted March 30, 2011 Share Posted March 30, 2011 This public void buttonNameActionPerformed(java.awt.event.ActionEvent evt) { Needs to call a function that does all of the other things. Right now it's just setting the text in the label. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now