Search code examples
javajtextfield

Updating text in a JTextField


K, so unlike with my last question, I've been proactive about trying to deal with this problem a number of times, and it's still not working.

Basically I'm trying implement a JTextField. I've added the action listener to it and the getters and setters for the text are working, but text that I'm enter isn't showing up in the textfield. I tried setting the text color to black and that didn't help. Honestly, I'm not sure what the issue is.

K here's the code.

import acm.program.*;
import java.awt.event.*;
import javax.swing.*;

public class NameSurfer extends Program implements NameSurferConstants {
//Change back to program after this
/* Method: init() */
/**
 * This method has the responsibility for reading in the data base
 * and initializing the interactors at the bottom of the window.
 */
public void init() {
    // You fill this in, along with any helper methods //
    createUI();
    addActionListeners();
}

/* Method: actionPerformed(e) */
/**
 * This class is responsible for detecting when the buttons are
 * clicked, so you will have to define a method to respond to
 * button actions.
 */
public void actionPerformed(ActionEvent e) {
    // You fill this in //
    if(e.getSource() == nameField || e.getSource() == graphName) {
        drawNameGraph(nameField.getText());
    } else if(e.getSource() == clearGraph) {
        clearNameGraph();
    }
}

 /* Method: createUI() */
 /**
  * This method sets up and adds the interactors at the bottom of the window*/
private void createUI() {
    nameField = new JTextField(25); 
    nameField.setColumns(25);
    nameField.addActionListener(this);
    graphName = new JButton("Graph");
    clearGraph = new JButton("Clear");
    graph=new NameSurferGraph();
    add(new JLabel("Name"), SOUTH);
    add(nameField, SOUTH);
    add(graphName, SOUTH);
    add(clearGraph, SOUTH);
    add(graph);
    //println(db.fileEntries.size());
}

/* Method: drawNameGraph(str) */
/** Draws the graph of the name entered in nameField
 * */
private void drawNameGraph(String str) {
    //println(str);


    NameSurferEntry entered = db.findEntry(str);
    if(entered != null) {
        //println("Graph: " + entered.toString());
        graph.addEntry(entered);
        nameField.setText("str");

    } else {
        graph.badEntry(str);
    }
    //nameField.setText("");
}

/* Method: clearNameGraph() */
private void clearNameGraph() {
    graph.clear();
}

private NameSurferDataBase db = new NameSurferDataBase(NAMES_DATA_FILE);

/**TextField where the names get entered*/
private JTextField nameField;

/**button to graph name popularity*/
private JButton graphName;

/**Clears graph*/
private JButton clearGraph;

private NameSurferGraph graph;

}

Also I'm going to try to explain my question better using images. Sorry if this don't work on your OS. Their .tiffs but I'll try to run them through image conversion later on. For some reason, stackoverflow isn't letting me post the images in question, so I'm going to try to do some links to them instead through some other site. Sorry for the inconvenience.

When I run the code, this is displayed. See the image for that here. Basically so far it works as expected.

The problem arises here. The getters and setters are working, but I'ld like to have the JTextField updated when the user enters the text, as opposed to not displaying anything that I've got entered in it.


Solution

  • Are you trying to do this?!?

    JTextField text = new JTextField();
    
    text.setText("ttttttttttttexxxt");