Reputation: 12372
I'm new at programming java desktop applications, so I would appreciate some help...
I added the components of the frame with the builder.
When I click in a button of my main frame, I am showing the dialog like this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
BehaviorDialog behavDialog = new BehaviorDialog(this, rootPaneCheckingEnabled);
behavDialog.setVisible(true);
}
And my BehaviorDialog
class is like this:
public class BehaviorDialog extends javax.swing.JDialog {
/**
* Creates new form BehaviorDialog
*/
public BehaviorDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
setTitle("Add behavior");
setLocationRelativeTo(this);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
//....
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
BehaviorDialog dialog = new BehaviorDialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
//...
// End of variables declaration
}
My questions are:
This is the correct way to start frames/dialog? (It works, but I would like to be sure if it is the best way...)
When I remove the invokeLater()
in the main
, it seems to work the same way... Should I keep it or can I remove it? What are the consequences of removing it?
Upvotes: 0
Views: 8104
Reputation: 115388
It is one of the possible ways. There are people that like to create subclass for each dialog/window, so the class is managing its own layout and (often) behavior. Other people like delegation more, i.e. do not create subclass for each dialog by create a kind of factory method that creates dialog and its layout. I think that this approach is better because it more flexible. You can easier separate your code to layers. For example layer that creates main panel, sub-layers that create sub panels, higher layer that puts panel into the dialog etc. In future you can replace layer that deals with dialog and put the same layout into JFrame or into other panel etc.
Concerning invokeLater()
- it just runs your code asynchronously. This does not make sense in your case. But it is useful for actions that take time. For example if your want to perform action that takes 10 seconds on button click you probably want to run this action asynchronously. Otherwise your GUI will freeze for 10 seconds.
Upvotes: 4
Reputation: 2181
I don't think there is anything wrong with your code. But you should definitely keep invokeLater because it handles the threading for you. that is, it queues this event until other AWT events finish. This makes you sure the interface keeps running smoothly.
But of course if you want to reuse this JDialog somewhere else (and want to do small modifications like location and size), I would suggest you move the setTitle and (probably) setLocationRelativeTo methods to the invoking frame.
Upvotes: 3