Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 531 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swing desktop app - how to organise my code

#1
We are going to use Swing for our next project which will be a lightweight desktop app.

I've been reading the Swing tutorials on Oracle's website and have started to get the hang of it.

I was told that instead of switching `JFrame` I need to work within one JFrame and switch `JPanels`.

My questions to you would be the following.

Can I create the JPanel designs (or their templates) in design mode (WYSIWYG editor) and call `jframe.setContentPane(nameOfJPanel)` or do I do them programatically in code?

Secondly, how do I structure my code... If I dynamically add new JPanels and bind their events to an event handler.. all my logic and code will be in one HUGE class with tens of methods. I just don't know how to proceed and nowhere can I find an example with multiple (more than 6 let's say) panels on the internet.

Reply

#2
This is how I organize my code:

class Panel1 extends JPanel{
//code for panel1 and its Components
}

class Panel2 extends JPanel{
//code for panel2 and its components
}
// and it follows.
class ApplicationFrame extends JFrame{
/// LOGIC to switch between panels
}

class Main{
//contains main() function
}

Any HAND-CRAFTED GUI code, is better than a COMPUTER GENERATED WYSIWYG -APP Code.
Reply

#3
## To WYSIWYG or not WYSIWYG ##
This is a debatable question at the best of times.

I encourage all my junior developers to start out hand coding UIs as it teaches them important basics about how to use layout managers and how to handle compound layouts.

This tends to take longer as you need to verify the layout with each change.

I personally use the form editor in Netbeans for most of my general work, but will hand tweak UI's

## Structure ##

Think about boundaries of responsibility, reuse and reduce strategies.

What you don't want is some huge master class that does EVERYTHING. It will be difficult to maintain and update (I live with this horror every day).

Instead, identify the distinct areas of responsibility and either use getters and setters or models to move the data around the application. The more you can decouple your code, the easier it will be to update and modify.

Identify like work and model it as interfaces and abstract classes where you can. Basically where ever you start thinking about coping code is probably a good indication that your design is off and you should consider implementing abstract classes to cover the overlap.

Take advantage of the `Action` API for replicating commonly used concepts (copy and paste is an example of this. You would want menu items, possible toolbar items and maybe even popup items, these can all be handled by the same `Action` class).

Separate the data from view. As I said before, take advantage of models. The data shouldn't care how it is collected or modified, only that it can be. Equally, the view shouldn't care about how the data is managed, only that it is.

If possible, define interfaces between the separate areas of your application. This way you can further decouple of the application and no one part becomes reliant on any one implementation (hello my world :P)

Don't be tempted to simply dig through a component hierarchy to gain access to that field, it will produce a nightmare if you need to change the code!!
Reply

#4
The JFrame will be the main window of your app and the panels will be the brick composing it.

You should create each panel in its own class and you can arrange them by functionality in packages. You'll have panels for holding content of logical part of your application and panels that contains real stuff.
A logical panel could be the main view and the menu bar. An application panel would be a form, a menu, a canvas...

You'll have to put application panels inside logical panel and to change the content of the logical in response to users actions.

You should study how layouts work in order to compose the view inside the JFrame and to layout components inside panels.

For example the BorderLayout is a classic when defining the main area of an application:
- menu and toolbar on top
- browser on the left
- status bar at the bottom
- main panel on the center

You can use WYSIWYG editor, but avoid doing all design inside the same class. Else you'll have an horrible HUGE class. Create your panels in separate classes and compose them in your main view.
You can use empty panels as placeholder to help you create the structure of your application.

You'll have to bind your domain data to the view in order to not mix the two layer.
Bind means that you will write a way to go from a java bean to a form and the reverse.
Basic binding is handwritten, but some tools exists to do that.
If it is a small application, it is maybe better for you to handwrite everything.

Building a Swing application can be very tricky (you'll have to know about the event dispatch thread, layout management, event management, widgets (label are trivial, but JTable can be very complicated to handle);

I'll recommend to find a book about the topic, and to find some open source swing application in order to study how it structured, before you start your project.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through