0

So I've managed to make the program work but my professor asks for it to be done in GUI format. The issue is, I've tried to make whatever I have work with a GUI supplement but I'm fairly new with how this works. I've looked through many sites and questions on here but I'm not too familiar with how this works. I'm worried I'll mess with my code so I just want to have an idea of how I can implement the GUI into this.

I've tried to make it so that some of the text do appear in a Pane but I don't know how to incorporate with GUI when inputting -1 as an indication that the inputs are in as well as using this concept on string course = keyboard.next();


In short, I'm just at a loss with this.

Some important things to look at [The Code without the mess I made with GUI]

import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;

public static void main (String [] args)
{

    HashMap <Integer, ArrayList <String>> students = new HashMap <>();

    System.out.println ("This program correlates ID with student courses\n");
    int currentID;
    boolean editing;

    while (true) {
        System.out.println ("Please enter Student ID: \n(Then enter a -1 when inputs are done)");
        Scanner keyboard = new Scanner (System.in);
        int ID = keyboard.nextInt();

        if(ID == -1) {
            break;
        }

        students.put (currentID = ID, new ArrayList<>());
        editing = true;

        System.out.println("Enter courses: \n(Once you're done, type \'next\')");

        while (editing) {
            String course = keyboard.next();
            if(course.equalsIgnoreCase("next")) {
                editing = false;
            } else {
                students.get(currentID).add(course);
            }
        }
    }

    System.out.println ("Final List: ");
    students.forEach((key, value) -> {
        System.out.println("Student " + key + ". Courses: " + Arrays.toString(value.toArray()));
    });
}

The Program information that's expected: enter image description here

  • I would start with [Create a GUI with Swing](http://docs.oracle.com/javase/tutorial/uiswing/). GUIs and consoles programs work through a different paradigm, so you're not going to be able to covert your current program to a GUI, but rather convert the functionality/requirements – MadProgrammer May 13 '17 at 01:19
  • Yeah I figured that would be the case when looking through this. Thank you for that hyperlink though, I appreciate it! :) Sorry, I'm a bit new with this – R. Ahmed May 13 '17 at 01:24
  • A simple example is examined [here](http://stackoverflow.com/a/24726834/230513). – trashgod May 13 '17 at 02:42
  • " I'm worried I'll mess with my code" : No need to worry. A. always keep a backup before you make major changes (or use your IDE history or a GIT solution). B. I would suggest to make a new small program, just to learn how to get user's input. Later see how you integrate the two programs. – c0der May 13 '17 at 07:43

1 Answers1

0

The SWING API is very large and it can be difficult to know where to start if trying to write GUI code by hand without having experience in it. I suggest you download NetBeans, put your code into a NetBeans project, and then use the built-in Swing designer to design the GUI graphically and generate the code.

You will find that when you double-click on a control in the designer, it will generate a callback method you can just fill in to respond to the user's input events.

Michael Warner
  • 464
  • 1
  • 5
  • 13