• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with intellij gui (or just how to use gui) and naming of "objects"  RSS feed

 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so im trying to make a java application (a dungeons and dragons character creator and background generator) and im still fairly new to java.

I can get the logic part to work and can deal with strings, ints , arrays etc.
I understand functions and how to reference strings etc from other classes
i know how to make a new object and give it a hardcoded name


but then somethings just make zero sense, and im not even sure where to begin so ill jump right in

i had an issue manipulating a gui and posted to ask for help (http://www.coderanch.com/t/634228/GUI/java/Trouble-changing-text-jlabel-class#2905574) i was basicly told to go back and learn the basics of Object orientated language so thats what i did, i understand now we need to make an object then manipulate it and the class file serves more as a template, armed with that knowledge i restarted my project afresh

i have a main class that starts everything off,

i have a class that creates the character (and since it is an object i should be able to make multiple characters)

the character is at the moment made by this function



now that means the character object i have created is called "mainCharacter" this is ok for now but i wanted to give it the name in the String nameTest

the idea was going to be that for each character i created its name would be added to an arraylist so i could reference each one for use with the background creator part, rather then have have the names of the objects hard coded like that, this seems impossible (nameTest is already defined is the error...well yeah ofcourse it is but i wanted to use the contents of nameTest not nameTest itself...grrrr)

so thats problem 1 naming objects dynamically seems impossible.


problem 2 and the most important one (i can get around problem 1 im sure just by making more hardcoded names then i need and setting each one up as needed)

i create the gui to display the character in intellij idea and it looks wonderful, should do as even though its only a quarter of the finished gui it took 2 hours of setting up labels, text boxes and panels to get right!)
again its hardcoded not dynamicly named.

the function that sets it up from within character is


so its object name is mainchar
it displays perfectly fine but the textbox "charNameField" is blank
AND the program pauses when the gui is displayed not continuing untill the gui is closed (System.out.println("test 2"); never gets executed even after the gui is closed)

so the questions on this:
1) is the fact that the gui object is called mainchar and called from the character class which is named in this case mainCharacter meaning that if i was also make a character object called mainCharacter2, its mainchar would be different to mainCharacter's mainchar ? or would they be fighting it out with each other for the same name?
2) why is it pausing at the gui?
3) how do i update the text in text boxes from the program?(yes text boxes are also designed to be written into manually but in this case i want them to be done from the program and the player can edit that later if they choose)


please dont just say learn more java, i realise your not too keen on helping people on here unless they have tried for them selfs first but please believe me i have struggled with this for days before risking coming back here and asking for help, i just dont get why its not working...im stuck

i have attached the source code...yes parts of it are messy but im sure seeing the code is bound to help more then just the snippets i have posted.(ok i cant attach zip files..)
uploaded it to drop box https://www.dropbox.com/s/xgqf5ene2p2th2n/src.zip

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Eastwick wrote:

now that means the character object i have created is called "mainCharacter" this is ok for now but i wanted to give it the name in the String nameTest

the idea was going to be that for each character i created its name would be added to an arraylist so i could reference each one for use with the background creator part, rather then have have the names of the objects hard coded like that, this seems impossible (nameTest is already defined is the error...well yeah ofcourse it is but i wanted to use the contents of nameTest not nameTest itself...grrrr)

so thats problem 1 naming objects dynamically seems impossible.

Correct, that is impossible.

But, you can store your characters in a Map<String, Character> so that you can easily find them by name. For example:

See this section of the Java Tutorials for more: http://docs.oracle.com/javase/tutorial/collections/

I am not sure if this is the best route to go, but it is, at least, a route.

problem 2 and the most important one (i can get around problem 1 im sure just by making more hardcoded names then i need and setting each one up as needed)
Please don't do that! ;) It will make the application harder to work with late on!

...so the questions on this:
1) is the fact that the gui object is called mainchar and called from the character class which is named in this case mainCharacter meaning that if i was also make a character object called mainCharacter2, its mainchar would be different to mainCharacter's mainchar ? or would they be fighting it out with each other for the same name?

A couple of bits for this:
1) The variable mainchar is local to the method, so there would be no conflict between different objects - the scope is much to small for that to be a problem (which is good - you want scope to be as small as possible).

2) you should do your best to separate the GUI code from the character code. Develop them at different times. Character is a data holder class, it should not have any GUI code in it. I would make the Character class as a stand alone class and write up some command-line based tests to make sure it is working. Example, create a simple applications with main methods that ask the user to input the names/levels on the command line. Use that information to create a Character, then print out the interesting bits. Then make another test that can handle 2, or 3 characters... Then, once you are happy with how the Character class is used, develop a new class for the GUI that is completely separate from the Character class, which is given a Character object and can use it to read values from and store values into.

3) You need to work on your naming. Names like mainCharacter and mainchar are easy to confuse. What is mainchar? A window to display a character? Why not call it characterDisplayWindow? The better you name the variables the easier it is to read code and the easier it is to sort out problems (especially when you come back to the code after a few days or weeks).

2) why is it pausing at the gui?

Impossible to say. You need to provide more code for us to understand that. My guess is that you start() method has some sort of loop in it that never exits, which is why the SOP never happens (SOP == System.out.println).

3) how do i update the text in text boxes from the program?(yes text boxes are also designed to be written into manually but in this case i want them to be done from the program and the player can edit that later if they choose)

I don't know, impossible to say from the code you provided. We would have to know something about the CharWindow. But you haven't given us any information.

please dont just say learn more java, i realise your not too keen on helping people on here unless they have tried for them selfs first but please believe me i have struggled with this for days before risking coming back here and asking for help, i just dont get why its not working...im stuck

Sometimes being instructed to go back and learn more is the right advice. This is probably one of them. You are still learning, but you may be jumping ahead of your own learning curve, trying to do more than you can actually achieve. My advice is to slow down and try smaller things eventually building up to this application. Trying to jump right into a project like this, though interesting, is hard as it has many different layers of complexity (GUI, data storage, threading...) and you haven't yet grasped the basics (variables, scope, naming conventions). Slow down, you will get to the answer faster that way.

One of the ways to start learning at a good pace is to get a book and work your way through it, going through and doing each of the exercises in order. That will give you a reference to work from, proof you are making progress, and typically the book will only provide exercises whose difficulty is commensurate with the information presented in that chapter - not likely to provide one that is way to hard to get into right away.

i have attached the source code...yes parts of it are messy but im sure seeing the code is bound to help more then just the snippets i have posted.(ok i cant attach zip files..)
uploaded it to drop box https://www.dropbox.com/s/xgqf5ene2p2th2n/src.zip
This may sound rude, but I, for one, am not likely to download a random zip file from someone I don't know. Plus, going to another site to download a file which is bound to be a lot of code to sift through is a lot of work. Too much for me to try to do. The best way to get help is to post the code here, but only enough to show the problem.
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would of attached the zip to the site but for some reason they dont allow zips, never occured there would be an issue with it but i understand reluctance

Main.class


Character.class


CharWindow.class


CharWindow.form


that should be the main ones that im having issue with

will have a read of the collections stuff in a bit.

mainchar and mainCharacter were place holder names while i tested stuff not the finished ones, so it was more off the top of my head

I have done extensive tests as i go to make sure the logic is working, it already spits out the very core of a character, name race class gender and core stats, using SOP. i wanted to get my head around how to display that on a gui before i did too much more of the logic as its been bugging me for weeks that i cant get it to do that, im not sure if that makes sense,its just how my brain works.

i can understand the idea of only doing simple programs to learn but i cant focus like that, to learn it has to be something that interestes me, i have however followed a few of the tutorials on the net along and even some of the minecraft mod making guides that while not being fully java stand alone however did help me learn some concepts

i cant see anything in the start() that would cause a loop but then intelij filled that bit in for me (though it wanted to make it a main method...and well i already have one of them)
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a lot of code. Like I said, it takes a lot of effort to go through all that code. I asked you to post the relevant code, not all your code. As it is, it is too long to walk through.

Also, when you post code, please avoid writing long lines, as they make the thread really hard to read. I edited your post to remove the long lines. I was going to fix them but there were too many. I don't know if what I did removed key parts of your code or not. Please review the changes. I would suggest editing your post, removing all non-essential code, and putting in all essential code to describe the problem. And please make sure the code you post doesn't have any lines longer than 80 characters. This sounds like a lot of heavy-handedness ("do this, do that, blah blah blah") and I am sorry for that. It isn't meant to be heavy handed. It is just that it is hard to help you with a wall of code and text you have to scroll horizontally through to read. This is meant to help you get answers not enforce 'rules.'
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:That is a lot of code. Like I said, it takes a lot of effort to go through all that code. I asked you to post the relevant code, not all your code. As it is, it is too long to walk through.

Also, when you post code, please avoid writing long lines, as they make the thread really hard to read. I edited your post to remove the long lines. I was going to fix them but there were too many. I don't know if what I did removed key parts of your code or not. Please review the changes. I would suggest editing your post, removing all non-essential code, and putting in all essential code to describe the problem. And please make sure the code you post doesn't have any lines longer than 80 characters. This sounds like a lot of heavy-handedness ("do this, do that, blah blah blah") and I am sorry for that. It isn't meant to be heavy handed. It is just that it is hard to help you with a wall of code and text you have to scroll horizontally through to read. This is meant to help you get answers not enforce 'rules.'


It seems i cant win, from not enough code to too much lol

I only posted the complete versions as i was told the snippets were not enough and somewhat unwilling to edit them down as im not sure what parts are relevent (i though i had done that in the first post after all) to the issue im having i certainly dont expect people to read all of them, just if they think the issue may be because of such and such they can see where the snippet comes from.

im starting to think maybe java just isnt a good language for some one my age, some one brought up with BASIC on the old spectrum and easy amos on the amiga. perhaps i would be better just making it command line and giving up on the gui. I have rad tons of sites, watched a lot of videos but they all show the same thing, heres some text (usually "hello world") heres a button, heres how to change it all, with all the code in the same class (which is wierd as most agree gui code and main code shouldnt be in the same class) im wondering if its my age thats just stopping me from grasping it, i wasnt taught this in school like todays generation are, i was taught to move a "turtle" around the floor lol

but lets simplify where im stuck, incase it is something someone can help with:

i just need to be able to make the fields in the gui change by sending the values to it from another class, this is essentialy what it boils down to ..oh and to stop it pausing the rest of the program while up
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Eastwick wrote:


I see why the name doesn't get displayed. In your Character#guiUpdate() method you create a new CharWindow(). You set the value of the text field, then you call start(). Start creates a new CharWindow and it is this new CharWindow which gets displayed. The one you created before is never put into a Frame and never made visible. You need to make sure you are working with just one window here.

i cant see anything in the start() that would cause a loop but then intelij filled that bit in for me (though it wanted to make it a main method...and well i already have one of them)

I see the beginning of one, though I may have hid the part that closes the loop in the //lots of gui code with long line... comment.

In your Character#guiUpdate() method you create a new CharWindow(). Doing that calls the instance initializer (lines 13-18 of CharWindow as displayed in this response) which calls the $$setup$$() method. That setup method is where the gui components are built. Then later on you call setup() which also creates a new CharWindow, triggers the instance initializer, and calls the $$setup$$ method. If that $$setup$$ method ever makes a call to either CharWindow.start(), or Character#character(), or Character#coreStats() or Character#guiUpdate() or CharWindow#refresh()[/tt] then there is some chance of an infinite loop. This is particularly likely if there is some event on the charNameField which might cause one of these other methods to be called.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 95 - 228 of your character class is not object oriented code. Your Character class should probably be abstract and then you have a separate concrete class for each of the character types (professions) that extends the Character class.
A lot of the code there seems to be the same for all the characters so that code should be in the Character class and any code unique to a particular character type should be in the subclass.
Then instead of passing the profession into the character method (that should really be a constructor) you use it to decide what sort of character to create.
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
I see why the name doesn't get displayed. In your Character#guiUpdate() method you create a new CharWindow(). You set the value of the text field, then you call start(). Start creates a new CharWindow and it is this new CharWindow which gets displayed. The one you created before is never put into a Frame and never made visible. You need to make sure you are working with just one window here.

i cant see anything in the start() that would cause a loop but then intelij filled that bit in for me (though it wanted to make it a main method...and well i already have one of them)

I see the beginning of one, though I may have hid the part that closes the loop in the //lots of gui code with long line... comment.

In your Character#guiUpdate() method you create a new CharWindow(). Doing that calls the instance initializer (lines 13-18 of CharWindow as displayed in this response) which calls the $$setup$$() method. That setup method is where the gui components are built. Then later on you call setup() which also creates a new CharWindow, triggers the instance initializer, and calls the $$setup$$ method. If that $$setup$$ method ever makes a call to either CharWindow.start(), or Character#character(), or Character#coreStats() or Character#guiUpdate() or CharWindow#refresh()[/tt] then there is some chance of an infinite loop. This is particularly likely if there is some event on the charNameField which might cause one of these other methods to be called.

ah i see, i assumed i was making a new object and that object was then making the window....hmmm back to the drawing board on that one then but atleast i know why its failing (thank you!)

when start() was made it was done auto by intellij which set it as public static void main(String[] args), since i already had a main function i renamed it to start, so it "shouldnt" be referenced anywhere, for a language everyone seems to assure is the easiest, its damn hard to understand it. i think ill drop the gui for now its a shame to but i just physicly cant get my head around it. thank you for the help though, i t is appreciated.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Eastwick wrote:It seems i cant win, from not enough code to too much lol
Haha, keep at it. It takes practice and patience, and a discriminating eye. Look over your code, think about the problem, think about the logical flow, and think about where a problem could possibly be. The problem wasn't likely to be in the Main class, so no need for that. The probably was related to GUI, so no need for the non-gui related stuff in the Character class, etc... I find when I start a post and follow the code to try to locate the relevant (and only relevant) code I need to post, it helps me to actually find the problem. When I am stuck, I often start writing a forum post as a means to solve the problem, and then don't need to actually post it - it just helps me direct my mind to where the problem might be and I end up finding the problem that way.

And don't give up on Java (unless you hate it). It takes a bit to learn. There are layers of concepts which makes it different than what you have done. That is why it is important to take it slow. You might have a hard time focusing on small tasks, but you have to or everything will just be a big jumble of confusion. Which is where I think you are at now.
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Lines 95 - 228 of your character class is not object oriented code. Your Character class should probably be abstract and then you have a separate concrete class for each of the character types (professions) that extends the Character class.
A lot of the code there seems to be the same for all the characters so that code should be in the Character class and any code unique to a particular character type should be in the subclass.
Then instead of passing the profession into the character method (that should really be a constructor) you use it to decide what sort of character to create.


that code takes the stat rolls above it and puts them in order of the profesions requirement, so it looks very similier but the first attribute listed in each class will be different (as the higher number needs to go there) later i will need to make checks to ensure that the number above the min required for that profession.

I dont know how to do abstract classes, i have heard in passing of them but none of the guides or vids i have watched shows what that is, though i will google it now and see what i can learn.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Eastwick wrote:>.. it was done auto by intellij which set it as public static void main(String[] args), since i already had a main function i renamed it to start,

This is a 'convenience' method created by the IDE, and would let you run the GUI class as a stand alone application. It can be ignored (deleted) if you don't need it. You shouldn't just re-purpose it though, unless you know what it is doing. That said, IDEs are nice, but they are yet more things to learn (what code that it creates is needed? What code not? What is that initializer, what does the $$setup$$ do, where does it come from, etc...) In the long run, they make coding easier, but until you understand Java the language, I would also suggest to do everything from the command line for a while so you know all the bits and bobs of your application (since you wrote them). But that is just my suggestion, no need to follow it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Eastwick wrote:that code takes the stat rolls above it and puts them in order of the profesions requirement, so it looks very similier but the first attribute listed in each class will be different (as the higher number needs to go there) later i will need to make checks to ensure that the number above the min required for that profession.

I dont know how to do abstract classes, i have heard in passing of them but none of the guides or vids i have watched shows what that is, though i will google it now and see what i can learn.

Don't worry about the abstract bit for the moment. The more important part is the inheritance - having a parent class that contains common code and then subclasses that contain code or values specific to the subtypes.
As has been suggested earlier you really need to go through a tutorial that introduces object oriented programming step by step. At the moment you are writing a procedural program with some objects added to it.
If you carry on this way it is just going to get more and more frustrating - the change from procedural to object oriented programming is not a simple one and will take time, but one day it will just click and you'll wonder what all the fuss was about.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another little thing. There already is a class called Character; I suggest you choose a different name for it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!