Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Using setText() to update the displayed string in a JTextField

  1. #1
    ayershov777 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Using setText() to update the displayed string in a JTextField

    I have tried using

    Java Code:
    public class Listener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource() == reset)
    			{
    				for(int i = 0; i<8; i++) fp.Text[i].setText("Invalid Input");
    				for(int i = 0; i<8; i++) System.out.println(fp.Text[i].getText());
    			}
    		}
    	}
    The code prints out "invalid input" like it should, but the textFields all remain as they were. What am I doing wrong? Would posting the rest of my code help? Everything else works fine I think.

    fp.Text[i] are all of my textFields

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using setText() to update the displayed string in a JTextField

    What are you doing wrong; I can detect a few things:
    • I wonder if "fp" is a class and not a variable that refers to a class. If so, please remember that class names should all begin with an upper-case letter, and field, variable, and method names lower case. This may seem like a minor quibble but it becomes very important when you ask others to read over and interpret your code.
    • You appear to be using static fields in your fp class. If so, don't. Make all of your fields private and instance fields, and reserve use of static fields for situations where this makes sense (it doesn't here).
    • All of your class's fields should be private with few exceptions. You should limit interaction with them through public methods, and these only when necessary.
    • Again, you should give your GUI class public methods that allow other classes to change its state, or "setter" methods. For instance you could give your GUI class above a setText(int index, String text) method that allows you to set the i'th text field.
    • Yes, for more help, you'll have to tell us and show us more.


    Best of luck!

  3. #3
    ayershov777 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: Using setText() to update the displayed string in a JTextField

    Thanks for the help man but none of those points that you mentioned seem to be the problem. I will post the entire code.

    Java Code:
    import javax.swing.JFrame;
    import java.awt.*;
    
    public class TravelExpenses
    {
    	public static void main(String[] args)
    	{
    		JFrame travelFrame = new JFrame();
    		FieldsPanel fields = new FieldsPanel();
    		ButtonPanel buttons = new ButtonPanel();
    		
    		travelFrame.setTitle("Travel Expenses");
    		travelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		travelFrame.setLayout(new BorderLayout());
    		travelFrame.add(fields,BorderLayout.NORTH);
    		travelFrame.add(buttons,BorderLayout.SOUTH);
    		travelFrame.pack();
    		travelFrame.setVisible(true);
    	}
    }
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class FieldsPanel extends JPanel
    {
    	private JLabel[] Label = new JLabel[8];
    	public JTextField[] Text = new JTextField[8];
    	
    	public FieldsPanel()
    	{
    		setLayout(new GridLayout(8,2));
    		
    		Label[0] = new JLabel("Number of days on the trip:");
    		Label[1] = new JLabel("Amount of airfare:");
    		Label[2] = new JLabel("Amount of car rental:");
    		Label[3] = new JLabel("Miles driven:");
    		Label[4] = new JLabel("Parking fees:");
    		Label[5] = new JLabel("Taxi fees:");
    		Label[6] = new JLabel("Conference registration:");
    		Label[7] = new JLabel("Lodging charges per night:");
    		
    		Listener listener = new Listener();
    		
    		for(int i = 0; i<8; i++)
    		{
    			Text[i] = new JTextField("0",15);
    			add(Label[i]);
    			add(Text[i]);
    			Text[i].addActionListener(listener);
    		}
    	}
    	
    	public class Listener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource() == Text[0]) Text[1].getCursor();
    		}
    	}
    }
    This is where the text field should be updated

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class ButtonPanel extends JPanel
    {
    	private FieldsPanel fp = new FieldsPanel();
    	private JButton calculate;
    	private JButton reset;
    	public ButtonPanel()
    	{
    		calculate = new JButton("Calculate");
    		reset = new JButton("Reset");
    		
    		Listener listener = new Listener();
    		calculate.addActionListener(listener);
    		reset.addActionListener(listener);
    		
    		add(calculate);
    		add(reset);
    	}
    	
    	public class Listener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource() == reset)
    			{
    				for(int i = 0; i<8; i++) fp.Text[i].setText("Invalid Input");
    				for(int i = 0; i<8; i++) System.out.println(fp.Text[i].getText());
    				for(int i = 0; i<8; i++) repaint();
    			}
    		}
    	}
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Using setText() to update the displayed string in a JTextField

    For a clue to what is happening, add this to the FieldsPanel class's constructor:
    Java Code:
          System.out.println("FP Constructor"); //<<<<<<<<<<
    How many times will it print?
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using setText() to update the displayed string in a JTextField

    Quote Originally Posted by ayershov777 View Post
    Thanks for the help man but none of those points that you mentioned seem to be the problem.
    Sorry. It was the best I could do given the limited information that was provided.

    I will post the entire code....
    Good. Norm's point will lead you to find the solution. Best of luck!

Similar Threads

  1. how do you update JTextField?
    By ayershov777 in forum New To Java
    Replies: 1
    Last Post: 09-21-2012, 02:55 AM
  2. JTextField setText problem
    By LancePaxton in forum AWT / Swing
    Replies: 6
    Last Post: 09-09-2012, 11:14 AM
  3. JTextField won't update
    By multarnc in forum New To Java
    Replies: 6
    Last Post: 01-11-2012, 02:07 PM
  4. can't get JTextField to update
    By rippon in forum AWT / Swing
    Replies: 4
    Last Post: 11-29-2010, 12:34 PM
  5. JTextField won't update from subclass
    By Edward in forum New To Java
    Replies: 9
    Last Post: 06-29-2009, 05:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •