每隔x秒从一个Jtable切换到另一个Jtable

时间:2015-01-29 14:37:11

标签: java swing jtable

我有一个简单的gui,假设显示表(JTable)。如何在x秒后切换到table1(JTable),然后在x秒后返回表并在循环中运行。我知道设置另一个表只不过是把这一行放在:

scrollPane.setViewportView(table1);

但是如何从一个切换到另一个并定期返回。我猜我必须使用Timer但需要一些帮助。谢谢

以下是简单的示例代码:

public class t extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JTable table1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    t frame = new t();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public t() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);
        table = new JTable(10,10);
        table1 = new JTable(10,5);
        scrollPane.setViewportView(table);
    }

}

最终结果:

public class t extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JTable table1;
    private Timer timer;
    private Timer timer1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    t frame = new t();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public t() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);
        table = new JTable(10,10);
        table1 = new JTable(10,5);
        scrollPane.setViewportView(table);

        ActionListener action = new ActionListener()
        {   
            @Override
            public void actionPerformed(ActionEvent event)
            {
               timer.stop();
               scrollPane.setViewportView(table1); 
               timer1.start();
            }
        };

        ActionListener action1 = new ActionListener()
        {   
            @Override
            public void actionPerformed(ActionEvent event)
            {
               timer1.stop();
               scrollPane.setViewportView(table);
               timer.start();

            }
        };

        timer = new Timer(3000, action);

        timer.start();
        timer1 = new Timer(3000, action1);

    }

}

这是我想要的工作示例。谢谢大家的帮助

0 个答案:

没有答案