Search code examples
javauser-interfacearraylistjtextfield

GUI Freezes and Output to JTextField from user input


I am having I hope only two issues currently. I have had help on this on a previous question that I have asked but now I am running into different issues. My current issues now is that I need to print out into a JTextField a sorted array. I do not have questions about how to sort the array I just would like help on how to get the array to print to the JTextField when the Bubble Sort button is pressed. Currently when I press the button it freezes. It freezes at this line.

list.add(Integer.valueOf(input.nextInt()));

please help Thank you.

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;
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);

/**
 * 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) {
            list.add(Integer.valueOf(input.nextInt()));
//              for(int i = 0; i < list.size(); i++){
//                  
//              }

        }

    });

    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) {

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

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

          }
}

what is needed:

  • The GUI will have three buttons that will represent sorting methods o Bubble o Quick o Merge
  • The GUI will have an area for the user to input 5 numbers to sort. store them in an array or arrayList
  • The GUI will have an area where each step of the sorting is displayed. This can be one area where each time a step is completed it replaces the line or several lines showing each step.

Current update within the Bubble Sort Button:

 public void actionPerformed(ActionEvent arg0) {
            userInput = inputArea.getText();
            Scanner input = new Scanner(userInput);
            list.add(Integer.valueOf(input.nextInt()));
            for(int i = 0; i < list.size(); i++){
                outputArea.setText(userInput);
            }

Solution

  • Don't try to mix Scanner input with a Swing GUI. Your problem is by doing this, you're tying up the Swing event thread, freezing your GUI as your program awaits Scanner input.

    Better to get your input through the GUI, optimally through JTextComponents such as JTextFields, or if you must, via a JOptionPane, as long as it's done in a way that plays nice with the Swing event thread.


    Edit
    Your comment:

    • The GUI will have three buttons that will represent sorting methods o Bubble o Quick o Merge
    • The GUI will have an area for the user to input 5 numbers to sort. store them in an array or arrayList
    • The GUI will have an area where each step of the sorting is displayed. This can be one area where each time a step is completed it replaces the line or several lines showing each step.

    You know that you can give your GUI 5 JTextAreas, one for each number to be entered.


    Edit 2
    You state:

    So I am also trying something new. Not sure if it is right. So when button is pushed I put within the action listener: userInput = inputArea.getText(); Scanner input = new Scanner(userInput); Is this what you were talking about?

    That could work, but if you do it that way, you'll need a single JTextArea, and the user will have to know that their numbers should be separated by a single space. Again a Scanner can work well here since you can call nextInt() on it and get your data (as long as the data is numeric, and the correct number of numbers entered). But again, don't use a Scanner with System.in with a GUI except in some unusual circumstances. Also remember to close your Scanner when you're done with it.