• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • paul wheaton
  • Paul Clapham
Saloon Keepers:
  • Piet Souris
Bartenders:

Java Swing

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I'm an Apprentice in 2nd year and I've just learned about Swing.
Currently my task is to experiment and try to make something.
First I made a rapid prototype.

Here is the Code to the Prototype:



This is what the prototype looked like and then i tried to do a second version of it and trying to refactor and use best practices.

Here is the updated version:

Main.java


MainWindow.java


CenterPanel.java


EastPanel.java


SouthPanel.java


TopPanel.java


I used static on the components since my job was to make a single page with some stuff on it since it wouldn't change and i don't need to add something.

What can I do better or what should i change?

Kind Regards
Vijiyarathan Rithush
 
Saloon Keeper
Posts: 5788
221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vijiyarathan,

two remarks that I want to make:

1) never make a variable static unless absolutely necessary! Even if you intend to never change them.
2) in your MainWindow you have the fields topPanel, et cetera. Nothing wrong with that, but do you need these references? If not, then you might as well do (in the constructor)

I prefer PAGE_START over NORTH, because NORTH is absolute, while PAGE_START takes the Locale into consideration: if a locale has a page going from bottom to top. then PAGE_START will put the panel at the bottom. But that is just a minor detail.
 
Vijiyarathan Rithush
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info!

I've talked with my teacher and he said I shouldn't let the classes inherit. He said something about composition over inheritance too, so i tried to implement it.
I'll try your tips as well.

This is the current state of my code:

Main.java


MainWindow.java


TopPanel.java


SouthPanel.java


CenterPanel.java


EastPanel.java


Let me know if there is anything else i should know or learn!
 
Marshal
Posts: 81758
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am pleased to see you aren't using extends any more. Your application usually ISN'T‑A‑JFrame, but it HAS‑A‑JFrame. It may be a good idea to change your fields to be local variables in the constructor or (maybe better) a setUpGUI() method. That will give you access to them, as Piet mentioned.

I shall move you to our forum where we usually discuss GUIs.
 
Vijiyarathan Rithush
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by using the setupGUI method. I'm asking myself what it means.

What my teacher also mentioned is that i had a getComponent() in the MainWindow.java class, which enabled me to execute ALL the other methods there is, but I only need the getComponent() method.
 
Piet Souris
Saloon Keeper
Posts: 5788
221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are doing the layout in the constructor. That is fine, and I always do that. Some people prefer to do the layout in a separate method, usually called setUpGUI. And when you use a dedicated GUI-builder, well these also work with a separate setUpGUI-method. Whether to use this or not, that is a personal preference.

Your code looks much better now! You call your method 'getComponent', I would prefer 'getPanel' instead, is more clear, and 'getFrame' for the main class. I would even go one step further. For instance in your EastPanel-class, I would make the constructor private, so that you cannot create directly an instance. Instead, I would have the method 'getPanel' as follows:

and then in MainPanel:

But this is what I would prefer. You could do exactly the same for your MainWindow, so in Main:
 
Vijiyarathan Rithush
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info!

I'm wondering why you can do this:



How can I come up with this since I never knew that you can do that.

I will provide my updated code. Please look if i can do something better coding wise...

Main.java


MainWindow.java


TopPanel.java


SouthPanel.java


EastPanel.java


CenterPanel.java


The main thing i want to know is how I personally can come up with these solutions you came up with.

May I also like get a code review. I wanna know what i did good and what i could improve on.

Thanks in advance!
 
Bartender
Posts: 29040
214
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Vijiyarathan,

two remarks that I want to make:

1) never make a variable static unless absolutely necessary! Even if you intend to never change them.
2) in your MainWindow you have the fields topPanel, et cetera. Nothing wrong with that, but do you need these references? If not, then you might as well do (in the constructor)

I prefer PAGE_START over NORTH, because NORTH is absolute, while PAGE_START takes the Locale into consideration: if a locale has a page going from bottom to top. then PAGE_START will put the panel at the bottom. But that is just a minor detail.



I don't know of any locales where displays run from bottom to top, but left-to-right/right-to-left is a thing. But I wouldn't recommend EAST or WEST for that.

Of course, old-time European traditionalists all know that the proper location of map direction is with Jerusalem at the centre, and East may be at the top.
 
Piet Souris
Saloon Keeper
Posts: 5788
221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!

Your code looks very fine to me! I would change one thing, though. Lets look at the CenterPanel class. You have three private final fields. That is fine, but if you look at the code, you see that you only need a reference for the panel, since we return that in the 'getPanel' method. We do not need a reference for the other two fields, so we can create them in the constructor. So we would get:


Vijiyarathan wrote:
The main thing i want to know is how I personally can come up with these solutions you came up with.


Experience and personal preferences. I like to use static factory-methods to create instances, and so I use that the last couple of years whenever that is handy. But nothing wrong with the new ...  construction! Use that if that is what you prefer.
 
Vijiyarathan Rithush
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I want to learn things like these. Where can i read or watch something about this?
 
Campbell Ritchie
Marshal
Posts: 81758
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am usually dubious about things found online; YouiTube is full of pretty poor tutorials. Baeldung is an exception; his site is good. It is probably worth your while getting a copy of Effective Java by Jshia Bloch [Sun Microsystems Press/Addison‑Wesley/Pearson]. Get the 3rd edition if possible, which came out about 2017. You will find a discussion of the advantages of factory methods in ยง1.
I think I might not use a factory method for that panel; all you are doing is adding components to it. I think you can do that as simply without that method. This goes to show there are usually several different ways to program your app. You could however consider a method like getPanelWithScrollPane() which will create the thing for you complete with scroll pane. You could pass the component to put inside the scroll pane as an argument by changing the parameters to such a method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic