I have made a Swing application which is rather simple in functionality. However the code which it consist of became rather large and very messy in my opinion. All the swing components and actions is in one file. So for instance if I was to make a even larger application with more functionality the code will be rather hard to go through.
So my question is how to make a good structure of the code. Or if there's a good webpage I can read about it, and if possible some code examples. I have checked Sun's tutorial about Swing, but those a rather simplistic examples they've shown.
UPDATE: I have pondering a while and check for some examples. I don't know if I got the MVC pattern correct. Anyway my idea is to seperate each JFrame to their own class file. Afterward I have one MainFrame which is the main window for the application. From that JFrame I create one instance of each JFrame I have. And call those frame from the MainFrame with Actions. I don't know if this is a good idea. However it makes the code significant easier to read anyway.
Here's an example of how I meant
class Main implements ActionListener {
private JFrame frame = new JFrame();
private JButton button1 = new JButton();
private JPanel panel = new JPanel();
private FirstFrame frame1 = new FirstFrame();
private SecondFrame frame2 = new SecondFrame();
private ThirdFrame frame3 = new ThirdFrame();
public Main() {
button1.addActionListener(this);
}
public createGUI() {
frame.setTitle("Main");
frame.setSize(400,300);
panel.add(button);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String args[]) {
new Main().createGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1)
{
frame1.enable();
}
}
}
August 3, 2021. Java Swing is part of Java Foundation Classes. It is used to create window-based applications which makes it suitable for developing lightweight desktop applications. Java Swing is built on top of an abstract windowing toolkit API purely written in Java programming language.
As we mentioned before, Swing provides three generally useful top-level container classes: JFrame , JDialog , and JApplet .
Advantages of Swing Swing components are platform-independent. Swing components can use a different look and feel. Swing components use the Model-View-Controller paradigm (MVC) and thus can provide a much more flexible UI. Swing components are lightweight (are less resource-intensive than AWT).
Use Model-View-Controller design pattern. It deals with separating user interface code from Business logic.
EDIT :
As OP is looking for organized code rather than flexible design, I have removed the NetBeans UI Designer part from my answer.
Organizing user interface code of any significant size is a problem with surprisingly little written about it, considering that every application has the same problem. There are some techniques that can help:
The basic design techniques have been around for quite some time, and are well understood, but applying the techniques on a larger scale is something that seems to be designed individually for each application.
One approach that I have seen applied effectively is to separate out the Events, Listeners, Actions, etc into their own classes and either organize them into packages by type, or use a consistent naming convention across packages so that a component and its associated classes are easily identifiable (XDialog, XDialogMouseListener, XDialogCancelAction, etc).
Another approach would be to look at some large open source applications (Eclipse, Firefox, ...), and see how they organize their GUI code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With