Updating text in a JTextField - java

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.

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.

Related

JTextArea shows up but for further action on it I receive empty

I have a class "MainFrame1" that extends a JFrame and also another class that is a file chooser. Whenever I press one of the JMenuItems in MainFrame1 class, I want the file chooser to open up and load up the text of the chosen file on a JTextArea that was created in MainFrame1 class. This works perfectly fine as I created a separate class implementing an ActionListener. Now my problem is that when I press another JMenuItem I want to do something else to the text in the JTextArea. I have implemented another ActionListener for that in a different class but the problem is that the JTextArea seems to be empty when I do that although I can see the text in there. Thanks in advance.
This is how I have created the JTextArea in the MainFrame1:
showAction = new JTextArea(10,10);
showAction.setEditable(false);
showAction.setFont(new Font("Arial", Font.BOLD, 12));
add(showAction, BorderLayout.NORTH);
And this is my second ActionListener class (also, whenever the text of a file is printed in the JTextArea, the text "loaded up." will also be printed) and I always get the else branch.
public class TransformController implements ActionListener{
MainFrame1 mf;
public TransformController(MainFrame1 mf) {
this.mf = mf;
}
#Override
public void actionPerformed(ActionEvent e) {
String text = mf.showAction.getDocument().toString();
if(text.contains("loaded up.")) {
char[] charText = text.toCharArray();
Parser parser1 = new Parser(charText);
parser1.packageVisitor();
}
else {
System.out.println("Load up a Java file first!");
}
}
}
This seems to be mostly a debugging question: First, find out what's in showAction.getDocument() to see if your menu item just isn't loading it right. Then check (with an IDE or via toString()) that mf.showAction really is the same object in the two cases.
Structurally, there's nothing in Java that prevents you from having a reference to the same JTextArea in two parts of the code, and reading the text out of it for different purposes.

Java - Linking a JTextField variable with another class variable

What I want to achieve is very simple.
I have 2 classes. "SpeedingTicket" & "SpeedingTicket GUI".
Inside my GUI I have 1 textbox name txtSpeedLimit & a button.
Inside my SpeedingTicket class I have a variable "int speedingTicket".
Inside my SpeedingTicket class I also have a get & set method for "speedingTicket".
I know how to get and set text using JTextFields, but I want to be able to:
receive input from the "txtSpeedLimit", and store that value into the "txtSpeedLimit" instance variable in the "SpeedTicket" class. I can then check for validation etc when I come to adding the vehicle speed.
Maybe this isn't the most efficient way of dealing with this program. Maybe I should scrap the instance variables in SpeedingTicket, and deal with it all in the GUI.
Any advice would be hugely appreciated.
Basically what I'm trying to do is this:
class confirmHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
String val = txtSpeedLimit.getText();
int realNum = speed.getSpeedLimit() = txtSpeedLimit; < but obviously that doesn't work, but I want the textbox link to the variable.
EDIT: If we take away the GUI, all I want my program to do is the following:
Speed Limit: 50 < enterd via textfield
Speed: 60 < entered via textfield
if the speed is blah blah (ive already coded this).. then output a result to one of my labels.
I achieved this without making a GUI and making it only console based, but instead of the user typing it via the console, I want it to be typed via textfields.
THe values that are entered into the textfields should be stored in the two variables (speed and speedlimit) that are in the SpeedingTicket class.
You can update a value in:
public class SpeedingTicket {
int speedingTicket;
public SpeedingTicket() {
speedingTicket = 500;
}
public int getSpeedingTicket() {
return speedingTicket;
}
}
by:
public class SpeedingTicketGUI extends JPanel{
SpeedingTicket st;
SpeedingTicketGUI() {
st = new SpeedingTicket();
setLayout(new FlowLayout(FlowLayout.LEFT));
JTextField txtField = new JTextField(10);
txtField.setText(""+st.getSpeedingTicket());
add(txtField);
JButton btn = new JButton("Update");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setSpeedingTicket(txtField.getText());
}
});
add(btn);
}
private void setSpeedingTicket(String text) {
try {
int speedTicket = Integer.parseInt(text);
st.setSpeedingTicket(speedTicket);
System.out.println("Speeding ticket set to " +st.getSpeedingTicket());
} catch (NumberFormatException ex) {
System.out.println("Invalid value " +text);
ex.printStackTrace();
}
}
public static void main(String[] args){
JFrame frame = new JFrame("Speeding Ticket");
frame.setSize(400,100);
frame.add(new SpeedingTicketGUI());
frame.setVisible(true);
}
}
You don't need to store values in JText or any GUI componenets...
Use global static variables. For example:
public static int speed_limit;
You can access this variable from ANY method,class, etc.
There are multiple ways to do it.
You can detect textfield changes by using a DocumentListener or if you want (not recommended) by a KeyListener.
The Listener could be implemented directly by your gui class or by your other class. If you want more abstraction you could implement the DocumentListener by your gui class and create a method
public void addSpeedChangeListener(SpeedChangeListener scl) {
this.speedChangeListeners.add(scl);
}
Your SpeedChangeListener could be very simple:
public interface SpeedChangeListener {
public void speedChanged(int value);
}
Then your second class implements the SpeedChangeListener and calls addSpeedChangeListener(this) on your gui class. Inside the gui class, your document listener calls speedChanged(val) for every listener registered.
EDIT
You can also use the Button and call the speedChanged on every listener inside the actionPerformed method of the ActionListener.
I think it would be easier to use a JOptionDialog which pop ups when the button is clicked. That way you can easily get input and also validate the input straight away.

How to use a key listener to verify input to a text field

so I am trying to verify what is typed into my text field is only numeric and not alphanumeric. I've looked into formatted textboxes, but haven't been able to implement them, due to lack of understanding the tutorial oracle has up on their site. I did some more searching and found the key listener class and it seemed like the best option.
So when I run the program and type into the text field instead of consuming the character event it just beeps at me. Here's the code:
private void buildPanel()
{
// Create a label to display instructions.
messageLabel = new JLabel("Enter a time in seconds the object has fallen");
// Create a text field 10 characters wide
fallingTextField = new JTextField(10);
// Add a keylistener to the text field
fallingTextField.addKeyListener(new TextFieldListener());
// Create a button with the caption "Calculate"
calcButton = new JButton("Calcualte");
// Add an action listener to the button
calcButton.addActionListener(new CalcButtonListener());
//Create a JPanel object and let the panel field reference it
panel = new JPanel();
// Add the label, text field, and button components to the panel
panel.add(messageLabel);
panel.add(fallingTextField);
panel.add(calcButton);
}
/**
The TextFieldListener class checks to see if a valid key input is typed into
the text field.
*/
private class TextFieldListener implements KeyListener
{
/**
The keyPressed method
#param evt is a key event that verifies a number is inputed otherwise
the event is consumed.
*/
public void keyPressed(KeyEvent evt)
{
char c = evt.getKeyChar();
if(Character.isAlphabetic(c))
{
getToolkit().beep();
evt.consume();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent ev)
{
}
}
Here:
if(Character.isAlphabetic(c))
{
getToolkit().beep();
You tell your listener to beep every time you enter an alphabetic character. So it beeps when you do that.
Everything working exactly as the code implies it should.
For the other part of your question; check out the Javadoc for consume() (which is inherited from InputEvent).
Consumes this event so that it will not be processed in the default manner by the source which originated it.
You are not on the source side of things. The event has already been created, and dispatched to listeners. Calling consume() in your context ... simply doesn't do anything any more. It is more or less a "no op".
Again: you wrote code that waits for keyboard input, and when the input is alphabetic, it beeps. That is all that your code does. I assume if you provide a "/" for example ... nothing should happen. If you want to validate, then you need a "feedback" loop; for example your listener could overwrite the text field content when it detects bad input.
I think the problem is that the character has already been added by the time the event has been processed. You need to set the value of the field without the last character, like:
fallingTextField.setValue(fallingTextField.getValue().substring(0, fallingTextField.getValue().length - 1))
Alternatively, you can use the example at Oracle site and use a Mask input, like the following:
zipField = new JFormattedTextField(
createFormatter("#####"));
...
protected MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}

FocusListener on JTextField not working

I've created an application that uses FocusListener to make sure a text fieid's value is always positive. When the user inputs negative value and then click the "tab" key to move focus away from the text field, the value will be multiplied by -1 so that the resulted value is positive. However, when I ran the application, the text field didn't change. I am not sure what I did wrong, and will appreciate any help.
Here is my code:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class AlwaysPositive extends JFrame implements FocusListener {
JTextField posField = new JTextField("30",5);
public AlwaysPositive() {
super("AlwaysPositive");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
JTextField posField = new JTextField("30",5);
JButton ok= new JButton("ok");
posField.addFocusListener(this);
pane.add(posField);
pane.add(ok);
add(pane);
setVisible(true);
}
public void focusLost(FocusEvent event) {
try {
float pos = Float.parseFloat(posField.getText());
if (pos < 0)
pos = pos*-1;
posField.setText("" + pos);
} catch (NumberFormatException nfe) {
posField.setText("0");
}
}
public void focusGained(FocusEvent event) {
}
public static void main(String[] arguments) {
AlwaysPositive ap = new AlwaysPositive();
}
}
The main problem is you are shadowing your variables
You declare
JTextField posField = new JTextField("30",5);
As an instance variable, but in your constructor, you redeclare it again...
public AlwaysPositive() {
//...
JTextField posField = new JTextField("30",5);
posField.addFocusListener(this);
//...
}
Add attach the focus listener to it, but in the focusLost method, you are referring to the instance variable, which isn't the one that is actually on the screen
Start by changing the declaration within the constructor
public AlwaysPositive() {
//...
posField = new JTextField("30",5);
posField.addFocusListener(this);
//...
}
However, there are better solutions to use then FocusListener.
For example, you could use an InputVerifier that will allow you to verify the value of the field and make decisions about whether focus should be moved or not.
Take a look at How to Use the Focus Subsystem and Validating Input in particular
You could also use a DocumentFilter to restrict what the user can actually enter, filtering the input as the user types it. Take a look at Text Component Features and Implementing a Document Filter in particular.
You can also take a look at these examples for more ideas
When you create object of same name inside a method, the listener is set to the method object and not to the Class object.

What is an advisable design pattern for switching between GUI pages?

What I don't like about my code below is:
getters are needed for every JButton on each page
the actionPerformed method can quickly become bloated with if-else statements
So, is there a better way to control all GUI actions from a single class?
If I define an actionPerformed method within each respective page (JPanel), each page will need access to instances of the page(s) switched to, and I am trying to avoid using the Singleton pattern for each page...
Here is the code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* #author Ian A. Campbell
*
*/
public class Controller implements ActionListener {
/**
* instance variables:
*/
private Frame frame;
private OptionPage firstPage;
private FirstOptionPage firstOption;
private SecondOptionPage secondOption;
/**
*
*/
public Controller() {
// instantiating the frame here:
this.frame = new Frame();
/*
* instantiating all pages here:
*
* NOTE: passing "this" because this class
* handles the events from these pages
*/
this.firstPage = new OptionPage(this);
this.firstOption = new FirstOptionPage(this);
this.secondOption = new SecondOptionPage(this);
}
/**
*
*/
public void start() {
this.frame.add(this.firstPage); // adding the first page
// NOTE: these lines prevent blank loading and flickering pages!
this.frame.validate();
this.frame.repaint();
this.frame.setVisible(true);
}
/**
*
* #return the JFrame instantiated from the class Frame
*/
public Frame getFrame() {
return this.frame;
}
#Override
public void actionPerformed(ActionEvent e) {
// the "first option" button from the OptionPage:
if (e.getSource() == this.firstPage.getFirstButton()) {
this.frame.getContentPane().removeAll();
this.frame.getContentPane().add(this.firstOption);
// the "second option" button from the OptionPage:
} else if (e.getSource() == this.firstPage.getSecondButton()) {
this.frame.getContentPane().removeAll();
this.frame.getContentPane().add(this.secondOption);
}
// NOTE: these lines prevent blank loading and flickering pages!
this.frame.validate();
this.frame.repaint();
this.frame.setVisible(true);
}
} // end of Controller
Use a Card Layout. Card Layout Actions adds some extra features that you might find helpful.
You could use card layout, or you could get creative and remove elements. For instance:
panel.remove((JButton)myButton1)); // Remove all of the elements...
panel.add((JButton)myButton2)); // Add the new elements
Of course I wouldn't deal with the java built in GUI at all, IMO the layout designs are horrific. I would much rather use something like "A New Look and Feel" -- http://www.javootoo.com/.

Categories