Mark Andrew Davis

Greenhorn
+ Follow
since Jul 13, 2017
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
8
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mark Andrew Davis

Paul Clapham wrote:

Mark Andrew Davis wrote:I am not 100% confident at reading this, but from what I can see is it is stating TollBar class Line 80 has a nullPointException



Yes, that's exactly right. And that means that the tabbedPane variable is null.

But if you accept my recommendation about the MainTextArea class, you wouldn't do that. You would write some code which gets a new JTextArea (from a MainTextArea object) and puts it into a scroller and adds that to the tabbed pane and sets the focus to point to it. You could of course put all of that into a method, which would be a good thing, but it would be a method of the class enclosing line 80 and not a method of some other class.

At least I think so. It's always hard to deal with the problem where somebody wrote a ton of code and then realized their object design was all out of whack and needed to be rethought. Which is very common with beginners starting with Swing GUIs.



Thanks for all your assistance paul! You put it into easy to understand terms for me :) I will work on refactoring a few things and write some code to perform advised and let you know how I go.
7 months ago

Paul Clapham wrote:

Mark Andrew Davis wrote:So what I want to do is, in ToolBar class I want to execute the bellow code so it will create a new blank tab. But when I run this I get a nullPointException.



You normally get a stack trace when an exception is thrown. It contains all sorts of useful information like the class, method, and line number where the exception occurred. In the case of a NullPointerException the thing to do is to look at the line identified at the top of the stack trace and see what variable unexpectedly had a null value.



Here is the stacktrace:



I am not 100% confident at reading this, but from what I can see is it is stating TollBar class Line 80 has a nullPointException

7 months ago

Paul Clapham wrote:

Don't tell that object to request focus. Ask it to request focus.

What I mean by that is, if something owned by a MainTextArea object is supposed to request focus then there should be code in the MainTextArea class which requests focus for it. If you want something outside MainTextArea to ask for that to happen, then there should be a public method in MainTextArea which allows it to ask.



Are you able to assist me on how I can achieve this? I've tested a few different ways but I don't think I figure out the code on how to achieve this.

If it's okay could you please explain along with the code so I can learn? I much prefer trying to figure out for myself but I think I am going in circles haha.

Thanks paul :)
7 months ago

Paul Clapham wrote:

Mark Andrew Davis wrote:also could you please advise me on how I can pass reference Toolbar to TabbedPane correctly?



I'm sorry, I don't understand that. I don't see why a TabbedPane needs a reference to any Toolbar objects.

As for your MainTextArea class, the textArea variable should be private. You have a method which wraps it in a scroll pane and returns it, so there's no need to expose it to the world on its own. I also don't think you need to create and disregard a scroll pane in the constructor.



haha that's okay! I think maybe I am the one confused and creating a domino effect of confusion...

So what I want to do is, in ToolBar class I want to execute the bellow code so it will create a new blank tab. But when I run this I get a nullPointException.



tabbedPane.addTabbedPane(); refers to the bellow code added to TabbedPane calss:



Is this the wrong way to go about it?

As for the MainTextArea I used to have it as private but I couldn't get the OpenDataItem and SaveDataItem to work (Located in ToolBarClass)

In the OpenDataItem for example, I have the bellow code which works (because TextArea is public):



If I make it private I thought I should be able to do it as followed, but didn't work either:




Lately these are the kind of things I am having trouble with... Either making reference to another class, an object an method etc. This is the whole basis of object orientated programming, but I'm slowly starting to piece everything together.

I hope this makes some what sense? And thanks for your assistance again :)

Mark.D
7 months ago

Paul Clapham wrote:
So first, it extends JPanel so it's a JPanel. And when you create it you initialize it so it contains a JTabbedPane and nothing else (from the GUI point of view, anyway). But then it contains the setTabbedPane and getTabbedPane which you always call like this: setTabbedPane(getTabbedPane()). It looks to me like the purpose of this construct is to add a new tab to the JTabbedPane. So why not just have one method:


Next I looked at your ToolBar class. It extends TabbedPane, which extends JPanel.  Having ToolBar extend JPanel is sort of defensible, but having it extend TabbedPane is confusing, because it doesn't need to be a TabbedPane. And both the ToolBar class and the TabbedPane class have a private MainTextArea member, which is a recipe for confusion. It's probable that the error you're asking about stems from this.

However there's this rule of thumb: "Prefer composition over inheritance". In your case that means that a ToolBar object should have a reference to a TabbedPane object, rather than being one. Likewise a MainTextArea object should have a reference to a JScrollPane (in fact, it already does) rather than being a JScrollPane. I'd really recommend that you modify your code to follow that rule, because a lot of code looks like you inherited a class just to avoid creating an object of that class.



Thanks Paul for your assistance.

- I have removed the extends TabbedPane from ToolBar
- I have also removed the private MainTextArea object from ToolBar and TabbedPane
- I have amended my code to include the above code you advised. This was actually my first way of doing things, but my issue I came across was that I didn't know how to properly pass reference to this through.
- At present i get a NullPointException when I press the New Document button, I am assuming this is due to the object not being initialized. That's due to the fact I don't know how I should pass this through correctly.

Are you able to review my changes and see if they fit more now? also could you please advise me on how I can pass reference Toolbar to TabbedPane correctly? I believe this is the reason why I turned to setters and getters in the first place(probably not the best approach).


TOOLBAR:


TABBED PANE:


MAINTEXTAREA


Thanks,
Mark.D
7 months ago
Hey Team,

I hope you are all having a wonderful week!

I have decided to make a Notepad application like windows notepad as a learning curve. As such, I have hit a little road block.

ISSUE
I have a JMenuItem called NewDataItem which is supposed to create a new blank document in a JTab. But the issue is, when I press New it doesn't do anything.
Something I tested was making my (in TabbedPane class) a public static and it worked! YAY!! But, I don't know why...

I don't want to leave this as static unless I know I have too, but without first understanding the cause, it is best not too.

I have created my Tabbed Pane, Main Text Area and Toolbar all in different classes for now as I would like to expand on them over time.

Is anyone able to assist?

Code Bellow:


MAIN CLASS:


MainFrame Class


Main Text Area Class



ToolBar Class



Tabbed Pane Class

7 months ago
Hey Guys,

I hope you are all well!! I was wondering, what are different ways to use/initialize a constructor and what are examples/purposes you would you them over another?

Apart from I don't really know many others and I do find myself hitting some walls due to such, so thought I would ask and expand my knowledge.

Thanks,
Mark.D
7 months ago

Paul Clapham wrote:There are some aspects of your code which don't make sense.

For example: your ToolBar class extends JPanel, but it doesn't override any JPanel methods and it's never used as a JPanel. Instead its entire purpose seems to be to produce a specialized JMenuBar. So why doesn't it just extend JMenuBar in the first place?

But no, that wouldn't make much sense either. because your JMenuBar isn't actually a specialized JMenuBar at all. It's just an ordinary JMenuBar. Sure, it's got menu items in it which do specific things, but that's normal -- you always want a menu item to do a specific thing.

So I think your problem arises only because you've taken your GUI initialization code and chopped it up into two separate classes. Personally I would do all of the GUI initialization in one place.

But if you just want an instance of your ToolBar class to have a reference to a particular GUI component, then pass that reference into the ToolBar class via a constructor parameter or a method parameter.




Thanks for your swift reply!!! I am still learning so for me, I take concepts from what I see and learn. At the moment I am watching the Udemy course Java Swing (GUI) Programming: From Beginner to Expert and in the course he creates the menu in a new Class which extends JPanel.

So since this was a course, I followed the same practice as I thought it must be how you should do things.

But anyways, in this case, should I just make it all one class? I was advised that it's best to keep all components/objects in its own class and use constructors/methods to pass-through and make reference to the different objects. Is this not correct?

Thanks again :) 
8 months ago
Hey Guys,

I hope you have all been well!! I've been busy practicing my programming and going through the 1000+ Programming idea's to get me started.

Currently I am working on making a Notepad application (trying my best to mirror the Native Windows Notepad application) and adding a few minor tweaks to it.

At the moment I am working on Opening and Saving text files, present it is working like it should. But, currently I have a public static JTextArea (which isn't ideal).

The reason I am using a public static JTextArea is due to the fact, I am unsure how to pass-through/interact the BufferedReader to the JTextArea in another class.

I wasn't to sure how to word this in order to search in google successfully, so thought I would make a post.

As you can see on my code below, Class: MainFrame contains the JTextArea and Class: ToolBar contains the open file and save file code.

When I perform textArea.read(); & textArea.requestFocus(); this is correct and working, but I don't like the fact I am using a public static for the JTextArea.

Could someone please teach me the "Professional" / "Clean" way of passing/interacting a bufferedreader to a JTextArea in another class?

Main:




MainFrame:



Toolbar:



I hope this all makes sense!
Thanks,
Mark.D
8 months ago

Dave Tolls wrote:Your call to testMethod is not part of the textInputListener, which is the lambda on lines 94 and 95.
So it never gets called (well, not as part of the button push anyway).  Indeed, you see a "1" because line 95 is appending the value the user has entered.

Since you want the user input to drive the output to the test area create a method called something like getDisplayText which takes a char (since it seems like you are limiting the user to a single character), or a String.
That method will do what testMethod does, except rather than doing the append to the text area it will return the correct text String.
The switch will be then based on the String/char passed in.  No point converting to an int.

Then your listener can do on line 95:


Obviously if this becomes more complex you would want to handle this flow differently, but you should be able to limit the code changes to the getDisplayText method.




Sorry, are you able to show me an example? I am a little confused. I greatly appreciate your assistance
10 months ago
I prefer watching a video and then going back to books after. I am not a 100% logical learner so having someone break things down and explaining it into scenario cases helps a lot, even just to add a nice foundation so when I go back and read the books, I have something to build off. It's hard to find video's that covers everything in fine detail that's in a book, so having both is really great.
10 months ago
Thought I would include a screen shot so you know what I am trying to achieve.

As you can see default append text shows and displays some text for input. At this stage I trigger buttonState = 1; for int

At this point I also need the listener to go back to the switch statement so the switch knows what case to play.

Hope this makes sense.

Thanks,
Mark.D
10 months ago

Your call to testMethod is not part of the textInputListener, which is the lambda on lines 94 and 95.
So it never gets called (well, not as part of the button push anyway).  Indeed, you see a "1" because line 95 is appending the value the user has entered.



Yep, this I realized. The issue is I don't know the correct way to do this...

Since you want the user input to drive the output to the test area create a method called something like getDisplayText which takes a char (since it seems like you are limiting the user to a single character), or a String.
That method will do what testMethod does, except rather than doing the append to the text area it will return the correct text String.
The switch will be then based on the String/char passed in.  No point converting to an int.

Then your listener can do on line 95:


Obviously if this becomes more complex you would want to handle this flow differently, but you should be able to limit the code changes to the getDisplayText method.



So, I actually cut some of my code off on purpose for this issue. I have limited the code because on this particulate text adventure games I will gave three types of scenarios for user input.
1. I have no text inside the player input field and when enter is pressed, it will display next appended text.
2. Only accepts ints
3. Only accepts strings

As followed:




So, In particular instances I would want the user to enter a string when for example a it prompts to enter his name. Then I would require a switch choice of 1 - 4 to choose where the player wants to go, etc.
I can set the state by calling buttonState in my code and set it when I need to change the function of my buttons, which confirmed working. The issue is, I need to connect my switch statements so they listen for user input which I can't get working...

I do thank you for your time to assist me, I am still learning so I get confused every now and then on different components due to lack of experience to link the knowledge back too.
10 months ago

Paweł Baczyński wrote:What is the value of playerChoice at the beginning of testMethod()? Have you tried displaying the value?
I can't see any assign statement.



So I want the value of playerChoice to be equal to whatever I put in the JTextField. For example if I put 1 in JTextField it will play case 1 , if I put 2 it will play case 2 and if i put not a stated case I will do an output of something like "not valid'.

I believe I need to some how link it back to my Listener but I am not to sure how I do this. I know if this was a console I can use scanner which then i would do playerChoice - userInput.nextInt(); (where userChoice = new scanner).

Hope that makes sense.

Thanks,
Mark.D
11 months ago
I am making a Text Adventure Game with a GUI as a self study project. In my game, I want users to be able to submit an int or string from the JTextField and when button is pressed, the player choice will display on the JTextArea and also the method that follows from the switch statement case will execute.

I have created a test code here to replicate the issue (excuse the ugly GUI)


This is the main class


And this is the Event:


With this test code you can see that when I select int 1 and submit from JTextField to JTextArea, "1" is displayed to the JTextArea but not the append text that follows case 1.

I have a feeling this is due to lack of knowledge of how to use switch statements with listeners. Happy for advise, links to knowledge, examples, anything that will allow me to not only get my code working, but to truly understand what I am doing. Thanks, Mark.D
11 months ago