繁体   English   中英

单击项目选项卡后,如何刷新JTabbedPane选项卡?

[英]How do I refresh a JTabbedPane tab when the item tab has been clicked?

我目前正在使用JTabbedPane的Java应用程序上工作。

我想刷新JTabbedPane的选项卡,但是当我单击“选项卡”项时,选项卡不会刷新。

可能是什么问题呢?

public class MainClass
    extends     JFrame
{
private     JTabbedPane tabbedPane;
private     JPanel      panel1;
private     JPanel      panel2;
private     JPanel      panel3;
public      JPanel      panel4;
private     JPanel      panel5;

public MainClass()
{


    setTitle( "Demandes d'autorisation d'absence" );
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize( 800 ,400  );
    setBackground( Color.gray );

    JPanel topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );

    // Create the tab pages
    createPage1();
    createPage2();
    createPage3();
    createPage4();
    createPage5();

    // Create a tabbed pane
    tabbedPane = new JTabbedPane();
    tabbedPane.addTab( "Mise à jour", panel1 );
    tabbedPane.addTab( "Demandes en cours", panel2 );
    tabbedPane.addTab( "Demandes autorisées", panel3 );
    tabbedPane.addTab( "Demandes autorisées mise à jour", panel4 );
    tabbedPane.addTab( "A propos", panel5 );
    topPanel.add( tabbedPane, BorderLayout.CENTER );
}

public void createPage1()
{
    panel1 = new JPanel();
    panel1.setLayout( null );

    JLabel label1 = new JLabel( "Mise à jour des autorisations :" );
    label1.setBounds( 10, 15, 300, 20 );
    panel1.add( label1 );


    JButton b = new JButton("Actualiser");
    b.setBounds( 10, 55, 150, 20 );
    b.addActionListener(new miseajour());
    panel1.add( b );

}

public void createPage2()
{
    panel2 = new JPanel();
    panel2.setLayout( null );

     ArrayList columnNames = new ArrayList();
        ArrayList data = new ArrayList();

        //  Connect to an MySQL Database, run query, get result set
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/Autorisations";
        String user = "root";
        String password = "root";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM Autorisations where etat='en cours'");
        {
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();

            //  Get column names
            for (int i = 1; i <= columns; i++)
            {
                columnNames.add( md.getColumnName(i) );
            }

            //  Get row data
            while (rs.next())
            {
                ArrayList row = new ArrayList(columns);

                for (int i = 1; i <= columns; i++)
                {
                    row.add( rs.getObject(i) );
                }

                data.add( row );
            }
        }}
        catch (SQLException e)
        {
            System.out.println( e.getMessage() );
        }

        // Create Vectors and copy over elements from ArrayLists to them
        // Vector is deprecated but I am using them in this example to keep 
        // things simple - the best practice would be to create a custom   defined
        // class which inherits from the AbstractTableModel class
        Vector columnNamesVector = new Vector();
        Vector dataVector = new Vector();

        for (int i = 0; i < data.size(); i++)
        {
            ArrayList subArray = (ArrayList)data.get(i);
            Vector subVector = new Vector();
            for (int j = 0; j < subArray.size(); j++)
            {
                subVector.add(subArray.get(j));
            }
            dataVector.add(subVector);
        }

        for (int i = 0; i < columnNames.size(); i++ )
            columnNamesVector.add(columnNames.get(i));

        //  Create table with database data    
        JTable table = new JTable(dataVector, columnNamesVector)
        {
            public Class getColumnClass(int column)
            {
                for (int row = 0; row < getRowCount(); row++)
                {
                    Object o = getValueAt(row, column);

                    if (o != null)
                    {
                        return o.getClass();
                    }
                }

                return Object.class;
            }
        };

        JScrollPane sp = new JScrollPane( table );
        sp.setBounds(10,25, 800, 1000);
        panel2.add(sp);

}

public void createPage3()
{
    ....

}

public void createPage4()
{
    ....

}

public void createPage5()
{
    ...

}


// Main method to get things started
public static void main( String args[] )
{
    // Create an instance of the test application
    MainClass mainFrame = new MainClass();
    mainFrame.setVisible( true );
}

如果您想知道何时选择了选项卡,则可以使用ChangeListener

tabbedPane.addChangeListener(...);

然后,在事件触发时,您可以获取选项卡式窗格的选定索引并进行处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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