开发者

Updating text in a JTextField

开发者 https://www.devze.com 2023-03-14 14:09 出处:网络
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.

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() == nameFiel开发者_如何学Pythond || 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.


Are you trying to do this?!?

JTextField text = new JTextField();

text.setText("ttttttttttttexxxt");


Quoting from the the Java 6 API on JTextField:

public JTextField()

Constructs a new TextField. A default model is created, the initial string is null, and the number of columns is set to 0.

(Emphasis added.)

If you are using a default constructor, then if you have not called setColumns(int), your JTextField has a column limit of 0 (regardless of the text field's width) and therefore will refuse all input from the user. I am inferring you are having trouble entering text as a user when the program is running, rather than trouble setting the text within the program and causing it to display?

Either use a form of the constructor that has a column limit, or use setColumns to specify a nonzero maximum after construction.

If this doesn't solve the issue, please provide a code sample, especially where you are constructing and initializing your JTextField.


The text is always defaulted to black so there no need to play with anything except setText.

There are a number of things you could be asking here so.

To set the text on load simply use setText at the top of your code.

public TestFrame() {
    initComponents();
    jTextField1.setText("Hello I am text in a box");
}

You can also have it respond to an event in the following way. Example is a button click.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Text Box changes from default
   jTextField1.setText("The button was pushed!!");        
}

Notice that it is all the same, I feel like you are making it a little more complicated than it actually is.


Use a normal TextField instead of a JTextfield. According to this post that was the issue. I'm not a specialist, but I have encountered the exact same problem, and it seems to be linked to the usage of the ACM library.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号