?
Solved

Help inserting contents of a string array into a JPanel

Posted on 2006-03-21
9
Medium Priority
?
308 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:reefcrazed
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16248804
Provide a toString method for your Players class and then

textArea.append(playerArray [lastPlayer].toString());
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 16248812
Or

textArea.append(playerArray [lastPlayer].toString() + "\n");
0
 

Author Comment

by:reefcrazed
ID: 16248821
I'll give that a try thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:reefcrazed
ID: 16249193
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16249218
You'd be better to build your gui first and then add Players to it
0
 

Author Comment

by:reefcrazed
ID: 16249284
Rather than prompt at the beginning?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16249298
Yes
0
 

Author Comment

by:reefcrazed
ID: 16249392
Moved the code for the players and removed northLayout.setLayout (new FlowLayout());

I now have something there to work with, thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16249425
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question