Nhey Perez
Nhey Perez

Reputation: 3

how to add the outputs from other class inside a JFrame?

i want to place my output from other class inside a JFrame heres the code:

inside main class

 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);
  }


}

and then here is my SortingAlgo class........................................................

  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<lenD;i++){
      j = i;
      for(int k = i;k<lenD;k++){
        if(data[j]>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<len;j++){
    key = data[j];

    i = j-1;

    while(i>=0 && data[i]>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<lenD;i++){

    for(int j = (lenD-1);j>=(i+1);j--){
      System.out.println("\n"+ Arrays.toString(data));
      if(data[j]<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<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<lenD;i++){
        if(i!=ind){
          if(data[i]<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;
    }
  }
}

as for my random class.................................

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 < g; i++) {
            int e = rand.nextInt(1000);
            randomNumbers[i] = e;

        }

        return randomNumbers;
    }

  }

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.

Upvotes: 0

Views: 347

Answers (3)

bsorrentino
bsorrentino

Reputation: 1641

Another solution could be redirect standard output (i.e. System.out) into a text area within JFrame

It is enough develop a Custom PrintStream and call

System.setOut( new MyCustomPrintStream() );

To have further details "How to redirect console content to a textArea in java?"

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347264

Start with a JFrame.

Create a JPanel and add a bunch of JRadioButtons to it. These will act as you choices, Selection Sort, Insertion Sort, Bubble Sort, Quick Sort. Add each of these to a ButtonGroup so that only button can be selected.

Add a JTextField to this panel, this will act as "random number length" value (you could also use a JSpinner, but let's not complicate things.

Add a JButton to the panel. This will act as you "Sort" button, which will take the input and actually do the sorting. Add an ActionListener to this button

Add this panel to the frame's BorderLayout.WEST position...

Create another JPanel, using a GridLayout.

To this, I would add two JLists, each inside their own JScrollPanes. These will act as the unsorted and sorted lists.

Add this panel to the BorderLayout.CENTER position of the frame.

When the user clicks the button, you will need to determine which JRadioButton is selected, get the text from the JTextField, convert this to an int (Integer.parseInt(text)).

Generate the list of random numbers, add these to the first JList, perform your sort, then add the result to the second JList.

Take a read through...

For more details...

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Regarding,

System.out.println("\n"+ Arrays.toString(data));
**//is this good? that i placed print here so i can show the ouput to the each sorting steps?**

No, that won't work well. println prints to the console not to a GUI. Your selectionSort() method returns an array of int already -- so why not simply use that? I'd get the array results from your selectionSort method and in the GUI, use a for loop to iterate through the array and print the results in your JTextArea.

switch(choice){
  case 1:{
    SortingAlgo algo=new SortingAlgo();
    data=algo.selectionSort(data);

    // you've got your sorted data here. 
    // put your for loop here to show it in your GUI

    break; 
  }

Upvotes: 0

Related Questions