4 Replies Latest reply: Apr 18, 2012 3:00 AM by gimbal2 RSS

    Java Swing problems

    931306
      I need to convert a console version of a program into a fully working Swing version and then add it to colleagues code as a complete program. I've never worked with swing before and I'm lost. I can't figure out how to print out my variables and how to store text as a variable. Also, in the console version, the first keyboard.input on each section didn't work until I duplicated it. No idea why that is
      import java.util.*;
      
      
      /**
       * Created by IntelliJ IDEA.
       * User: Adam
       * Date: 26/03/12
       * Time: 02:42
       * Comment: Music Review System with a log in system which differentiates users and uploaders
       */
      public class MusicReviewSystem
      
      {   static Scanner keyboard = new Scanner(System.in);
          static String delete1;
          static private String password;
          static String usernameUser="Adam", usernameArtist="Kurt";
          static String lastComment="This album is very enjoyable, up beat and quirky";
          static int lastReview=5;
          static int noOfUploads=0;
      
          static String [] genrePop = {"Pop","Pop"};//parallel array for the Pop category
          static String [] artistPop = {"Katy Perry", "null"};//parallel array for the Pop category
          static String [] albumPop = {"Part of Me", "null"};//parallel array for the Pop category
          static int [] yearPop = {2012, 0};//parallel array for the Pop category
          static int popCount=0;//adam
      
          static String [] genreIndie = {"Indie","Indie"};//parallel array for the Indie category
          static String [] artistIndie = {"LCD Soundsystem", "null"};//parallel array for the Indie category
          static String [] albumIndie = {"This Is Happening", "null"};//parallel array for the Indie category
          static int [] yearIndie = {2010, 0};//parallel array for the Indie category
          static int indieCount=0;//adam
      
          static String [] genreDance = {"Dance","Dance"};//parallel array for the Dance category
          static String [] artistDance = {"Calvin Harris", "null"};//parallel array for the Dance category
          static String [] albumDance = {"I Created Disco", "null"};//parallel array for the Dance category
          static int [] yearDance = {2007, 0};//parallel array for the Dance category
          static int danceCount=0;//adam
      
          static String [] genreRap = {"Rap","Rap"};//parallel array for the Rap category
          static String [] artistRap = {"JAY Z & Kanye West", "null"};//parallel array for the Rap category
          static String [] albumRap = {"Watch The Throne", "null"};//parallel array for the Rap category
          static int [] yearRap = {2011, 0};//parallel array for the Rap category
          static int rapCount=0;//adam
      
          static String [] genreFolk = {"Folk","Folk"};//parallel array for the Folk category
          static String [] artistFolk = {"Noah & the Whale", "null"};//parallel array for the Folk category
          static String [] albumFolk ={"Last Night On Earth", "null"};//parallel array for the Folk category
          static int [] yearFolk = {2011, 0};//parallel array for the Folk category
          static int folkCount=0;//adam
      
          public  static void userProfile ()//adam
          {
              System.out.println("Welcome "+usernameUser);
              System.out.println("The last comment you made was ''"+lastComment+".''");
              System.out.println("You gave your last review a "+lastReview+" star rating\n\n");
              mainMenu();
              //menu system
          }//UserProfile
      
          public static void artistProfile()//adam
          {
              int artistMenu;
              System.out.println("Welcome "+usernameArtist);
              System.out.println("You have uploaded "+noOfUploads+" albums for review.\n\n");
      
              do{
                  System.out.println("Please enter your selection from the following options:");
                  System.out.println("1. Add a Pop album");
                  System.out.println("2. Add an Indie album");
                  System.out.println("3. Add a Dance album");
                  System.out.println("4. Add a Rap album");
                  System.out.println("5. Add a Folk album");
                  System.out.println("6. Moderate previously uploaded albums");
                  System.out.println("7. Return to the main menu\n");
                  System.out.print("Please enter your choice: ");
                  artistMenu=keyboard.nextInt();
                  System.out.print("\n\n");
                  switch(artistMenu)
                  {
                      case 1:
                          addPop();
                          break;
                      case 2:
                          addIndie();
                          break;
                      case 3:
                          addDance();
                          break;
                      case 4:
                          addRap();
                          break;
                      case 5:
                          addFolk();
                          break;
                      case 6:
                          deleteComment();
                          break;
                      case 7:
                          mainMenu();
                      default: System.out.println("This is not a valid menu option. Please re-enter.");
                  }//switch
              }//while
              while(artistMenu!=1 && artistMenu!=2 && artistMenu!=3 && artistMenu!=4 && artistMenu!=5 && artistMenu!=6 && artistMenu!=7);
          }//ArtistProfile
      
          public static void addPop()//adam
          {
              do
              {
                  if (popCount!=0)
                      popCount++;
                  else
                  {
                      System.out.print("Please enter the artist: ");
                      artistPop[popCount]= keyboard.nextLine();
                      artistPop[popCount]= keyboard.nextLine();//workaround as original did not work
                      System.out.print("Please enter the album name: ");
                      albumPop[popCount]= keyboard.nextLine();
                      System.out.print("Please enter the year of release: ");
                      yearPop[popCount]= keyboard.nextInt();
                      noOfUploads++;
                      mainMenu();
                  }
              }//while
              while (popCount<3);
      
          }//Add Pop
      
          public static void addIndie()//adam
          {
              do
              {
                  if (indieCount!=0)
                      indieCount++;
                  else
                  {
                      System.out.print("Please enter the artist: ");
                      artistIndie[indieCount]= keyboard.nextLine();
                      artistIndie[indieCount]= keyboard.nextLine();//workaround as original did not work
                      System.out.print("Please enter the album name: ");
                      albumIndie[indieCount]= keyboard.nextLine();
                      System.out.print("Please enter the year of release: ");
                      yearIndie[indieCount]= keyboard.nextInt();
                      noOfUploads++;
                      mainMenu();
                  }
              }//while
              while (indieCount<3);
          }//Add Indie
      
          public static void addDance()//adam
          {
              do
              {
                  if (danceCount!=0)
                      danceCount++;
                  else
                  {
                      System.out.print("Please enter the artist: ");
                      artistDance[danceCount]= keyboard.nextLine();
                      artistDance[danceCount]= keyboard.nextLine();//workaround as original did not work
                      System.out.print("Please enter the album name: ");
                      albumDance[danceCount]= keyboard.nextLine();
                      System.out.print("Please enter the year of release: ");
                      yearDance[danceCount]= keyboard.nextInt();
                      noOfUploads++;
                      mainMenu();
                  }
              }//while
              while (danceCount<3);
          }//Add Dance
      
          public static void addRap()//adam
          {
              do
              {
                  if (rapCount!=0)
                      rapCount++;
                  else
                  {
                      System.out.print("Please enter the artist: ");
                      artistRap[rapCount]= keyboard.nextLine();
                      artistRap[rapCount]= keyboard.nextLine();//workaround as original did not work
                      System.out.print("Please enter the album name: ");
                      albumRap[rapCount]= keyboard.nextLine();
                      System.out.print("Please enter the year of release: ");
                      yearRap[rapCount]= keyboard.nextInt();
                      noOfUploads++;
                      mainMenu();
                  }//else
              }//while
              while (rapCount<3);
          }//Add Rap
      
          public static void addFolk()//adam
          {
              do
              {
                  if (folkCount!=0)
                     folkCount++;
                  else
                  {
                      System.out.print("Please enter the artist: ");
                      artistFolk[folkCount]= keyboard.nextLine();
                      artistFolk[folkCount]= keyboard.nextLine();//workaround as original did not work
                      System.out.print("Please enter the album name: ");
                      albumFolk[folkCount]= keyboard.nextLine();
                      System.out.print("Please enter the year of release: ");
                      yearFolk[folkCount]= keyboard.nextInt();
                      noOfUploads++;
                      mainMenu();
                  }//else
              }//while
              while (folkCount<3);
          }//Add Folk
      
      
      
          public static void deleteComment()//adam
          {
              //search for previously uploaded albums?
              //return albums based on number of uploads.
              //print comments on album
              //show a delete option
              //run the menu
          }//Delete Comment
      
          public static void mainMenu()//adam
          {
              Scanner keyboard = new Scanner(System.in);
              int mainMenu;
      
              do{
                  System.out.println("Please enter your selection from the following options:");
                  System.out.println("1. Log in/Register//void");
                  System.out.println("2. View User profile");
                  System.out.println("3. View Artist Profile");
                  System.out.println("4. View all albums");
                  System.out.println("5. Search for albums");
                  System.out.println("6. Exit the system\n");
                  System.out.print("Please enter your choice: ");
                  mainMenu=keyboard.nextInt();
                  System.out.print("\n\n");
                  switch(mainMenu)
                  {
                      case 1:
                          mainMenu();//update for full program
                          break;
                      case 2:
                          userProfile();
                          break;
                      case 3:
                          artistProfile();
                          break;
                      case 4:
                          mainMenu();//update for full program
                          break;
                      case 5:
                          mainMenu(); //update for full program
                          break;
                      case 6:
                          System.exit(0);
                          break;
                      default: System.out.println("This is not a valid menu option. Please re-enter.");
                  }//switch
              }//while
              while(mainMenu!=1 && mainMenu!=2 && mainMenu!=3 && mainMenu!=4 && mainMenu!=5);
      
          }//Main Menu
      
      
      
          public static void main(String[] args)
          {
              mainMenu();
          }//main
      }//class
      Any help would be greatly appreciated. Even if someone could point me in the right direction. Thank you
        • 1. Re: Java Swing problems
          798692
          928303 wrote:
          I've never worked with swing before and I'm lost.
          Google it something like 'java swing tutorials for beginners'.
          I can't figure out how to print out my variables and how to store text as a variable. Also, in the console version, the first keyboard.input on each section didn't work until I duplicated it. No idea why that is
          If you go thro' some of the basics, you will come to know.
          Any help would be greatly appreciated. Even if someone could point me in the right direction. Thank you
          I did pointed out in a best direction. There is no way other than learning swing by yourself. No one will work for you freely.
          • 2. Re: Java Swing problems
            931306
            cheers mucker
            • 3. Re: Java Swing problems
              931373
              ok, First of all, you can have JTextFields on your frame(Swing), and store it's text in a String variable using getText method :

              String textVariable ;
              JTextField textfield ;
              .
              .
              .
              .
              textVariable = textField.getText() ;

              and the reason your keyboard.nextLine(); won't work is that user hits enter when he wants to input a number in the first menu.
              the first keyboard.nextLine(); consumes that enter key.

              if you want to avoid duplicated keyboard.nextLine(); , you can simply do this on your main menu:

              instead of "artistMenu=keyboard.nextInt();"
              use :
              String command = keyboard.nextLine();
              artistMenu = Integer.parseInt(command);

              Edited by: 928370 on Apr 17, 2012 10:49 PM
              By the way, if you want a good swing tutorial , you can try this : http://zetcode.com/tutorials/javaswingtutorial/
              • 4. Re: Java Swing problems
                gimbal2
                928303 wrote:
                cheers for your wise words, I am a mucker
                Fixed that for you