如何在单击项目选项卡时刷新JTabbedPane选项卡?

时间:2016-06-08 11:37:48

标签: java swing user-interface jpanel jtabbedpane

我目前正在使用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 );
}

1 个答案:

答案 0 :(得分:2)

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

tabbedPane.addChangeListener(...);

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