Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7693323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:03:49+00:00 2026-05-31T21:03:49+00:00

Ok. I have a mainframe which contains two containers. One contains my wizard-alike buttons

  • 0

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;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:03:50+00:00Added an answer on May 31, 2026 at 9:03 pm

    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;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I work with netbeans. I have written a code which has two classes
i have a mainFrame that contains panel. i want in this panel a thread
I have 2 classes, mainFrame and panel. By clicking the button on mainFrame I
I have a container which switches its main content between several screen-sized panes. I'm
I have a frame which I use as the main form (mainFrame), it inherits
I've looked at several other posts, and I have yet to find a clear
So I have a MainFrame class which has a JTable in it, listing all
I have a CMFCRibbonStatusBar in my mainframe to which I add a CMFCRibbonButtonsGroup which
I have a JPanel subclass on which I add buttons, labels, tables, etc. To
I have two pdf files and two text files which are converted into ebcdif

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.