[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 781
  • Last Modified:

Newbie trying to output string text to textField

I have a class called scrambledWordSelection(), which contains a word array.  I am trying to get wordSelection[0] to appear in the jTextField2 box.  Can anyone give me any pointers.

Thanks.

Here is the code:
____________________



import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;


public class Shufflebug extends javax.swing.JFrame {
   
    //Creates new form Shufflebug
    public Shufflebug() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setTitle("SHUFFLE BUG");
        setFont(new java.awt.Font("Tahoma", 0, 12));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton1.setText("Enter");
        jButton1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 230, 60, 30));

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton2.setText("Exit");
        jButton2.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 230, 60, 30));

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 16));
        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText("Enter your guess here");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 180, 280, 40));

        jTextField2.setBackground(new java.awt.Color(255, 255, 255));
        jTextField2.setEditable(false);
        jTextField2.setFont(new java.awt.Font("Tahoma", 0, 36));
        jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, 340, 70));

        pack();
    }

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
       
       scrambledWordSelection();
       
    }

         
    //Word array
    public void scrambledWordSelection(){
           //String wordSelection Array
        String[] wordSelection = {"TOBLFALO", "ALBETKLABS", "SEBLBLAA"};// declare an String array
       
        String[] wordsArray = new String[2];// create an array of Strings

            // assign a value to each array element and print
            for (int i = 0; i < wordSelection.length; i++)
            {  
               jTextField2.setText(wordSelection[0]); // Print first array to window
                break;
            }  
    }
   
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        //TODO add your handling code here:
        //Enter button
       
    }

   
   
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //User can enter in their guess for of the scrambled word here
       
     
    }

       
     //Exit the application
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
       System.exit(0);
    }
   
     // Exit the application
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
   
    // @param args the command line arguments
    public static void main(String args[]) {
        new Shufflebug().show();
       
     
    }
   
   
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration
}
0
shannonrice
Asked:
shannonrice
  • 4
  • 3
  • 3
  • +3
1 Solution
 
petmagdyCommented:
jTextField2.setText(  wordSelection[0].toString() );
0
 
sudhakar_koundinyaCommented:
textField.setText("your text");
0
 
sudhakar_koundinyaCommented:

jTextField2.setText(  wordSelection[0].toString() );
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CEHJCommented:
Are you trying to select a random element of your array? If so

jTextField2.setText(wordSelection[(int)(Math.random() * wordSelection.length)]);

If you *really* only want to assign the first element, then do


jTextField2.setText(wordSelection[0]);

as the loop is redundant in its present form
0
 
shannonriceAuthor Commented:
I tried each both ways and it still doesn't output the word to the jTextField2 area when the program is executed.
0
 
CEHJCommented:
>>when the program is executed.

You need some way to activate it. Put the call to scrambledWordSelection() into one of your button......actionPerformed methods
0
 
shannonriceAuthor Commented:
I did that and still nothing....

_______________________

  private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
        //scrambled word goes here
     
        scrambledWordSelection();
       
    }

         
    //Word array
    public void scrambledWordSelection(){
           //String wordSelection Array
        String[] wordSelection = {"TOBLFALO", "ALBETKLABS", "SEBLBLAA"};// declare an String array
       
        String[] wordsArray = new String[2];// create an array of Strings

            // assign a value to each array element and print
            for (int i = 0; i < wordSelection.length; i++)
            {  
                jTextField2.setText(wordSelection[(int)(Math.random() * wordSelection.length)]);// Select random word in array and output to jTextField
                break;
            }  
    }

   
         
0
 
JavatmCommented:
I fixed some of the codes try this :

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;


public class Shufflebug extends javax.swing.JFrame {
   
    //Creates new form Shufflebug
    public Shufflebug() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        getContentPane().setLayout(new GridLayout(2,2));

        setTitle("SHUFFLE BUG");
        setFont(new java.awt.Font("Tahoma", 0, 12));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 16));
        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText("Enter your guess here");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField1);

        jTextField2.setBackground(new java.awt.Color(255, 255, 255));
        jTextField2.setEditable(false);
        jTextField2.setFont(new java.awt.Font("Tahoma", 0, 20));
        jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField2);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton1.setText("Enter");
        jButton1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
                scrambledWordSelection();
            }
        });

        getContentPane().add(jButton1);

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton2.setText("Exit");
        jButton2.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton2);

        pack();
    }

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
       
       scrambledWordSelection();
       
    }

         
    //Word array
    public void scrambledWordSelection(){
           //String wordSelection Array
        String[] wordSelection = {"TOBLFALO", "ALBETKLABS", "SEBLBLAA"};// declare an String array
       
        String[] wordsArray = new String[2];// create an array of Strings

            // assign a value to each array element and print
            for (int i = 0; i < wordSelection.length; i++)
            {  
               jTextField2.setText(wordSelection[(int)(Math.random() * wordSelection.length)]);// Print first array to window
               break;
            }  
    }
   
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        //TODO add your handling code here:
        //Enter button
       
    }

   
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //User can enter in their guess for of the scrambled word here
       
     
    }
       
     //Exit the application
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
       System.exit(0);
    }
   
     // Exit the application
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
   
    // @param args the command line arguments
    public static void main(String args[]) {
        new Shufflebug().show();    
     
    }
   
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration
}

0
 
CEHJCommented:
The loop, as i mentioned before, is unnecessary
0
 
objectsCommented:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;


public class Shufflebug extends javax.swing.JFrame {
   
    //Creates new form Shufflebug
    public Shufflebug() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setTitle("SHUFFLE BUG");
        setFont(new java.awt.Font("Tahoma", 0, 12));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton1.setText("Enter");
        jButton1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 230, 60, 30));

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton2.setText("Exit");
        jButton2.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 230, 60, 30));

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 16));
        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText("Enter your guess here");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 180, 280, 40));

        jTextField2.setBackground(new java.awt.Color(255, 255, 255));
        jTextField2.setEditable(false);
        jTextField2.setFont(new java.awt.Font("Tahoma", 0, 36));
        jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, 340, 70));

        pack();
    }

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
       
       scrambledWordSelection();
       
    }

    private List words = Arrays.asList(new String[] {"TOBLFALO", "ALBETKLABS", "SEBLBLAA"});

    public void scrambledWordSelection(){
        Collections.shuffle(words);
        jTextField2.setText(words.get(0).toString()); // Print first array to window  
    }
   
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        //TODO add your handling code here:
        //Enter button
       
    }

   
   
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        //User can enter in their guess for of the scrambled word here
       
     
    }

       
     //Exit the application
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
       System.exit(0);
    }
   
     // Exit the application
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
   
    // @param args the command line arguments
    public static void main(String args[]) {
        new Shufflebug().show();
       
     
    }
   
   
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration
}
0
 
JavatmCommented:

I fixed some part and added a mouse press listener over jTextFiled1. So that when the user clicks jTextFiled1 the text there
will be erased so that the user can type his/her guess.

Reminder : Some parts may not be editable under Netbeans. You can compile and run this in command promt.

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;


public class Shufflebug extends javax.swing.JFrame {
   
    //Creates new form Shufflebug
    public Shufflebug() {

        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        getContentPane().setLayout(new GridLayout(2,2));

        setTitle("Shuffle Bug . . .");
        setFont(new java.awt.Font("Tahoma", 0, 12));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText("Enter your guess here");
        jTextField1.addMouseListener(new java.awt.event.MouseListener() {
            public void mouseEntered(java.awt.event.MouseEvent e) {}
            public void mousePressed(java.awt.event.MouseEvent e) {        
                jTextField1.setText("");
            }
            public void mouseClicked(java.awt.event.MouseEvent e) {}
            public void mouseReleased(java.awt.event.MouseEvent e) {}
            public void mouseExited(java.awt.event.MouseEvent e) {}
        });

        getContentPane().add(jTextField1);

        jTextField2.setBackground(new java.awt.Color(255, 255, 255));
        jTextField2.setEditable(false);
        jTextField2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField2);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton1.setText("Enter");
        jButton1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton1);

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jButton2.setText("Exit");
        jButton2.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton2);

        pack();
    }

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
       
       scrambledWordSelection();
       
    }

         
    //Word array
    public void scrambledWordSelection(){
           //String wordSelection Array
        String[] wordSelection = {"TOBLFALO", "ALBETKLABS", "SEBLBLAA"};// declare an String array
        String[] wordsArray = new String[2];// create an array of Strings

        jTextField2.setText(wordSelection[(int)(Math.random() * wordSelection.length)]);// Print first array to window
    }
   
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        scrambledWordSelection();
    }
       
     //Exit the application
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }
   
     // Exit the application
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
   
    // @param args the command line arguments
    public static void main(String args[]) {
        new Shufflebug().show();    
     
    }
   
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration
}

Hope that helps . . .
Javatm
0
 
shannonriceAuthor Commented:
Thanks!
0
 
JavatmCommented:
http://freewebs.com/cube-j

Always Glad To Help . . .
Friend : Javatm
0
 
CEHJCommented:
A correct implementation of my suggestion ;-) But

>>String[] wordsArray = new String[2];// create an array of Strings

is redundant
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now