Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to add the outputs from other class inside a JFrame?
    primarykey
    data
    text
    <p>i want to place my output from other class inside a JFrame heres the code:</p> <p>inside main class</p> <pre><code> import java.util.Arrays; import java.util.*; import javax.swing.JOptionPane; import javax.swing.*; public class Main { public static void main(String[] args){ int choice; int g; String randomNo; Scanner input=new Scanner(System.in); randomNo=JOptionPane.showInputDialog(null,"Enter how many random no:"); g=Integer.parseInt(randomNo); RandomAlgo rand=new RandomAlgo(); int[] data=rand.randomNum(g); JOptionPane.showMessageDialog(null,"Your Random numbers Are : " + Arrays.toString(data)); String randomChoice; randomChoice=JOptionPane.showInputDialog(null, "Choose Sorting algorithm: \n (1)Selection Sort \n (2)Insertion Sort \n (3)Bubble Sort \n (4)Quick Sort" ); choice=Integer.parseInt(randomChoice); switch(choice){ case 1:{ SortingAlgo algo=new SortingAlgo(); data=algo.selectionSort(data); break; } case 2:{ SortingAlgo algo=new SortingAlgo(); data=algo.InsertionSort(data); break; } case 3:{ SortingAlgo algo=new SortingAlgo(); data=algo.bubbleSort(data); break; } case 4:{ SortingAlgo algo=new SortingAlgo(); data=algo.quickSort(data); } } } public Main(){ JFrame frame=new JFrame("Sorted List"); JLabel jdate=new JLabel(""); frame.setSize(300,500); frame.setVisible(true); } } </code></pre> <p>and then here is my SortingAlgo class........................................................</p> <pre><code> import java.util.Arrays; public class SortingAlgo{ public int[] selectionSort(int[] data){ int lenD = data.length; int j = 0; int tmp = 0; for(int i=0;i&lt;lenD;i++){ j = i; for(int k = i;k&lt;lenD;k++){ if(data[j]&gt;data[k]){ j = k; } } tmp = data[i]; System.out.println("\n"+ Arrays.toString(data)); data[i] = data[j]; data[j] = tmp; } return data; } public int[] InsertionSort(int[] data){ int len = data.length; int key = 0; int i = 0; for(int j = 1;j&lt;len;j++){ key = data[j]; i = j-1; while(i&gt;=0 &amp;&amp; data[i]&gt;key){ data[i+1] = data[i]; i = i-1; data[i+1]=key; System.out.println("\n"+ Arrays.toString(data)); } } return data; } public int[] bubbleSort(int[] data){ int lenD = data.length; int tmp = 0; for(int i = 0;i&lt;lenD;i++){ for(int j = (lenD-1);j&gt;=(i+1);j--){ System.out.println("\n"+ Arrays.toString(data)); if(data[j]&lt;data[j-1]){ tmp = data[j]; data[j]=data[j-1]; data[j-1]=tmp; } } System.out.println("\n"+ Arrays.toString(data)); } return data; } public int[] quickSort(int[] data){ int lenD = data.length; int pivot = 0; int ind = lenD/2; int i,j = 0,k = 0; if(lenD&lt;2){ return data; } else{ int[] L = new int[lenD]; int[] R = new int[lenD]; int[] sorted = new int[lenD]; pivot = data[ind]; for(i=0;i&lt;lenD;i++){ if(i!=ind){ if(data[i]&lt;pivot){ L[j] = data[i]; j++; } else{ R[k] = data[i]; k++; } } } int[] sortedL = new int[j]; int[] sortedR = new int[k]; System.arraycopy(L, 0, sortedL, 0, j); System.arraycopy(R, 0, sortedR, 0, k); sortedL = quickSort(sortedL); sortedR = quickSort(sortedR); System.arraycopy(sortedL, 0, sorted, 0, j); sorted[j] = pivot; System.arraycopy(sortedR, 0, sorted, j+1, k); System.out.println("\n"+ Arrays.toString(sorted)); return sorted; } } } </code></pre> <p>as for my random class.................................</p> <pre><code>import java.util.HashSet; import java.util.*; public class RandomAlgo { public int[] randomNum(int g){ Random rand = new Random(); int[] randomNumbers = new int[g]; for (int i = 0; i &lt; g; i++) { int e = rand.nextInt(1000); randomNumbers[i] = e; } return randomNumbers; } } </code></pre> <p>my code is working in console. but im now i want to place the ouput of the sorted list inside a frame. and not on console. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload