Make heavy use of the MVC pattern. Here's a simple example of what I mean:
class Person
{
String firstName;
String lastName;
// and getters and setters...
}
class PersonSwingModel
{
private Person person;
private javax.swing.text.PlainDocument firstName;
private javax.swing.text.PlainDocument lastName;
// and getters and setters...
// Create some method like init() that initializes PlainDocument values
// to attributes in model.
}
class SavePersonAction extends AbstractAction
{
private PersonSwingModel model;
// and getters and setters...
}
class PersonSwingView extends JFrame
{
private PersonSwingModel model;
private javax.swing.JTextField firstName;
private javax.swing.JTextField lastName;
private SavePersonAction savePersonAction; // hook up to JButton/JMenuItem
// and getters and setters...
// Create some method like init() which binds PlainDocument to JTextField
// and Actions to JButtons or JMenuItems
}
I see some people disagree with extending JFrame or JPanel. I don't. Works for me.
Also, use LayoutManagers. GridBagLayout is very powerful. If you use it, define some GridBagConstraints constants (like LABEL_GBC and FIELD_GBC) and keep reusing them.