1

I am trying to make a program that takes the number of students that are going to be graded and then get the x number of grades and bubble sort them in ascending order.

I am not sure how to add the grades from the arraylist to jlist and output it. this is my error:

Unresolved compilation problem: 
    The method setListData(Integer[]) in the type JList<Integer> is not applicable for the arguments (Object[])

This is my code:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;

public class bubbleSwing {

    private JFrame frame;
    private JTextField inputNoOfGrades;
    private JTextField inputGrades;
    private String inputStr;
    private JLabel tvNoOfGrades;
    private JButton btnNoOfGrades;
    private JLabel tvGrades;
    private JButton btnGrades;
    private JButton btnSort;
    private JList<Integer> list;

    ArrayList<Integer> grades = new ArrayList<Integer>();



    private int numOfGrades;
    private int inputGradesInt;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    bubbleSwing bubbleSwing = new bubbleSwing();
                    bubbleSwing.frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    /**
     * Create the application.
     */
    public bubbleSwing() {
        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.getContentPane().setLayout(null);

        tvNoOfGrades = new JLabel("New label");
        tvNoOfGrades.setBounds(10, 11, 150, 29);
        frame.getContentPane().add(tvNoOfGrades);

        tvGrades = new JLabel("New label");
        tvGrades.setBounds(170, 11, 150, 29);
        frame.getContentPane().add(tvGrades);

        inputNoOfGrades = new JTextField();
        inputNoOfGrades.setBounds(10, 51, 86, 20);
        frame.getContentPane().add(inputNoOfGrades);
        inputNoOfGrades.setColumns(10);

        inputGrades = new JTextField();
        inputGrades.setBounds(158, 51, 86, 20);
        frame.getContentPane().add(inputGrades);
        inputGrades.setColumns(10);

        btnNoOfGrades = new JButton("Submit");
        btnNoOfGrades.setBounds(10, 82, 89, 23);
        frame.getContentPane().add(btnNoOfGrades);

        btnGrades = new JButton("Submit grades");
        btnGrades.setBounds(156, 83, 101, 20);
        frame.getContentPane().add(btnGrades);

        btnSort = new JButton("Sort");
        btnSort.setBounds(290, 61, 89, 23);
        frame.getContentPane().add(btnSort);

        list = new JList<Integer>();
        list.setBounds(60, 116, 167, 135);
        frame.getContentPane().add(list);
        btnNoOfGrades.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                getNoOfGrades();

            }
        });
        btnGrades.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addToList();

            }
        });

        btnSort.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                bubbleSort(grades);

            }
        });
    }

    private void addToList() {
        inputStr = inputGrades.getText().toString().replaceAll(" ", "");

        if (inputStr.isEmpty()) {
            tvGrades.setText("no grades added");
        } else {
            // converts string to integer and checks if item is already
            // added

            if (grades.size() < numOfGrades) {

                inputGradesInt = Integer.parseInt(inputStr);
                if (inputGradesInt < 0 || inputGradesInt > 10) {

                    tvGrades.setText("Enter a grade between 1-10");
                } else {

                    grades.add(inputGradesInt);
                    list.setListData( grades.toArray()); 

                    inputGrades.setText("");
                    list.notify();
                }

            } else {

                btnSort.setEnabled(true);
            }

        }

    }

    private void getNoOfGrades() {

        inputStr = inputNoOfGrades.getText().toString();//.replaceAll(" ", "");

        if (inputStr.isEmpty()) {
            tvNoOfGrades.setText("no items added");
        } else {
            // converts string to integer and checks if item is already
            // added

            numOfGrades = Integer.parseInt(inputStr);
            if (numOfGrades > 0) {
                inputNoOfGrades.setEnabled(false);
                btnNoOfGrades.setEnabled(false);
                tvGrades.setText("Enter " + numOfGrades
                        + " student grades between 1-10");
            }
        }

    }

    private void bubbleSort(ArrayList<Integer> grades) {

        for (int i = grades.size() - 1; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                if (grades.get(j) > grades.get(j + 1)) {
                    int temp = grades.get(j);
                    grades.set(j, grades.get(j + 1));
                    grades.set(j + 1, temp);
                }
            }
        }
    }

}
1
  • 1
    Try something like list.setListData( grades.toArray(new Integer[0]));, List#toArray returns Object[] Commented May 17, 2016 at 20:31

1 Answer 1

1

Collection.toArray() returns Object[]. You need to use Collection.toArray(T[]) to get an array of your specific object type:

list.setListData(grades.toArray(new Integer[grades.size()]));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.