Help inserting contents of a string array into a JPanel

Working on a project for school and have it a road block, being spring break all the normal avenues for help and currently cutoff. SI's probably in Tijuana or something.

Anyway, project is a simple game. At the begining I ask for player information and prompt for more players.

I have setup in my GUI using border layout and intend to place the player info in the north.
I'm thinking I need to use a JTextarea for this maybe.

heres a snipet of the code I am working with so far.

                  super();
                  do
                  {
                        String playerName = JOptionPane.showInputDialog("Please Enter the Players Name.");
                        String age = JOptionPane.showInputDialog("Enter the Players Age.");
                        int playerAge = Integer.parseInt(age);
                        int playerScore = 0;
                        // Enter Player into Player Array
                        playerArray [lastPlayer] = new Players ( playerName, playerAge, playerScore );
                        prompt = JOptionPane.showConfirmDialog(null,"Add another Player?");
                  } while( prompt == JOptionPane.YES_OPTION );

I am at a loss for how to get it placed into the north field.
reefcrazedAsked:
Who is Participating?
 
CEHJCommented:
Or

textArea.append(playerArray [lastPlayer].toString() + "\n");
0
 
CEHJCommented:
Provide a toString method for your Players class and then

textArea.append(playerArray [lastPlayer].toString());
0
 
reefcrazedAuthor Commented:
I'll give that a try thanks.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
reefcrazedAuthor Commented:
Still having a bit of trouble. Rather than a snippet, here is the code.
I do have a toString made in the players class.
I made a JTextArea at the top.
I do have other issues but this one is the most pressing atm.

      public class GameGUI extends JFrame implements ActionListener
      {
            JPanel imagePanel;                                    // Panel for the images on buttons.
            JTextArea playerInfo;                              // JTextArea for Player Information.
            JButton [] imageButton;                              // Array of buttons to hold images.
            ImageIcon []imageOnButton;                        // Array of imageIcons.
            Players [] playerArray = new Players [10];// Array for Player information.
            ImageIcon imageNull;                              // Null image for blank button.
            int status =1;                                          // Flag for when button selected.
            int indexOfFirst = 0;                              // Temp index for image comparison.
            int lastPlayer = 0;
            int prompt;
                        
            // Array of image file names to be used to create imageIcons to be shown on
            // the buttons.
            String [] images = {"apple.gif", "ball.gif", "bannana.gif", "bear.gif", "bird.gif", "bug.gif",
                        "butterfly.gif", "cake.gif", "cat.gif", "dog.gif", "doll.gif", "dragon.gif", "duck.gif",
                        "elephant.gif", "fish.gif", "flower.gif", "frog.gif", "gift.gif", "hat.gif", "hotdog.gif",
                        "jackinthebox.gif", "keys.gif", "pizza.gif", "plane.gif", "rabbit.gif", "rocket.gif",
                        "rockinghorse.gif", "shoe.gif", "skateboard.gif", "snail.gif", "squirrel.gif", "sun.gif",
                        "apple.gif", "ball.gif", "bannana.gif", "bear.gif", "bird.gif", "bug.gif",
                        "butterfly.gif", "cake.gif", "cat.gif", "dog.gif", "doll.gif", "dragon.gif", "duck.gif",
                        "elephant.gif", "fish.gif", "flower.gif", "frog.gif", "gift.gif", "hat.gif", "hotdog.gif",
                        "jackinthebox.gif", "keys.gif", "pizza.gif", "plane.gif", "rabbit.gif", "rocket.gif",
                        "rockinghorse.gif", "shoe.gif", "skateboard.gif", "snail.gif", "squirrel.gif", "sun.gif"};
            Container container;
            public GameGUI()
            {      
                  super();
                  do
                  {
                        String playerName = JOptionPane.showInputDialog("Please Enter the Players Name.");
                        String age = JOptionPane.showInputDialog("Enter the Players Age.");
                        int playerAge = Integer.parseInt(age);
                        int playerScore = 0;
                        // Enter Player into Player Array
                        playerArray [lastPlayer] = new Players ( playerName, playerAge, playerScore );
                        //playerInfo.append(playerArray [lastPlayer].toString() + "\n");
                        prompt = JOptionPane.showConfirmDialog(null,"Add another Player?");
                  } while( prompt == JOptionPane.YES_OPTION );
                  
                  container = getContentPane();
                  JPanel southLayout = new JPanel();
                  JPanel northLayout = new JPanel();
                  JPanel border = new JPanel(new BorderLayout ());
                  southLayout.setLayout(new FlowLayout());
                  southLayout.add(new JButton("Start New Game"));
                  southLayout.add(new JButton("Exit"));
                  
                  playerInfo = new JTextArea(100,100);
                  northLayout.setLayout (new FlowLayout());
                  northLayout.add( new JScrollPane());
                        
                  JPanel imageGrid = new JPanel();
                  imageGrid.setLayout(new GridLayout(8,8,3,3));
                  imageButton = new JButton[64];
                  imageOnButton = new ImageIcon[64];
                        
                  for (int i =0; i < images.length; i++)              
                  {
                        imageButton[i] = new JButton(""+i);             // Create the array of buttons.
                        imageButton[i].addActionListener(this);                  // Add the listener for button clicks.
                        imageGrid.add(imageButton[i]);                              // Add the buttons to the panel.
                        imageOnButton[i] = new ImageIcon(images[i]);      // Create the array of imageIcons.
                        scramble (imageButton);
                  }      
                  
                  border.add(northLayout,BorderLayout.NORTH);
                  border.add(imageGrid,BorderLayout.CENTER);
                  border.add(southLayout,BorderLayout.SOUTH);
                        
                  imageNull = new ImageIcon("");
                  container.add(border);
                  setSize(800,800);
                  setVisible(true);
            }
            public void actionPerformed(ActionEvent event)
            {
                  for (int i = 0; i < images.length; i++)
                  {
                        if ((event.getSource()== imageButton[i])&& status==1)
                        {
                              imageButton[i].setIcon(imageOnButton[i]);
                              indexOfFirst = i;
                              status = 2;
                        }
                        else if ((event.getSource()== imageButton[i])&& status == 2)
                        {
                              imageButton[i].setIcon(imageOnButton[i]);
                        System.out.println(" "+i+" "+images[i]+" "+indexOfFirst+" "+ images[indexOfFirst]);
                              if(images[i]==(images[indexOfFirst]))
                              {
                                    JOptionPane.showMessageDialog(null,"Match");
                                    // Add point to player
                              }
                              else
                              {
                                    JOptionPane.showMessageDialog(null,"Try Again");
                                    imageButton[i].setIcon(imageNull);
                                    imageButton[indexOfFirst].setIcon(imageNull);
                                    // Switch to next player
                              }
                              status = 1;
                        }
                  }      
            }// End actionPerformed.
            public static void scramble(JButton array[])
             {
                for (int i = 0; i < array.length; i++)
                {
                   swap(array, (int)(Math.random()* 64), (int)(Math.random()* 64));
                }
             }
            public static void swap(JButton array[], int firstElement, int secondElement)
             {
                JButton temp = array[firstElement];
                array[firstElement] = array[secondElement];
                array[secondElement] = temp;
             }
      public static void main(String[] args)
      {      
            GameGUI application = new GameGUI();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }//End main.
}// End JFrame.
0
 
CEHJCommented:
You'd be better to build your gui first and then add Players to it
0
 
reefcrazedAuthor Commented:
Rather than prompt at the beginning?
0
 
CEHJCommented:
Yes
0
 
reefcrazedAuthor Commented:
Moved the code for the players and removed northLayout.setLayout (new FlowLayout());

I now have something there to work with, thanks!
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.