java - javaFx radio button and textfield user selection and user input -
so question trying implement gui has 4 radio buttons let user choose type of sorting want do(quick,insertion,bubble,selection) , can pick 3 more radio buttons , choose either sorted, random, reverse sorted. has textfield allows them chose input size of array , block size. after user has chosen radio buttons , put in information input size text field , block size text field, hit go , program sort array , output sorted array console.
so need implementing action event or listener go button information radio buttons , text fields. understand logic behind wasn't sure how link event handler/action event. code far compiles , when hit 'go' button prints array of 100 numbers sorted in order 1-100. doesn't take account user selects on radio buttons or text field's block size , input size , need that. here code:
package project4practice; import java.util.random; import javafx.application.application; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.control.alert; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.radiobutton; import javafx.scene.control.textfield; import javafx.scene.control.togglegroup; import javafx.scene.layout.borderpane; import javafx.scene.layout.gridpane; import javafx.stage.stage; public class project4practice extends application { @override public void start(stage primarystage) { borderpane rootpane = new borderpane(); gridpane gp = new gridpane(); rootpane.setcenter(gp); gp.setvgap(5); gp.sethgap(5); rootpane.prefwidth(700); rootpane.prefheight(400); gp.prefwidth(400); gp.prefheight(400); label sort = new label(" sorting algorithm "); radiobutton selection = new radiobutton("selection "); radiobutton bubble = new radiobutton("bubble "); radiobutton insertion = new radiobutton("insertion"); radiobutton quick = new radiobutton("quick "); label inputtype = new label(" input type "); radiobutton sorted = new radiobutton("already sorted "); radiobutton reverse = new radiobutton("reverse "); radiobutton random = new radiobutton("random "); label inputsize = new label(" input size: "); textfield inputtext = new textfield(); inputtext.setonaction((actionevent inputtext1) -> { string inputtext2 = inputtext.gettext(); double inputtext3 = double.parsedouble(inputtext2); system.out.println(inputtext3); }); label blocksize = new label(" block size: "); textfield block = new textfield(); block.setonaction((actionevent block1) -> { string block2 = block.gettext(); double block3 = double.parsedouble(block2); system.out.println(block3); }); button go = new button("go "); togglegroup tg = new togglegroup(); selection.settogglegroup(tg); selection.setselected(true); bubble.settogglegroup(tg); insertion.settogglegroup(tg); quick.settogglegroup(tg); togglegroup tg1 = new togglegroup(); sorted.settogglegroup(tg1); sorted.setselected(true); reverse.settogglegroup(tg1); random.settogglegroup(tg1); gp.add(sort, 0, 0); gp.add(selection, 0, 1); gp.add(bubble, 0, 2); gp.add(insertion, 0, 3); gp.add(quick, 0, 4); gp.add(inputtype, 0, 7); gp.add(sorted, 0, 8); gp.add(reverse, 0, 9); gp.add(random, 0, 10); gp.add(inputsize, 0, 12); gp.add(inputtext, 1, 12); gp.add(blocksize, 0, 13); gp.add(block, 1, 13); gp.add(go, 0, 16); go.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent go1) { //selection sorted if (selection.isselected() && sorted.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int chunk = integer.parseint(block.gettext());//block size user input // for(int i=0;i<block.length;i+=chunk){ // system.out.println(arrays.tostring(arrays.copyofrange(block, i, math.min(block.length,i+chunk)))); // } int[] array = getsorted(arraysize, true); print(array); selectionsort(array); }//selction sorted reverse else if (selection.isselected() && reverse.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = null; array = getreverse(array); print(array); selectionsort(array); } //selection sorted random else if (selection.isselected() && random.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = getrandom(arraysize); print(array); selectionsort(array); }//quick sort random else if (quick.isselected() && random.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = getrandom(arraysize); print(array); quicksort(array, 0, array.length - 1); }//quick sort sorted else if (quick.isselected() && sorted.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = getsorted(arraysize, true); print(array); quicksort(array, 0, array.length - 1); }//quick reverse sort else if (quick.isselected() && reverse.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = null; array = getreverse(array); print(array); quicksort(array, 0, array.length - 1); }//insertion sorted sort else if (insertion.isselected() && sorted.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = getsorted(arraysize, true); print(array); insertionsort(array); }//insertion random sort else if (insertion.isselected() && random.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = getrandom(arraysize); print(array); insertionsort(array); }//insertion reverse else if (insertion.isselected() && reverse.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = null; array = getreverse(array); print(array); insertionsort(array); }//bubble sort else if (bubble.isselected() && sorted.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = getsorted(arraysize, true); print(array); bubblesort(array); }//bubble random sort else if (bubble.isselected() && random.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = getrandom(arraysize); print(array); bubblesort(array); }//bubble reverse sort else if (bubble.isselected() && reverse.isselected()) { int arraysize = integer.parseint(inputtext.gettext()); int[] array = null; array = getreverse(array); print(array); bubblesort(array); } alert alert = new alert(alert.alerttype.information); alert.settitle("thread sorted!"); alert.setheadertext("finished"); alert.setcontenttext("sort completed in milliseconds "); alert.showandwait(); } }); scene scene = new scene(rootpane, 500, 350); primarystage.settitle("project 4"); primarystage.setscene(scene); primarystage.show(); } //insertion sort public static void insertionsort(int array[]) { // int loopcount = 0; int n = array.length; (int j = 1; j < n; j++) { int key = array[j]; int = j - 1; while ((i > -1) && (array[i] > key)) { array[i + 1] = array[i]; i--; } array[i + 1] = key; } //return loopcount; } //quick sort int partition(int arr[], int left, int right) { int = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr[i] < pivot) { i++; } while (arr[j] > pivot) { j--; } if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } return i; } //quick sort public void quicksort(int arr[], int left, int right) { int index = partition(arr, left, right); if (left < index - 1) { quicksort(arr, left, index - 1); } if (index < right) { quicksort(arr, index, right); } //return index; } //bubble sort public static void bubblesort(int[] arr) { int n = arr.length; // int loopcount = 0; int temp = 0; (int = 0; < n; i++) { (int j = 1; j < (n - i); j++) { if (arr[j - 1] > arr[j]) { temp = arr[j - 1]; arr[j - 1] = arr[j]; arr[j] = temp; } } } //return loopcount; } //selection sort public static void selectionsort(int[] arr) { (int = 0; < arr.length - 1; i++) { int index = i; (int j = + 1; j < arr.length; j++) { if (arr[j] < arr[index]) { index = j; } } int smallernumber = arr[index]; arr[index] = arr[i]; arr[i] = smallernumber; } } public static int[] getrandom(int size) { random rand = new random(); int[] array = new int[size]; (int = 1; <= size; i++) { array[i - 1] = math.abs(rand.nextint()) % 100; } return array; } public static int[] getsorted(int size, boolean accending) { int[] array = new int[size]; if (accending) { (int = 1; <= size; i++) { array[i - 1] = i; } } else { (int = size; > 0; i--) { array[size - i] = i; } } return array; } public static int[] getreverse(int[] arrayw) { int[] array = new int[arrayw.length]; (int = 0,j = array.length-1; i<array.length;i++,j--) { array[j] = arrayw[i]; } return array; } public static void print(int[] array) { (int = 0; < array.length; i++) { system.out.print(" " + array[i]); } system.out.println(); } /** * @param args command line arguments */ public static void main(string[] args) { launch(args); } }
your potential radio options are:
"selection, sorted":"selection,reverse":...:"quick, random"
you need handle of different cases have have between 2 togglegroups.
if(selection.isselected() && sorted.isselected()){ int arraysize = integer.parseint(inputtext.gettext()); int[] array = getsorted(arraysize,true); print(array); int loopcount = selectionsort(array); } else if(selection.isselected() && reverse.isselected() { //do } else if(...) { } . . . else if(quick.isselected() && random.isselected()) { //do }
added due updated comment
now print method should like:
public static void print(int[] array, int blocksize) { (int = 0; < array.length; i++) { system.out.print(" " + array[i]); if((i + 1) % blocksize == 0) { system.out.println(); } } system.out.println(); }
and old call print methods change from:
print(array);
to:
print(array, integer.parseint(block.gettext()));
Comments
Post a Comment