【问题标题】:How do I refresh a JTabbedPane tab when the item tab has been clicked?单击项目选项卡后,如何刷新 JTabbedPane 选项卡?
【发布时间】:2016-06-08 11:37:48
【问题描述】:

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

【问题讨论】:

  • 刷新是什么意思?
  • 在第一个选项卡中,我执行了一些 SQL 查询,在第二个选项卡中我有一个 Jtable,我希望在单击选项卡项时更新它
  • 你必须执行并再次填写
  • 如需尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。硬编码一些数据来替换数据库。
  • @Andrew Thompson 我发布了代码

标签: java swing user-interface jpanel jtabbedpane


【解决方案1】:

如果您想知道标签何时被选中,您可以使用ChangeListener

tabbedPane.addChangeListener(...);

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

【讨论】:

    猜你喜欢
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多