Question

OK, I have an assignment where we are to create a generic pair class, and then test it to see if it works. The pair class has certain requirements that must be followed, but we are free to do what we want with the test class. I have admittedly gone a bit overboard, but only because playing around with ideas helps me learn other things we don't cover in class. Now, I can't show the generic class stuff because that part is the important part of the assignment, but as for my test class, that I will show all of. I am attempting to make it so that my fifth text field, (tf5), can take in user information, and after hitting the enter key, that information, (Which is set as an ArrayList), is then sent over to my JSrollPanel, (myScrollPane). Right now, the way I have my keylistener setup, it adds anything I type into the text field, into the JScrollPanel, as I am typing it. So, if I type 1, 2, 3, 4, I get 4 array lists printed out to the panel, (with 1 number each). I want to be able to type whatever I want into the text field, and have it passed into the JScrollPanel only after I hit enter, but for the life of me I just can't sort out how the heck to do it. Can anyone help me out with this? Here's my code:

package lab10;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Integer.*;
import java.util.ArrayList;


public class Lab10 extends JFrame {

private Container content;
private JTextField tf1, tf2, tf3, tf4, tf5, tf6;
private JLabel lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, lbl9, lbl10;
private Font myFont;
private JButton myBtn, myBtn2, tstb;
private JTextArea myTextArea;
private JScrollPane myScrollPane;
private KeyEvent x;
Integer I1, I2;
String S1, S2;
Double D1, D2;
ArrayList A1, A2;
Pair<Integer, Integer> myPairInt = new Pair<>(I1, I2);
Pair<String, Double> myPairStrDbl = new Pair<>(S1, D2);
Pair<ArrayList, String> myPairAryStr = new Pair<>(A1, S2);

public Lab10() {
    this(640, 540, "Testing a Generic Pair");
}

public Lab10(int width, int height, String title) {
    this.setVisible(true);
    this.setTitle(title);
    this.setSize(width, height);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiComponent();

}

private void guiComponent() {
    SpringLayout layout = new SpringLayout();
    content = this.getContentPane();
    content.setLayout(layout);
    myFont = new Font("Magneto", Font.BOLD, 12);
    myBtn = new JButton("Do It!");
    myBtn2 = new JButton();
    myBtn2.setLayout(new BorderLayout());
    JLabel label1 = new JLabel("Click here to add elements to Array List");
    JLabel label2 = new JLabel("(Before you click the bottom button!)");
    myBtn2.add(BorderLayout.NORTH, label1);
    myBtn2.add(BorderLayout.SOUTH, label2);
    myBtn.addActionListener(new ButtonClick());
    myBtn2.addActionListener(new ArrayListClickAdd());
    lbl1 = new JLabel("Enter a string of numbers for me to count");
    lbl2 = new JLabel("Now enter another string of numbers for me to count");
    lbl3 = new JLabel("This is where I show you what you typed ");
    lbl4 = new JLabel("Now type a String and I'll count the Ascii sum ");
    lbl5 = new JLabel("Now type in a double, i.e. 2.4 or 2.3 or 5.6 ");
    lbl6 = new JLabel("This is where I show you what you typed ");
    lbl7 = new JLabel("Now Enter an Array List, (just a standard string of numbers) ");
    lbl8 = new JLabel("Now Enter a String and I will convert it all to uppercase");
    lbl9 = new JLabel("This is where I will show you what you've typed ");
    lbl10 = new JLabel();
    tf1 = new JTextField(8);
    tf2 = new JTextField(8);
    tf3 = new JTextField(8); 
    tf4 = new JTextField(8);
    tf5 = new JTextField(8); 
    tf6 = new JTextField(8);

        tf5.addKeyListener(new MyKeyEvent());

    myTextArea = new JTextArea();
    myTextArea.setColumns(12);
    myTextArea.setRows(8);
    myTextArea.setFont(myFont);


    content.add(lbl1);
    layout.putConstraint(SpringLayout.WEST, lbl1, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl1, 10, SpringLayout.NORTH, content);

    content.add(tf1);
    layout.putConstraint(SpringLayout.WEST, tf1, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, tf1, 10, SpringLayout.SOUTH, lbl1);

    content.add(lbl2);
    layout.putConstraint(SpringLayout.WEST, lbl2, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl2, 10, SpringLayout.SOUTH, tf1);

    content.add(tf2);
    layout.putConstraint(SpringLayout.WEST, tf2, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, tf2, 10, SpringLayout.SOUTH, lbl2);

    content.add(lbl3);
    layout.putConstraint(SpringLayout.WEST, lbl3, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl3, 10, SpringLayout.SOUTH, tf2);

    content.add(lbl4);
    layout.putConstraint(SpringLayout.WEST, lbl4, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl4, 10, SpringLayout.SOUTH, lbl3);

    content.add(tf3);
    layout.putConstraint(SpringLayout.WEST, tf3, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, tf3, 10, SpringLayout.SOUTH, lbl4);

    content.add(lbl5);
    layout.putConstraint(SpringLayout.WEST, lbl5, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl5, 10, SpringLayout.SOUTH, tf3);

    content.add(tf4);
    layout.putConstraint(SpringLayout.WEST, tf4, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, tf4, 10, SpringLayout.SOUTH, lbl5);

    content.add(lbl6);
    layout.putConstraint(SpringLayout.WEST, lbl6, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl6, 10, SpringLayout.SOUTH, tf4);

    content.add(lbl7);
    layout.putConstraint(SpringLayout.WEST, lbl7, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl7, 10, SpringLayout.SOUTH, lbl6);

    content.add(tf5);
    layout.putConstraint(SpringLayout.WEST, tf5, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, tf5, 10, SpringLayout.SOUTH, lbl7);

    content.add(lbl8);
    layout.putConstraint(SpringLayout.WEST, lbl8, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl8, 10, SpringLayout.SOUTH, tf5);

    content.add(tf6);
    layout.putConstraint(SpringLayout.WEST, tf6, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, tf6, 10, SpringLayout.SOUTH, lbl8);

    content.add(lbl9);
    layout.putConstraint(SpringLayout.WEST, lbl9, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl9, 10, SpringLayout.SOUTH, tf6);

    content.add(lbl10);
    layout.putConstraint(SpringLayout.WEST, lbl10, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, lbl10, 2, SpringLayout.SOUTH, lbl9);

    myScrollPane = new JScrollPane(myTextArea);
    content.add(myScrollPane);
    layout.putConstraint(SpringLayout.WEST, myScrollPane, 250, SpringLayout.EAST, tf4);
    layout.putConstraint(SpringLayout.NORTH, myScrollPane, 1, SpringLayout.SOUTH, myBtn2);

    content.add(myBtn2);
    layout.putConstraint(SpringLayout.WEST, myBtn2, 3, SpringLayout.EAST, lbl7);
    layout.putConstraint(SpringLayout.NORTH, myBtn2, 8, SpringLayout.SOUTH, lbl6);

    content.add(myBtn);
    layout.putConstraint(SpringLayout.WEST, myBtn, 10, SpringLayout.WEST, content);
    layout.putConstraint(SpringLayout.NORTH, myBtn, 10, SpringLayout.SOUTH, lbl10);
}

private String upper(String y) {
    return y.toUpperCase();
}

private String getAsciiSum(String z) {
    int addSum = 0;
    for (int i = 0; i < z.length(); i++) {
        char x = z.charAt(i);
        int j = (int) x;
        addSum += x;
    }
    z = Integer.toString(addSum);
    return z;
}

int sumNums(int x) {
    String myNum = Integer.toString(x);
    int addSum = 0;
    for (int i = 0; i < myNum.length(); i++) {
        addSum += Character.digit(myNum.charAt(i), 10);
    }
    return addSum;
}

int[] getArray(int num) {
    String counter = "" + num;
    int digits = counter.length();
    int[] nums = new int[digits];
    for (int i = digits - 1; i >= 0; i--) {
        nums[i] = num % 10;
        num /= 10;
    }
    return nums;
}

private class MyKeyEvent implements KeyListener {

    public MyKeyEvent() {
    }

    @Override
    public void keyTyped(KeyEvent ke) {

        ke.setKeyCode(KeyEvent.VK_RIGHT);
       if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {

            String arrNum = tf5.getText();
            ArrayList arList = new ArrayList();
            arList.add(arrNum);
            myPairAryStr.setFirst(arList);
            myTextArea.append(myPairAryStr.getFirst() + "\n");
            tf5.setText("");
       }
    }

    @Override
    public void keyPressed(KeyEvent ke) {
    }

    @Override
    public void keyReleased(KeyEvent ke) {
    }
}

private class ArrayListClickAdd implements ActionListener {

    public ArrayListClickAdd() {
    }

    @Override
    public void actionPerformed(ActionEvent ae) {

        String arrNum = tf5.getText();
        ArrayList arList = new ArrayList();
        arList.add(arrNum);
        myPairAryStr.setFirst(arList);
        myTextArea.append(myPairAryStr.getFirst() + "\n");
        tf5.setText("");
    }
}

private class ButtonClick implements ActionListener {

    public ButtonClick() {
    }

    @Override
    public void actionPerformed(ActionEvent ae) {


        int num = Integer.parseInt(tf1.getText());
        int num2 = Integer.parseInt(tf2.getText());
        String num3 = tf3.getText();
        double num4 = Double.parseDouble(tf4.getText());
        String arrLst = tf5.getText();
        String num6 = tf6.getText();

        int count1 = sumNums(num);
        int count2 = sumNums(num2);
        String strSum = getAsciiSum(num3);
        String goBig = upper(num6);


        Integer int1 = new Integer(count1);
        Integer int2 = new Integer(count2);
        String str1 = strSum;
        Double dbl2 = new Double(num4);
        ArrayList arList = new ArrayList();
        arList.add(arrLst);
        String str2 = goBig;

        myPairInt.setFirst(int1);
        myPairInt.setSecond(int2);
        myPairStrDbl.setFirst(str1);
        myPairStrDbl.setSecond(dbl2);
        myPairAryStr.setFirst(arList);
        myPairAryStr.setSecond(str2);

        lbl3.setText("Your numbers fromthe first field add up to : " + myPairInt.getFirst() + " and your numbers from the second field add up to : " + myPairInt.getSecond());
        lbl6.setText("The Ascii sum of the first Field is : " + myPairStrDbl.getFirst() + " and you typed : " + myPairStrDbl.getSecond() + " In the Second Field");
        lbl9.setText("You typed : " + myPairAryStr.getFirst() + " In the first Field,");
        lbl10.setText("and : " + myPairAryStr.getSecond() + " In the Second");
        myTextArea.append(myPairAryStr.getFirst() + "\n");

    }
}

public static void main(String[] args) {
    Lab10 test = new Lab10();
}

}

Was it helpful?

Solution

Start by taking a look at How to use textfields.

Basically, you want to register an ActionListener with on the textfield, this will be notified when ever the user presses the Enter key

Take a look at How to write Action Listeners for more details.

You should avoid KeyListeners, using a DocumentListener to monitor for changes to the underlying document or a DocumentFilter if you want to filter what is being entered. See Using Text Components for more details

OTHER TIPS

Don't recreate the ArrayList on each keyTyped. Create a field of the class for the ArrayList and on type add values in the list.

When ENTER is pressed send content of the ArrayList to the text area and recreate it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top