0

I'm new to Swing and I'm trying to figure something out. I'm having difficulty explaining what I'm trying to do and I can't find anything online so I wrote a program demonstrating what I want to have happen. I use a CardLayout here:

import java.awt.event.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class Test extends JFrame implements ActionListener {
  private CardLayout crd;
  private int currentCard;

  // Buttons
  private JButton twoToCardOne;
  private JButton threeToCardOne;
  private JButton toCardTwo;
  private JButton toCardThree;

  // Container
  private Container cPane;

  // Panels
  private JPanel cardOne;
  private JPanel cardTwo;
  private JPanel cardThree;

  public Test() {
    currentCard = 1;
    cPane = getContentPane();
    crd = new CardLayout();
    cPane.setLayout(crd);

    // Create buttons
    twoToCardOne = new JButton("To Card One");
    threeToCardOne = new JButton("To Card One");
    toCardTwo = new JButton("To Card Two");
    toCardThree = new JButton("To Card Three");
    twoToCardOne.addActionListener(this);
    threeToCardOne.addActionListener(this);
    toCardTwo.addActionListener(this);
    toCardThree.addActionListener(this);

    // Create Panel (Card One)
    cardOne = new JPanel();
    cardOne.add(toCardTwo);
    cardOne.add(toCardThree);

    // Create Panel (Card Two)
    cardTwo = new JPanel();
    cardTwo.add(twoToCardOne);

    // Create Panel (Card Three)
    cardThree = new JPanel();
    cardThree.add(threeToCardOne);

    // Add Panels
    cPane.add(cardOne);
    cPane.add(cardTwo);
    cPane.add(cardThree);
  }

  @Override
  public void actionPerformed(ActionEvent e){
    int target = 0;
    if (e.getSource() == toCardThree){target = 3;}
    else if (e.getSource() == toCardTwo){target = 2;}
    else {target = 1;}

    while (currentCard != target){
      crd.next(cPane);
      if (currentCard < 3){++currentCard;}
      else {currentCard = 1;}
    }
  }

  public static void main(String[] args){
    Test test = new Test();
    test.setSize(800, 500);
    test.setVisible(true);
    test.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
}

So, what I have here are buttons on various panels that all connect to the CardLayout. So, there are different buttons on the different panels that will move me panels when clicked on.

I have two questions/worries here:

  1. If I wanted to make each JPanel its own class, how would I make this work? Since the buttons will be in a different class, I would no longer be able to control the CardLayout with actionPerformed.

  2. My plan is to make this a hockey program where clicking on a button will take you to the player's profile with their stats and my plan was to have each individual player be a panel. This means that I will have A LOT of panels and buttons and I was wondering if there would be a better way to go about doing this. The only thing I've found online is to use CardLayout but this seems like it would not be a very optimal way of doing this.

Any help is much appreciated!

Note: Again, I'm very new to Swing so apologies if what I'm trying to explain is confusing or if I got something completely wrong.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • First and foremost, tease out the program logic from the GUI aspect of the code via M-C-V or something similar. Do this right and everything should fall into place. – Hovercraft Full Of Eels Jul 03 '22 at 00:44
  • If you make a custom JPanel, it is a subclass of JPanel, so you can still treat it as a JPanel. eg `JPanel firstCard = new MyCustomJPanel()`. – Dmytro Jul 03 '22 at 00:46
  • *my plan was to have each individual player be a panel.* - generally I would suggest you have a JList or JTable to list all the players. Then when you select a player you just update the stats of that player on a common panel. So your panel would need a method like "refereshPlayerStats(player)". – camickr Jul 03 '22 at 00:47
  • Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. @Hovercraft Full Of Eels has the right idea. Create an application model for teams and players using two plain Java getter/setter classes. Your GUI should be one main `JPanel` to show the teams and one `JDialog` to show the player information. – Gilbert Le Blanc Jul 03 '22 at 00:49
  • For [example](https://stackoverflow.com/questions/36971360/button-in-cardlayout-not-working/36972129#36972129); [example](https://stackoverflow.com/questions/31575926/how-to-completely-get-rid-of-a-jpanel-and-everything-in-it/31576472#31576472); [example](https://stackoverflow.com/questions/23352226/singleton-with-cardlayout-wont-show-card-when-another-class-calls-singleton-ins/23352348#23352348); [example](https://stackoverflow.com/questions/24296505/how-to-effectively-use-cardlayout-in-java-in-order-to-switch-from-panel-using-bu/24296872#24296872) – MadProgrammer Jul 03 '22 at 05:11

0 Answers0