Advertisement
Advertisement


Need a method to show results in JTextField


Question

Program Description: The purpose of this program is to allow a user to sort integers and fractions either in descending or ascending order. Using a series of 4 radio buttons the user can select the numeric type (integer or fraction) to be sorted and the way the numeric type is going to be sorted (ascending or descending). After the user has selected the options, put their numbers or fractions in the JTextField then they can simple hit the "Perform Sort" Button and the sort version of the numbers will appear in the result JTextField.

Problem: At the ActionListner in the P3GUI.JAVA, resultField.setText(( ai.inOrder(ai.root))); is used to display the sorted version of the integers in the resultField JTextField. However resultField.setText(( ai.inOrder(ai.root))); casues "incompatiable types: int cannot be converted to String."

Maybe a Integer toString method is need?? not sure

Any Suggestion on making this run will be greatly appreciated.

class getItListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String text;

        /*text = entryField.getText().trim(). replaceAll(" ","");
       String textArray[] = text.split(" ");*/
        text = getAmtValue();
        String textArray[] = text.split(" ");

        if (ascending.isSelected() && integer.isSelected()) { for integers
            BST ai = new BST(new Integer(textArray[0]));
            for (int i = 1; i < textArray.length; i++) {
                ai.insert(ai.root, new Integer(textArray[i]));
            }
            resultField.setText((int ai.inOrder(ai.root)));
        } else if (descending.isSelected() && integer.isSelected()) {
            //Do Something
        } else if (ascending.isSelected() && fraction.isSelected()) { 
            // Do Something
        } else if (descending.isSelected() && fraction.isSelected()) { 
          // Do Something
        } else {
            JOptionPane.showMessageDialog(popup, "Please entvalid amount.");
        }
    }
}

METHOD BEING CALLED:

public int inOrder(Node node) {
    if (node != null) {
        inOrder(node.left);

        inOrder(node.right);
    }
    return ((Integer) node.element);
} 




/**
 *
 * @param node
 * @return 
 */
public int descOrder(Node node) {
    if (node != null) {
        descOrder(node.right);

        descOrder(node.left);
    }
   return ((Integer) node.element);
}

FULL CODE

MAIN

package p3gui;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

/**
 *
 * @author Mike
 */
public class P3GUI extends JFrame {

    JFrame f = new JFrame("Binary Search Tree Sort");// Title
/////////////////////////////////////////////Set up of fields, labels and buttons//////////////////////////////////////////////
    private final JButton evaluate;
    private final JLabel textfieldLabel;
    private final JTextField entryField;
    private final JLabel resutfieldlabel;
    private final JLabel radioLabel1;
    private final JLabel radioLabel2;
    private final JTextField resultField;
    private final JRadioButton ascending;
    private final JRadioButton descending;
    private final JRadioButton integer;
    private final JRadioButton fraction;
    private final ButtonGroup radioButtons = new ButtonGroup();//create radio buttons group
    private final ButtonGroup radioButtons2 = new ButtonGroup();//create radio buttons group
    private final JOptionPane popup = new JOptionPane();

/////////////////////////////////////////////Display///////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////Start Panel///////////////////////////////////////////////////////////////////////
    P3GUI() {

        f.setSize(425, 375);
        f.setLayout(null);//using no layout managers  
        f.setVisible(true);//making the frame visible  //window size
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/////////////////////////////////////////////JRadioButton label////////////////////////////////////////////////////////////
        radioLabel2 = new JLabel(" Numeric Type ");
        f.add(radioLabel2);
        radioLabel2.setBounds(225, 275, 100, 15);

/////////////////////////////////////////////JRadioButton Ascending///////////////////////////////
        integer = new JRadioButton(" Integer ");
        radioButtons.add(integer);
        f.add(integer);
        integer.setBounds(225, 295, 160, 15);

/////////////////////////////////////////////JRadioButton Descending///////////////////////////////
        fraction = new JRadioButton(" Fraction ");
        radioButtons.add(fraction);
        f.add(fraction);
        fraction.setBounds(225, 315, 160, 15);

/////////////////////////////////////////////JRadioButton label////////////////////////////////////////////////////////////
        radioLabel1 = new JLabel(" Sorted Order ");
        f.add(radioLabel1);
        radioLabel1.setBounds(10, 275, 100, 15);

/////////////////////////////////////////////JRadioButton Ascending///////////////////////////////
        ascending = new JRadioButton(" Ascending ");
        radioButtons2.add(ascending);
        f.add(ascending);
        ascending.setBounds(10, 295, 160, 15);

/////////////////////////////////////////////JRadioButton Descending///////////////////////////////
        descending = new JRadioButton(" Descending ");
        radioButtons2.add(descending);
        f.add(descending);
        descending.setBounds(10, 315, 160, 15);

/////////////////////////////////////////////Text Field Label and Field////////////////////////////////////////////////////////
        textfieldLabel = new JLabel(" Original List ");
        f.add(textfieldLabel);
        textfieldLabel.setBounds(42, 10, 160, 25);

/////////////////////////////////////////////Entry Field Label and Field////////////////////////////////////////////////////////
        entryField = new JTextField("");
        //entryField.addActionListener(this);//ActionListener
        f.add(entryField);
        entryField.setBounds(118, 10, 245, 25);

/////////////////////////////////////////////Add Evaluate Button///////////////////////////////////////////////////////////////
        evaluate = new JButton(" Perform Sort ");//creating instance of JButton  
        f.add(evaluate);
        evaluate.setBounds(137, 180, 130, 30);

/////////////////////////////////////////////Result label and Field////////////////////////////////////////////////////////////
        resutfieldlabel = new JLabel(" Sorted List ");
        f.add(resutfieldlabel);
        resutfieldlabel.setBounds(52, 100, 100, 25);

        resultField = new JTextField("");
        //resultField.addActionListener(this);//ActionListener
        resultField.setEditable(false);
        f.add(resultField);
        resultField.setBounds(125, 100, 220, 25);

        evaluate.addActionListener(new getItListener());

    }//END of P3GUI

    class getItListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String text;

            /*text = entryField.getText().trim(). replaceAll(" ","");
           String textArray[] = text.split(" ");*/
            text = getAmtValue();
            String textArray[] = text.split(" ");

            if (ascending.isSelected() && integer.isSelected()) { //ascending for integers
                BST ai = new BST(new Integer(textArray[0]));
                for (int i = 1; i < textArray.length; i++) {
                    ai.insert(ai.root, new Integer(textArray[i]));
                }
                resultField.setText((int ai.inOrder(ai.root)));
            } else if (descending.isSelected() && integer.isSelected()) { // descending for integer
                //Do Something
            } else if (ascending.isSelected() && fraction.isSelected()) { //ascending for fractions
                // Do Something
            } else if (descending.isSelected() && fraction.isSelected()) { // descending for fractions
              // Do Something
            } else {
                JOptionPane.showMessageDialog(popup, "Please entvalid 
 amount.");
            }
        }
    }

    //returns value in text field
    public String getAmtValue() {
        try {
            return (entryField.getText().trim().replaceAll(" ", ""));
        } catch (NumberFormatException e) {
            System.out.println("This is not a number");
            eraseTextField();
            return "";
        }
    }

    //clears text field
    public void eraseTextField() {
        entryField.setText("");
        entryField.requestFocus();
    }

    public static void main(String[] args) {
        P3GUI p3GUI;
        p3GUI = new P3GUI();

    }

}

BST.JAVA

package p3gui;


class Node {

public Object element;
public Node left;
public Node right;

// CONSTRUCTORS 
public Node(Object theElement) {
    this(theElement, null, null);
}

public Node(Object theElement, Node lLink, Node rLink) {
    element = theElement;
    this.left = lLink;
    this.right = rLink;
}
}

public class BST {

    public Node root;

    public BST(Object x) { // ONLY CONSTRUCTOR//
        root = new Node(x);
    }


    public Node insert(Node node, Integer x) {
        if (node == null) {
            return node = new Node(x);
        }
        if (x < (Integer) node.element) {
            node.left = insert(node.left, x);
        } else {
            node.right = insert(node.right, x);
        }
        return node;
    }


    /**
     *
     * @param node
     * @return 
     */
    public int inOrder(Node node) {
        if (node != null) {
            inOrder(node.left);

            inOrder(node.right);
        }
        return ((Integer) node.element);
    } 




    /**
     *
     * @param node
     * @return 
     */
    public int descOrder(Node node) {
        if (node != null) {
            descOrder(node.right);

            descOrder(node.left);
        }
       return ((Integer) node.element);
    }




}
2017/10/17
1
0
10/17/2017 11:29:24 AM