0

I have a JTabbedPane with 4 tabs. I would like to load 1 tab 1 after another as the tabs uses, refers and retrieves from the same database. And this is causing an issue of "Database is locked" in my application.

Thanks in advance for the help and suggestions :)

This is how I create the JTabbedPane

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setBounds(0, 0, 450, 300);

    tabbedPane.addTab("tab1", new class1UseDb());
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    tabbedPane.addTab("tab2", new class2UseDb());
    tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

    tabbedPane.addTab("tab3", new class3UseDb());
    tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

    tabbedPane.addTab("tab4", new class());
    tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
user1321096
  • 143
  • 3
  • 14
  • 1
    Please post all relevant code... As the question currently stands, we are unable to help you. – initramfs Nov 08 '13 at 10:29
  • Added a little of the code, wonder if it helps. :) – user1321096 Nov 08 '13 at 10:56
  • 1
    You probably need a lot more information on how the database is accessed. The code you posted shows four new tabs being added all at once with nothing much in it. Are you implying the data retrieval is time-consuming and you want to display each time as the data becomes available? Please post more information/code regarding your issue. Also explain where this "Database is locked" is coming from. – initramfs Nov 08 '13 at 11:10

1 Answers1

1

Based on this example, the sscce below simply creates a new panel for each click on the Add button and fills in the result. In a real program, you may want to use a SwingWorker to manage latency and resources.

image

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.h2.jdbcx.JdbcDataSource;

/**
 * @see https://stackoverflow.com/a/19860170/230513
 * @see https://stackoverflow.com/a/15715096/230513
 * @see https://stackoverflow.com/a/11949899/230513
 */
public class TabData {

    private int n = 1;

    private void display() {
        JFrame f = new JFrame("TabData");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTabbedPane jtp = new JTabbedPane();
        jtp.add(String.valueOf(n), createPane());
        f.add(jtp, BorderLayout.CENTER);
        JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        p.add(new JButton(new AbstractAction("Add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                jtp.add(String.valueOf(++n), createPane());
                jtp.setSelectedIndex(n - 1);
            }
        }));
        f.add(p, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JPanel createPane() {
        JPanel p = new JPanel();
        JLabel l = new JLabel();
        p.add(new JLabel("Result: "));
        p.add(l);
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:file:~/src/java/jdbc/test;IFEXISTS=TRUE");
        ds.setUser("sa");
        ds.setPassword("");
        try {
            Connection conn = ds.getConnection();
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("SELECT RAND() FROM DUAL");
            rs.next();
            l.setText(rs.getString(1));
        } catch (SQLException ex) {
            ex.printStackTrace(System.err);
        }

        return p;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TabData().display();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045