Question

How would one best organise his GUI. I have a JFrame which will hold my application. Currently I have put all JPanel, JTables and others which will fill up the JFRame inside the custom JFrame class.

I instantiate these components there and save them in a local field. Would it be a better practice as to make a seperate class for each JPanel,JTable and use getters and setters to manipulate the objects?

For example. My current GUI class is about 3500 lines long which gets more complicated over time.

Thanks in advance.

EDIT (he wanted screenie):

Targui - The game

Was it helpful?

Solution

I would definitely suggest you start to separate your GUI components into different files, it will help in the long run. As far as best practice goes, I think it is better than cluttering a file with multiple classes even if they are related to each other. The only benefit I can see is you have only 1 file to open to see all the GUI code.

As you said, having a long class (3500ish lines) can make it troublesome to maintain. Organize them in proper packages too while at it. Like making a gui package, or whatever name you feel is appropriate, and so on.

Good advices are given in the answers of the questions tagged user-interface and organization

OTHER TIPS

Break up your GUI code into custom panels.

ie:

class MyPanel extends JPanel { .. }

You should be able to break up the frame into logically distinct panels.

For a busy frame maybe I would have three custom panels. But this can drastically vary depending on what you have.

Also have a controller class. The controller handles backend calls to delegates, and event listening. Your GUI classes, eg customer panels, or custom frame should contain barely any if or loop statements. All that kind of logic should be in your controller.

More complex GUI code is usually generated by an IDE and not hand-coded. Sometimes, you might edit the generated code to get certain effects, but for very complex setups it becomes overly burdensome to hand-write the Swing code. Better to use a tool to generate it for you.

If you already have 3,500 lines of code, it's probably time for you to consider a GUI builder to take some of this work away from you. The learning curve can be steep, and it's not the easiest thing to do, but I would suggest it's way easier than hand-modification of a large GUI.

So basically, the answer is that for moderate/complex GUIs, people don't usually do it the way that you're doing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top