can anyone convert this code to GUI?? I really don't know where to put the GUI codes in it.. :(
please help.. :(

import java.util.Scanner;
//main class
public class Main
{

    public static void main(String[] args)
    {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];

        System.out.print("Enter "+ n +" numbers: ");
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }

        SelectionSort access = new  SelectionSort();
        System.out.print("The Sorted numbers: ");
        access.SelectionSort(x);
    }

}

//selectionSort class

public class SelectionSort
{

     public void SelectionSort(int[] arr){
     for(int i=0; i<arr.length; i++)
     {
        for(int j=i+1; j<arr.length; j++)
        {
            if(arr[i] > arr[j] )
            {
                int temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
            }
        }
     }

     for(int i=0; i<arr.length; i++)
     {
         System.out.print(arr[i] + " ");
     }
}
}

In your main class, create a GUI using JFrame and a JPanel to accept the inputs from a user. Once you are done with the input, compute and display it out to the same or another JPanel... A very simple JFrame with a button...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public static void main(String[] args) {
  JFrame f = new JFrame("A JFrame");
  JPanel pane = new JPanel();  // a panel
  JButton jb1 = new JButton("Click");

  // do your add action listener to the button somehow
  // please read the JButton

  pane.add(jb1);  // add the button to the panel
  f.getContentPane().add(pane);  // add the JPanel to the frame

  // set up the frame and make it visible
  f.setSize(400, 250);  // set width & height
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setVisible(true);
}

Or you can read this for a tutorial...

Edited 4 Years Ago by Taywin

Hi sir, thanks for the reply..

..btw, I mess up all my codes, all I want to do is just like this code below but this code only converts the text to a dialog box and not the functions or process of sorting which is in this code.

import java.util.Scanner;
import javax.swing.JOptionPane;

public class Main
{

    public static void main(String[] args)
    {


        Scanner input = new Scanner(System.in);

        JOptionPane.showInputDialog(null, "Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];

        JOptionPane.showInputDialog(null, "Enter "+ n +" numbers: ");
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }

        SelectionSort access = new  SelectionSort();
        System.out.print("The Sorted numbers: ");
        access.SelectionSort(x);
    }

}

please help me again sir.. Im just a beginner in java and GUI codes. I really don't know how.

btw, this is my project in class and my professor said that if I can convert this to GUI then Im exempted for our Final Examination tomorrow.

I really need your help sir.. :(

Any help is highly appreciated!

Edited 4 Years Ago by gm.rupert

in which case it looks like you are asking us to do your schoolwork. your professor said that you'll be exempted for the examination, based on the assumption that you'll know more than enough of java coding to pass the test. that YOU know ... not that we do it.
my advice, either start Swing coding, or, if your examination is tomorrow, start studying.

@riahc3: why it sucks? it doesn't. it might take some effort to get to know it good enough to be fluent in it, but once you do, you'll see what I mean.

This article has been dead for over six months. Start a new discussion instead.