Dale Viljoen

+ Follow
since May 21, 2020
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dale Viljoen

Hi everyone at coderanch!

My question is poorly worded and I apologies for that. I was not to sure how to ask this question.

I have been learning java for almost a year now and can do everything I want to at this point. However I am stuck on a few things in terms of how to correctly do them.

One of my classes named teacher has a method as below:


There is currently no need for the other methods as my question is about the way I code. I create two suspects and present them to the player.

Whilst doing my unit testing I noticed that testing the chooseWhoDidIt() method to test if the suspects information were correctly pulled from the database just felt wrong. I feel like I should have a separate method that deals with the creation of the suspects.

That brings me to my second question. If I do make a second method that creates suspects, would it make sense to even have that method in the Teacher class?

I am more then happy to recieve any other input to help me better my practises. That is what I would like to focus on for the new few weeks/months.

Thank you all!
2 weeks ago
Wonderful, I will remember that in the future in terms of long comments! Thank you for all of the help, bettering my coding practices Lastly, why does one have to put "evt ->" when adding action listener?
1 month ago
Junila, thank you for all of that. I finally understand this whole GUI and the order of execution and the two methods you have given me are brilliant and I definitely will be using one of those. You all have given me many ideas now to work with my program and I will definitely changes things up a lot now. I have no clue when I would of figured this stuff out had I not asked here!
1 month ago

Paul Clapham wrote:

Dale Viljoen wrote:But how do I get the method to "wait" for the user to trigger the action listener and return the value back to the introduction() method? Or is that not possible?

It's not possible, and it's not meaningful to ask that anyway. In a Swing application, after you construct the GUI and call its setVisible(true) method, Swing is in charge. It waits for the user to do things to the GUI, like typing in text fields and clicking on buttons. When the user does certain things, Swing calls your action listeners and other similar methods. The purpose of your code is simply to react to those events.

Would my idea that I listed work out fine or is not impossible? So if this is all not possible, then how exactly do programmers deal with this situation. How do I have my teacher class ask the user a question because as you said it is impossible? I just want to know how it would I can do it as I have already finished my program and just currently developing a GUI for it (we had to develop the CUI version first for the assignment).
1 month ago
Thank you for the help Campbell and the others , you have really helped me understand how I should be setting up GUI classes as we were not taught this way. I also personally find this method a lot easier and makes a lot more sense. I will go ahead and ask publicly so the rest of the students can see in case others are stuck with the same idea. He did mention it in a lecture so a few individuals might of missed it.

the submitAnswer() method you have created is really great and passing the label through as a parameter for the method allows me to access the JTextField value from another object (in my case, Teacher object) so thank you for that.

What I am needing, is once the user hits enter button (after writing their answer) then the action listener for that button essentially gives the answer back to the Teacher class and this is where I am stuck on.

So (just for example) this is the method inside Teacher and is called by the GUI class:

Firstly, do not worry about the System.out.println as I will later on figure on how to redirect output to the JTextArea instead. But how do I get the method to "wait" for the user to trigger the action listener and return the value back to the introduction() method? Or is that not possible?

The only thing I can think of, is to have two methods. One that introduces the students and when it asks for the name the method finishes. Once the student writes their name into the JTextField and triggers the action listener then have the action listener call a method (that is in Teacher object) called SetTypeHere(String typeHere) where the JTextField value is passed through and then have that method call the second introduction2() method which carries on the game now with the created Player class which was created with the inputted name.

Thank you for the trouble everyone has gone through on this, It is really nice of you all!
1 month ago
Just gave everyone here a like for all their help. I have been a long time lurker on this site and decided to join here instead of other communities as it seemed a lot more forgiving to new programmers. Thank you everyone!
1 month ago

Paul Clapham wrote:Well, for your example I don't see why you need two separate classes. There's no reason to have a class which extends JPanel; the class you posted doesn't override JPanel in any way. So you could perfectly well include all of its code in your GUI class. Which, by the way, doesn't override JFrame in any way so it shouldn't extend JFrame either. Just create a JFrame in your GUI class and work with it.

After you do that you'll find that you only have one class and your problem goes away.

There are certainly ways to communicate between two objects (not "between two classes" as you think) but it's better to avoid that until you've studied object-oriented design a bit more.

Let me give you an example of what my program holds in terms of classes. So I have the two GUI classes and a example of some of my other classes are Player, Teacher, Location, Suspect and so on. The teacher class holds a method called introduction lines and in that method the teacher talks to the player. Telling them how the game works etc. Once the teacher is done talking he asks for the players name. Here is where I want to take what the player has inputted from JTextField and return that value back to the teacher class. The name the player has chosen will be used to make a Player class.

That is what I am stuck with. How do I get the players name from the text field to the Teacher class? Also I just wanted to say thank you so much for your help, as you can tell this is all new for me and others have not been as forgiving.

Lastly, just to double check. You are saying its best to make one GUI class that holds the JFrame and JPanel as we do not actually override any methods? If that is what you are saying, it makes perfect sense. However, my lecturer said we will lose marks if we have all the GUI in one class. What I have done with extending was how we were taught so I am not sure why he would tell us to do it that way.
1 month ago

Junilu Lacar wrote:You don't. It depends on how you're asking the user. If it's via a modal dialog, like what this tutorial about creating dialogs shows, you'll get the answer as a return value. If the user has to enter the answer into a text field and then click a "Submit" button, then you'd have an ActionListener on the submit button and then get the value of the answer from the text field.

I am asking the user via the TextArea (have not figured out (yet) how to direct output, for the methods in other classes, to the GUI TextArea). I am unable to do dialogs as we have not been taught that and want us to stay within what we have been taught. Now, in terms of the submit button having a action listener. I had that idea but here is the part were I get stuck. Method from class A asks question, user inputs answer into GUI in class B. How do I get the users answer from class B to class A?
1 month ago
I have a method which asks the user a question and in my GUI I have a JTextArea where they type in the answer. How do I get the method (that is asking the question) to get the value of the JTextField once the user has typed something into the value? I have a action listener I am just unsure what to put in it to get this working

GUIPanel (JPanel class):

I have not yet added the method call yet but basically it will call a method which will ask a question and then the user has to answer. This is what I am stuck on. I do not know how I can get the method to "detect" that the user has entered something and then retrieve the data.

I thought about making the textfield static and making a static boolean which gets changed to true when the action performed (user presses enter) gets triggered but that would mean the method would have to have a while loop to check so there must be a better way out there. Sorry for all this, very new to GUI/Swing.
1 month ago