Note that there are some explanatory texts on larger screens.

plurals
  1. POCant output to GUI, can output to console
    primarykey
    data
    text
    <p>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. </p> <p>Here is my error when my outputArea is set to textField using .append:</p> <pre><code>The method append(int) is undefined for the type JTextField. </code></pre> <p>I am just curious as to what I should use for this problem.</p> <pre><code>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 &lt; output.length; i++){ try{ list[i] = Integer.parseInt(output[i]); } catch (NumberFormatException nfe){}; } bubbleSort(list); for(int k = 0; k &lt; 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 &lt; output.length; i++){ try{ list[i] = Integer.parseInt(output[i]); } catch (NumberFormatException nfe){}; } mergeSort(list); for(int k = 0; k &lt; 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 &lt; output.length; i++){ try{ list[i] = Integer.parseInt(output[i]); } catch (NumberFormatException nfe){}; } quickSort(list); for(int k = 0; k &lt; 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 &gt; 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 &gt; low){ while(low &lt;= high &amp;&amp; list[low] &lt;= pivot) low++; while(low &lt;= high &amp;&amp; list[high] &gt; pivot) high--; if(high &gt; low){ int temp = list[high]; list[high] = list[low]; list[low] = temp; } } while(high &gt; first &amp;&amp; list[high] &gt;= pivot) high--; if(pivot &gt; list[high]){ list[first] = list[high]; list[high] = pivot; return high; } else{ return first; } } protected void mergeSort(int[] list) { if(list.length &gt; 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 &lt; list1.length &amp;&amp; current2 &lt; list2.length) { if(list1[current1] &lt; list2[current2]) temp[current3++] = list1[current1++]; else temp[current3++] = list2[current2++]; } while(current1 &lt; list1.length) temp[current3++] = list1[current1++]; while(current2 &lt; list2.length) temp[current3++] = list2[current2++]; } protected void bubbleSort(int[] list) { boolean needNextPass = true; for(int k = 1; k &lt; list.length &amp;&amp; needNextPass; k++){ needNextPass = false; for (int i = 0; i &lt; list.length - k; i++){ if(list[i] &gt; list[i + 1]){ int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; needNextPass = true; } } } } } </code></pre> <p>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.</p> <pre><code>for(int k = 0; k &lt; list.length; k++){ outputArea.setText(list[k] + " "); // System.out.print(list[k] + " "); } </code></pre>
    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.
 

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