user2929005
user2929005

Reputation: 61

Cant output to GUI, can output to console

Thank you guys for helping me out so much on this particular issue. I have managed to do everything that is required in this program except I cant get the results that I need to output to the GUI. I have looked at other forums and some have said output to a textField instead of a textArea but either way I still end up getting an error.

Here is my error when my outputArea is set to textField using .append:

The method append(int) is undefined for the type JTextField.

I am just curious as to what I should use for this problem.

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

public class Sorting {

private JFrame frame;
private JTextArea inputArea;
private JTextField outputArea;
String userInput;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Sorting window = new Sorting();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Sorting() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Sorting");
    frame.getContentPane().setLayout(null);

    JButton bubbleButton = new JButton("Bubble Sort");
    bubbleButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String userInput = inputArea.getText();
            String[] output = userInput.split(" ");
            int[] list = new int[output.length];

            for(int i = 0; i < output.length; i++){
                try{
                    list[i] = Integer.parseInt(output[i]);
                } catch (NumberFormatException nfe){};
            }
            bubbleSort(list);
            for(int k = 0; k < list.length; k++){
                outputArea.setText(list[k] + " ");
//                  System.out.print(list[k] + " ");
            }

        }

    });

    bubbleButton.setBounds(10, 211, 114, 23);
    frame.getContentPane().add(bubbleButton);

    JButton mergeButton = new JButton("Merge Sort");
    mergeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String userInput = inputArea.getText();
            String[] output = userInput.split(" ");
            int[] list = new int[output.length];

            for(int i = 0; i < output.length; i++){
                try{
                    list[i] = Integer.parseInt(output[i]);
                } catch (NumberFormatException nfe){};
            }
            mergeSort(list);
            for(int k = 0; k < list.length; k++){
                System.out.print(list[k] + " ");
            }
        }
    });
    mergeButton.setBounds(305, 211, 114, 23);
    frame.getContentPane().add(mergeButton);

    JButton quickButton = new JButton("Quick Sort");
    quickButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String userInput = inputArea.getText();
            String[] output = userInput.split(" ");
            int[] list = new int[output.length];

            for(int i = 0; i < output.length; i++){
                try{
                    list[i] = Integer.parseInt(output[i]);
                } catch (NumberFormatException nfe){};
            }
            quickSort(list);
            for(int k = 0; k < list.length; k++){
                System.out.print(list[k] + " ");
            }
        }
    });
    quickButton.setBounds(163, 211, 114, 23);
    frame.getContentPane().add(quickButton);

    inputArea = new JTextArea();
    inputArea.setBounds(10, 36, 414, 51);
    frame.getContentPane().add(inputArea);

    outputArea = new JTextField();
    outputArea.setEditable(false);
    outputArea.setBounds(10, 98, 414, 59);
    frame.getContentPane().add(outputArea);
    outputArea.setColumns(10);

    JLabel label = new JLabel("Please Enter 5 Numbers");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setBounds(10, 11, 414, 14);
    frame.getContentPane().add(label);

}

protected void quickSort(int[] list) {
    quickSort(list, 0, list.length - 1);
}

private void quickSort(int[] list, int first, int last) {
    if(last > first){
        int pivotIndex = partition(list, first, last);
        quickSort(list, first, pivotIndex -1);
        quickSort(list, pivotIndex + 1, last);
    }

}

private int partition(int[] list, int first, int last) {
    int pivot = list[first];
    int low = first + 1;
    int high = last;

    while(high > low){
        while(low <= high && list[low] <= pivot)
            low++;

        while(low <= high  && list[high] > pivot)
            high--;
        if(high > low){
            int temp = list[high];
            list[high] = list[low];
            list[low] = temp;
        }

    }

    while(high > first && list[high] >= pivot)
        high--;

    if(pivot > list[high]){
        list[first] = list[high];
        list[high] = pivot;
        return high;
    }
    else{
    return first;
    }
}

protected void mergeSort(int[] list) {
    if(list.length > 1){
        int[] firstHalf = new int[list.length / 2];
        System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
        mergeSort(firstHalf);

        int secondHalfLength = list.length - list.length / 2;
        int[] secondHalf = new int[secondHalfLength];
        System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
        mergeSort(secondHalf);

        merge(firstHalf, secondHalf, list);
    }
}

private void merge(int[] list1, int[] list2, int[] temp) {
    int current1 = 0;
    int current2 = 0;
    int current3 = 0;

    while(current1 < list1.length && current2 < list2.length) {
        if(list1[current1] < list2[current2])
            temp[current3++] = list1[current1++];
        else
            temp[current3++] = list2[current2++];
    }

    while(current1 < list1.length)
        temp[current3++] = list1[current1++];

    while(current2 < list2.length)
        temp[current3++] = list2[current2++];

}

protected void bubbleSort(int[] list) {
    boolean needNextPass = true;
    for(int k = 1; k < list.length && needNextPass; k++){
        needNextPass = false;
        for (int i = 0; i < list.length - k; i++){
            if(list[i] > list[i + 1]){
                int temp = list[i];
                list[i] = list[i + 1];
                list[i + 1] = temp;

                needNextPass = true;
            }
        }
    }

}
}

When I have my outputArea in the Bubble Sort button set like this I have no error and prints out the highest number inputed by the user.

for(int k = 0; k < list.length; k++){
                outputArea.setText(list[k] + " ");
//                  System.out.print(list[k] + " ");
            }

Upvotes: 0

Views: 189

Answers (1)

Dan Bron
Dan Bron

Reputation: 2324

I haven't used Java in a decade, but it seems to me the exception is complaining that the text field will not accept numbers.

Try formatting your int as a String before passing to the GUI (e.g. try list[k].toString() or some permutation, though I would have expected the + " " to silently coerce the value to a string anyway).

Upvotes: 1

Related Questions