0

I have a simple gui, that is suppose to shows table(JTable). How to make it switch to table1(JTable) after x amount of seconds, and then back to table after x amount of seconds and have that run in a loop. I know setting the other table is nothing more than putting this line in:

scrollPane.setViewportView(table1);

but how to switch from one to the other and back periodically. Guess I have to use the Timer but need some help with this one.thanks

Here is the simple example code:

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

}

THE FINAL RESULT:

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

    }

}

here is the working example of what i wanted.thanks everyone for help

caniaskyouaquestion
  • 657
  • 2
  • 11
  • 21
  • 3
    Look for Swing Timer examples. You could also just swap table models instead of changing the entire table. – BillRobertson42 Jan 29 '15 at 14:39
  • 3
    @Bill I prefer *"..just swap table models instead of changing the entire table."* but the OP might also want to look into [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) (as shown in [this answer](http://stackoverflow.com/a/5786005/418556)) if they want to maintain separate tables. – Andrew Thompson Jan 29 '15 at 14:42
  • @Bill yea i know that, there are few ways i agree...its the functionality of one table showing for lets say 10 seconds and then the other one for 10seconds and loop that. – caniaskyouaquestion Jan 29 '15 at 14:44
  • @AndrewThompson that is ok but in this case i really like the border layout (because it should be an easy app with whats in table that matters) because its nice and centered and auto re-sizes my table's width. – caniaskyouaquestion Jan 29 '15 at 14:45
  • *"i really like the border layout"* You don't have to choose between them! Create a panel with `CardLayout` for the two tables (each having a scroll pane) , then put **that panel** into one of the `BorderLayout` areas (`CENTER`, `PAGE_START` or `PAGE_END` would work best, so extra width can go to the tables). – Andrew Thompson Jan 29 '15 at 14:48
  • @AndrewThompson thanks...that is one way of doing it, that will show both tables at once..right? I though it would look neat if they were switching but need help with timer :) – caniaskyouaquestion Jan 29 '15 at 14:50
  • *"..that will show both tables at once..right?"* No! Since when has that been a requirement? For 'both at once' you might use a single column `GridLayout` (equal size for each table) or a `JSplitPane` (I prefer the split pane, since it allows the user to adjust the space used by each table). – Andrew Thompson Jan 29 '15 at 15:21
  • @AndrewThompson sorry, i was to fast with my assumption. Tried to make it work with cardlayout with in borderlayout.center but in that case the table is not taking the whole width of the main frame. I think i should figure out how to make a timer and just switch tables within the scrollpane when the timers says so....if anyone can help, would be a great help for me. thanks – caniaskyouaquestion Jan 29 '15 at 17:06
  • *"if anyone can help"* I doubt anyone will provide better help until you post an attempt.. – Andrew Thompson Jan 29 '15 at 17:36

0 Answers0