Question

I am currently learning Swing and am trying to create a simple program that stores information about different sports teams.

I have created multiple tabbed panels which all hold various information about each team. I would like to be able to have a button that when press displays each tabbed panel say every 10 seconds or so - sort of a slide show effect.

I have read up on action listeners but have not spent a lot of time on them as of yet so i am having trouble implementing this. I would be very grateful if anyone could maybe help me or just give me a push in the right direction. I have posted a snippet of code that i have atempted but i am at a loss about what to actually put inside the loop to achieve this.

      slides.addActionListener(new ActionListener()
        public void actionPerformed(ActionEvent actionEvent){
            for(int i = 0; i<arrayList.size(); i++)
             {
              //code that changes the tabbed panels every few seconds.                   
             }
           }
        });
Was it helpful?

Solution

I have created multiple tabbed panels which all hold various various information about each team.

Rather you should focus on creating a JPanel that can display team stats, and not so much JTabbedPanes. The JPanel can then be displayed in a JTabbedPane if desired.

I would use a CardLayout to swap JPanels, and then a Swing Timer to do the swapping. However if you use a single JPanel to display the stats, then you could even display one single JPanel and simply change the model (Team Stats information) that is displayed in it rather than swap JPanels.

As to what to put in your ActionListener, it will not be a for loop at all, but rather a Swing Timer, and you can read about it here: Swing Timer Tutorial.

e.g.,

slides.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent actionEvent){ 
    int timerDelay = 10 * 1000; // 10 seconds
    new Timer(timerDelay, new ActionListener() {
      private int count = 0;
      public void actionPerformed(ActionEvent evt){ 
        if (count < maxCount) {
          // code to show the team data for the count index
          count++;
        } else {
          ((Timer) evt.getSource()).stop(); // stop timer
        }
      }
    }).start();
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top