Antonio Moretti

Ranch Foreman
+ Follow
since Aug 10, 2021
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 Antonio Moretti

Tim Moores wrote:I forgot the details of what's required in which situation, but I generally just download the latest pdfbox-app-xxx.jar file, and that contains everything I need. Certainly the code you showed would compile with just that.

Agree about the scant documentation; I found some useful PDFBox code examples online.

Thanks. Can you give me a link so I can be sure to use the correct one?

[Edit] Ah don't worry, I found it and used that one. It works now. Cheers.
2 weeks ago

I've been trying to install the libaries in Netbeans for PDFBox 2.0.24.  I added the library files shown under 2.0.24 here:

In Netbean I right-clicked on properties, and then library, and then the plus sign next to CLASSPATH, and added the six libraries.

Once all the classes are imported, everything looks OK, but then I do this simple test to create a blank PDF document:

The IDE does not show any errors, but when I run it, it gives error of "java.lang.NoClassDefFoundError".  I can't really figure out what to do as there is not much documentation on this and I haven't done much with external libraries before. Can anyone help?

2 weeks ago

Stephan van Hulst wrote:Another thing: constructors should be really simple. You should only use them to initialize your fields using data passed to constructor parameters. Constructors of a GUI class can be a bit more complex, to initialize all the visual components, but under NO circumstance should you perform file operations from a constructor.

Instead, create an instance of GameLogic before you call the MainGUI constructor, and pass it to the constructor as an argument.

As a final remark, it's conventional to write abbreviations using PascalCase, so it should be MainGui, not MainGUI.

Thanks & thanks.

So suppose I want to read in stuff from a CSV file when I make a new object. The way I've been doing is it to have method that performs this task, and call that method from the constructor. This is then wrong? May I ask what problem this can cause?
3 weeks ago

Stephan van Hulst wrote:SwingUtilities.invokeLater() causes the function that you passed to it to be called on the Event Dispatch Thread. The EDT is a special thread that you must use to interact with Swing components.

Your issue is caused by line 9 running on the EDT, and lines 11 and 12 running on the thread that called the constructor. So while the Event Dispatch Thread has set the winMain field, the main thread doesn't "observe" this change because you haven't synchronized the two threads, and doing this from the constructor is not really feasible.

ALL of the code in the constructor must run on the EDT, not just line 9. But you really MUST NOT do this by calling invokeLater() inside the constructor.

NEVER start a thread from a constructor. This includes calling JFrame.pack(), JFrame.setVisible() and SwingUtilities.invokeLater() and SwingUtilities.invokeAndWait(), as all these methods may cause a thread to start running indirectly.

The solution is to follow the example that I posted earlier. The GUI's constructor must be called in the function passed to SwingUtilities.invokeLater(), which is called in the main() method. This causes the entire constructor to run on the EDT, without the danger of spawning new threads inside the constructor body. Also note that frame.pack() and frame.setVisible() are not called in the constructor, but instead in a separate show() method which is called after the constructor has returned.

Thanks. I think I follow you, but I would never have figured that calling .setVisible() in the constructor could spawn a new thread. I've seen loads of tutorials that do this, including the most popular Swing course on Udemy. Nor would I have thought of the solution to avoid this.

I feel like I need a list of of what you can and can't put in the constructor now!
3 weeks ago
Something else I tried was to make the JFrame inside the constructor to the GUI app.

I need the reference winMain because later I need to pass it  as the parent when using a child JDialog.

When I try this verion I get a null pointer exception when calling JFRame methods on winMain, but if I remove the instantiation of JFrame from the thread, these method then work. I'm a bit puzzled by this. I don't really understand what invokeLate() is or why we have to use it.
3 weeks ago

Stephan van Hulst wrote:I feel that having a class called "Logic" doesn't really help things. Application logic is usually not encapsulated in one single class. . . .

Thanks...looks at bit advanced for me at this stage, but something to study later for sure.
3 weeks ago
Thanks. The only thing I was wondering about here is the fact that both the Logic app and the GUI app are now in the same thread.

While I was waiting for a reply, I tried a third approach. I made the public static main method instantiate a "Logic" object, and call a "run()" method on it.

In the "Logic" object, there is a constructor that initialises things, and then this run() method take over. The first thing it does is make a JFrame, using the lamda you suggested:

Is an acceptable approach or not?

3 weeks ago

I am making a simple app that uses Swing. The "logic" for the application is one class, let's say "Logic", and the main GUI app is another class, we can call that MainGUI.

MainGUI uses composition to implement a JFrame, and this the main application window. Other Dialogue windows open from here.

Not sure how to structure this.

Firstly, should MainGUI go in a separate thread like this?

Also, as I understand Swing, the main Swing window seems to be where program control resides, so should I instantiate a Logic object from MainGUI? Or should I start by making a Logic object and create the GUIMain object in there?

3 weeks ago

I have made a simple app that has information in different objects which I need to serialize in order to save the program state to file.  What's the best way to do this? Should a create a special save object that collects the stuff I need and save that?


4 weeks ago

Campbell Ritchie wrote:. . . . Check the age of the tutorials . . . JFrame is a kind of frame.

Thanks. It's kind of confusing because Swing is making a come back and I guess there's a lot of old stuff out there. I'm glad I don't have to bother with this method.
1 month ago
I'm having some trouble understanding this method. Some tutorials use it to add components to JFrame, and some don't. Most of what I could find online is just regurgitated explanation that did not clarify. I would like to know what it is for and when we need to use it. Thanks!
1 month ago

Can anyone explain how IntStream is being used here? I don't understand the syntax. It looks like a class that is being instantiated, but there is no 'new' keyword. The API says it is an interface too.

1 month ago

paul nisset wrote:In terms of Swing and MVC, think in terms of events.

A user pushing a button is an event . A user entering or leaving an edit box is an event.
Something(or nothing) happens as result of the event.

That event calls an action.
That action does something with the data in the form.

You might want to have classes that handles different types of actions -That would be your controller.
That controller then might pass the form data onto another class (the model) class to validate the the data.
The form holding the button and edit boxes is the view

These three things can/should be tested independently of each other.
You want them to be loosely coupled ie not dependent on something happening on one layer for something in the other layer to work.

So you want to keep as much java business code out of your forms(views) as possible .
Use the form to gather and display the data and have it interact with model classes that do the heavy lifting.

Think of it like you would if it was a web app .

A web app  =  jsp/html page-> controller/servlet code -> model code validates the form that  the controller passed to it/other business logic -> data persistence retrieval -> Web server sends a response back to the browser.

A swing/console app= form -> validates the form other business logic -> data persistence retrieval -> You (not a webserver) respond back to the view layer of the application .

The main difference is what happens with the controller classes. In a web app it routes to another a page or deals with the data and sends a response back to the browser .
In a console/Swing app the event is what calls a controller action that action then processes the event , models any data passed to it . It then processes the model data then sends a response back  to the form application .

Hope this helps.

Very much so. Thanks!
1 month ago
They were the Swedish extra alphabetical characters: ä, ö and å.
1 month ago

I have been looking at a Swing app that has some special characters in it. The compiler warned me when I tried to run it in Netbeans that they might not display properly. Then the characters in question were displayed as boxes. This seems like an obvious error to do with encoding so hopefully someone can tell me how to fix it. Thanks.

1 month ago