Ok. I have a mainframe which contains two containers. One contains my wizard-alike buttons and the other one a contentPane. I have several classes Window1, Window2 etc. which I instantiate in my mainframe class and then toogle through via a cardLayout which decides which Window class to be visible in the contentPane.
Each of these Window classes need a connection to a database. Currently I instantiate a database connection in the window classes separetly but I want some form of a global session where I'm connected from the moment that I passed my Window1 class (also known as the login class) until I close down the application, so that when I get to the other Window classes, I can read and write to the database using this session.
My MainFrame class:
public class MainFrame {
private static void createAndShowGUI() {
JFrame frame = new JFrame("Stackoverflowquestion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
final JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout(5, 5));
Database db = new Database();
//Trying to make some kind of global database instance but fails
Window1 win1 = new Window1();
contentPane.add(win1);
Window2 win2 = new Window2();
contentPane.add(win2);
Window3 win3 = new Window3();
contentPane.add(win3);
Window4 win4 = new Window4();
contentPane.add(win4);
Window5 win5 = new Window5();
contentPane.add(win5);
Window6 win6 = new Window6();
contentPane.add(win6);
JPanel buttonPanel = new JPanel();
final JButton previousButton = new JButton("< PREVIOUS");
previousButton.setEnabled(false);
final JButton nextButton = new JButton("NEXT >");
final JButton cancelButton = new JButton("CANCEL");
buttonPanel.add(cancelButton);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
nextButton.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Verifiable verifiable = null;
Component[] contents = contentPane.getComponents();
for(Component component : contents) {
if(component.isVisible() && component instanceof Verifiable) {
verifiable = (Verifiable)component;
}
}
if(verifiable != null && verifiable.isDataValid()) {
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
previousButton.setEnabled(true);
nextButton.setCursor(Cursor.getDefaultCursor());
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
//Should close the database session
}
});
frame.add(contentPane);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Example of a Window class:
public class Window1 extends JPanel implements Verifiable {
public static final String IDENTIFIER = "FIRST";
JTextField txtUsername = new JTextField();
JPasswordField txtPassword = new JPasswordField();
Database db = new Database();
//As I said, I currently intantiate a database connection separetely which
//I want to get rid of
public Window1() {
init();
}
private void init() {
JLabel lblUsername = new JLabel("Username:", JLabel.CENTER);
lblUsername.setBounds(10, 91, 135, 77);
txtUsername = new JTextField();
txtUsername.setBounds(155, 116, 188, 27);
JLabel lblPassword = new JLabel("Password:", JLabel.CENTER);
lblPassword.setBounds(0, 163, 149, 77);
txtPassword = new JPasswordField();
txtPassword.setBounds(155, 188, 188, 27);
setLayout(null);
add(lblUsername);
add(txtUsername);
add(lblPassword);
add(txtPassword);
String title = "Log in";
setBorder(BorderFactory.createTitledBorder(title));
}
@Override
public boolean isDataValid() {
String username = new String(txtUsername.getText());
String password = new String(txtPassword.getPassword());
try {
DatabaseConnection conn = db.getDatabaseConnection(username, password, "test", "test", "test");
db.getTest(conn);
return true;
} catch (LoginFailedException e) {
JOptionPane.showMessageDialog(this, "Something went wrong",
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}
@Override
public String getIdentifier() {
return IDENTIFIER;
}
}
I would use the singleton pattern. It basically allows you to have one instance of an object and you can get it anywhere. Learn more here: http://en.wikipedia.org/wiki/Singleton_pattern
In my example below, you could have a database connection which you would use to create any calls to the database you would like. I am doing this exact same thing in a project I'm currently working on. It works great :)
Here's an example:
public class DatabaseConnection {
private static DatabaseConnection instance; //note this is static
private DatabaseConnection() { //note this is private
}
public static DatabaseConnection getInstance() { //note this is static
if (instance == null) {
instance = new DatabaseConnection();
}
return instance;
}
You could also try what Wikipedia calls the traditional way (after seeing this I think this is how I'll do it from now on).
public class DatabaseConnection {
private static final DatabaseConnection instance = new DatabaseConnection();
// Private constructor prevents instantiation from other classes
private DatabaseConnection() { }
public static DatabaseConnection getInstance() {
return instance;
}
}