简体   繁体   中英

Java - Load JTabbedPane one tab after another

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

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.

图片

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM