Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By doWhile
  • 1 Post By HelloWorld1234

Thread: change the text of a JTextField

  1. #1
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default change the text of a JTextField

    how can I change the text of a JTextField in a method?
    I have tried this but
    all the fields except field9 is also uneditable.
    Java Code:
    protected void ButtonClick() {
    		field1 = field2;
    		field2 = field3;
    		field3 = field4;
    		field4 = field5;
    		field5 = field6;
    		field6 = field7;
    		field7 = field8;
    		field8 = field9;
    		field9 = null;
    		System.out.println("You Changed the text");
    	}
    but it did NOT work... someone plz help me here?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: change the text of a JTextField

    See the API for JTextField - the setText method should accomplish what you need.
    Fubarable likes this.

  3. #3
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: change the text of a JTextField

    When I tried this:
    Java Code:
    protected void ButtonClick() {
    		field1.setText(field2.toString());
    		field2.setText(field3.toString());
    		field3.setText(field4.toString());
    		field4.setText(field5.toString());
    		field5.setText(field6.toString());
    		field6.setText(field7.toString());
    		field7.setText(field8.toString());
    		field8.setText(field9.toString());
    		field9.setText("");
    		System.out.println("You Changed the text");
    	}
    it made all the TextFields this:
    javax.swing.JTextField[,41,208,224x20,layout=javax.swing.plaf.basic.Basic TextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0 ,border=javax.swing.plaf.BorderUIResource$Compound BorderUIResource@33319df3,flags=296,maximumSize=,m inimumSize=,preferredSize=,caretColor=sun.swing.Pr intColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResourc e[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIRes ource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=20,columnWidth=11,command=,horizontalAlig nment=LEADING]

  4. #4
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: change the text of a JTextField

    I made it work!
    Java Code:
    protected void ButtonClick() {
    		field1.setText(field2.getText());
    		field2.setText(field3.getText());
    		field3.setText(field4.getText());
    		field4.setText(field5.getText());
    		field5.setText(field6.getText());
    		field6.setText(field7.getText());
    		field7.setText(field8.getText());
    		field8.setText(field9.getText());
    		field9.setText("sup?");
    		System.out.println("You Changed the text");
    	}
    Fubarable likes this.

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

    Default Re: change the text of a JTextField

    Great, now consider using arrays as this would greatly simplify your code and make it easier to debug.

  6. #6
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: change the text of a JTextField

    Can you help me with using arrays for this?
    Java Code:
    Field1 = new JTextField("this is field 1", 20);
    Field2 = new JTextField("this is field 2", 20);
    Field3 = new JTextField("this is field 3", 20);
    Field4 = new JTextField("this is field 4", 20);
    Field5 = new JTextField("this is field 5", 20);
    Field6 = new JTextField("this is field 6", 20);
    Field7 = new JTextField("this is field 7", 20);
    Field8 = new JTextField("this is field 8", 20);
    Field9 = new JTextField("this is field 9", 20);

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

    Default Re: change the text of a JTextField

    Quote Originally Posted by HelloWorld1234 View Post
    Can you help me with using arrays for this?
    Java Code:
    Field1 = new JTextField("this is field 1", 20);
    Field2 = new JTextField("this is field 2", 20);
    Field3 = new JTextField("this is field 3", 20);
    Field4 = new JTextField("this is field 4", 20);
    Field5 = new JTextField("this is field 5", 20);
    Field6 = new JTextField("this is field 6", 20);
    Field7 = new JTextField("this is field 7", 20);
    Field8 = new JTextField("this is field 8", 20);
    Field9 = new JTextField("this is field 9", 20);
    I'll bet you could solve this without our help, or with only minor help, so why don't you give it a try first. Then if your code attempt has problems, post it with the errors.

  8. #8
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: change the text of a JTextField

    Java Code:
    for(int i=1;i>=fields.length;i++){
    				fields[i] = new JTextField("this is field " + i, 20);
    			}
    I was sure this should've worked, but it gives me thousands of errors I don't understand
    I post it for you if you want

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

    Default Re: change the text of a JTextField

    Quote Originally Posted by HelloWorld1234 View Post
    Java Code:
    for(int i=1;i>=fields.length;i++){
    				fields[i] = new JTextField("this is field " + i, 20);
    			}
    I was sure this should've worked, but it gives me thousands of errors I don't understand
    I post it for you if you want
    You will need to show more of your code, including where you declare the array. Also, show the error messages.

  10. #10
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: change the text of a JTextField

    here is the decleration of the array and the items in the array:
    Java Code:
    private JTextField Field1;
    private JTextField Field2;
    	private JTextField Field3;
    	private JTextField Field4;
    	private JTextField Field5;
    	private JTextField Field6;
    	private JTextField Field7;
    	private JTextField Field8;
    	private JTextField Field9;
    	private JTextField fields[] = {Field1, Field2, Field3 , Field4, Field5, Field6, Field7, Field8};
    here is the method, both with the array and the basic method as comment:
    Java Code:
    if(!defined){
    			/*Field1 = new JTextField("this is field 1", 35);
    			Field2 = new JTextField("this is field 2", 35);
    			Field3 = new JTextField("this is field 3", 35);
    			Field4 = new JTextField("this is field 4", 35);
    			Field5 = new JTextField("this is field 5", 35);
    			Field6 = new JTextField("this is field 6", 35);
    			Field7 = new JTextField("this is field 7", 35);
    			Field8 = new JTextField("this is field 8", 35);
    			Field9 = new JTextField("this is field 9", 45);*/
    			for(int i=1;i>=fields.length;i++){
    				fields[i] = new JTextField("this is field " + i, 20);
    			}
    			Field9 = new JTextField("this is field 9", 20);
    			
    			defined = true;
    		}
    and finally, the error:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.ole.GuiTesting.Gui.paintComponent(Gui.java:54)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unkn own Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run( Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

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

    Default Re: change the text of a JTextField

    Quote Originally Posted by HelloWorld1234 View Post
    here is the decleration of the array and the items in the array:
    Java Code:
    private JTextField Field1;
    private JTextField Field2;
    	private JTextField Field3;
    	private JTextField Field4;
    	private JTextField Field5;
    	private JTextField Field6;
    	private JTextField Field7;
    	private JTextField Field8;
    	private JTextField Field9;
    	private JTextField fields[] = {Field1, Field2, Field3 , Field4, Field5, Field6, Field7, Field8};
    I would simplify this even more:

    Java Code:
    // const
    public static final int FIELD_COUNT = 9;
    
    // variables
    private JTextField[] fields = new JTextField[FIELD_COUNT];
    here is the method, both with the array and the basic method as comment:
    Java Code:
    if(!defined){
    			for(int i=1;i>=fields.length;i++){
    				fields[i] = new JTextField("this is field " + i, 20);
    			}
    I'm not sure that your for loop will ever be entered, since i does not satisfy your for loop condition, i >= fields.length, at the start of the loop. Thus the entire for loop will be skipped.

    Java Code:
    Field9 = new JTextField("this is field 9", 20);[/quote]
    This should be included in the for loop initialization.


    and finally, the error:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.ole.GuiTesting.Gui.paintComponent(Gui.java:54)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    In the future, you'll want to show us the line that causes the error, but I suspect that it's due to your for loop never having been entered, and the JTextFields held by the array never having been initialized.

  12. #12
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: change the text of a JTextField

    I would never have thought of anything like that... but I'll try something like this, thx!

  13. #13
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: change the text of a JTextField

    So i've tried this, but it gave me an error.

    Java Code:
    package com.ole.GuiTesting;
    
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Gui extends JPanel{
    	private static final int NUMBER_OF_JTEXTFIELDS = 9;
    	private JTextField[] fields = new JTextField[NUMBER_OF_JTEXTFIELDS];
    	private JButton Button;
    	private JButton QButton;
    	
    	private boolean buttons = false;
    	private boolean defined = false;
    	private boolean setEditableity = false;
    	private boolean added = false;
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		
    		if(!defined){
    			for(int i=0;i<=fields.length;i++){
    				 fields[i] = new JTextField("this is field " + i, 20);
    			}
    			
    			defined = true;
    		}
    		
    		//set the editableity
    		if(!setEditableity){
    			for(int i=0;i<=fields.length;i++){
    				fields[i].setEditable(false);
    			}
    			
    			setEditableity = true;
    		}
    		
    		//add the fields
    		if(!added){
    			for(int i=0;i<=fields.length;i++){
    				add(fields[i]);
    			}
    				
    			added = true;
    		}
    		
    		//define and add the buttons
    		if(!buttons){
    			Button = new JButton("Enter");
    			Button.setToolTipText("enter the text");
    			Button.setBounds(300, 500, 50, 50);
    			Button.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					System.exit(0);
    				}
    			});
    			add(Button);
    			
    			QButton = new JButton("Quit");
    			QButton.setToolTipText("Quit");
    			QButton.setBounds(350, 550, 100, 40);
    			QButton.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					System.exit(0);
    				}
    			});
    			add(QButton);
    			
    			buttons = true;
    		}
    	}
    
    }
    here is the error I get:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
    at com.ole.GuiTesting.Gui.paintComponent(Gui.java:27)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unkn own Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run( Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

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

    Default Re: change the text of a JTextField

    Immediate concern: you're doing much within a paintComponent method that should not be done. Please note that this method is for painting only and should not be used for creating objects if possible, and definitely should not be used for layout out components. If this method is slowed down for any reason, your GUI responsiveness will slow down in kind. Also, you do not have complete control over when or even if this method will be called. Next, it is often called *many* times during your program's run, resulting in unnecessary and redundant creation of GUI components.

    So create your GUI components once and only once in the class's constructor or in some initComponents method that you've created.

    Next: fix your naming conventions. Methods and fields should all begin with a lower-case letter, and classes with an upper-case letter.

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

    Default Re: change the text of a JTextField

    Quote Originally Posted by HelloWorld1234 View Post
    So i've tried this, but it gave me an error.

    here is the error I get:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
    at com.ole.GuiTesting.Gui.paintComponent(Gui.java:27)
    at javax.swing.JComponent.paint(Unknown Source)
    As for the error, the problem is here:

    Java Code:
             for (int i = 0; i <= fields.length; i++) {
                fields[i] = new JTextField("this is field " + i, 20);
             }
    Arrays are 0 based. So an array of JTextField initialized to 9: fields = new JTextField[9], will have 9 items, going from fields[0] to fields[8]. You should change your for loop condition to i < fields.length, not i <= fields.length. If you want your field text to be 1 based and not 0 based, you can do the math:

    Java Code:
             for (int i = 0; i < fields.length; i++) { // note the difference!!
                fields[i] = new JTextField("this is field " + (i + 1), 20);
             }
    I

Similar Threads

  1. Changing the text in a JTextField
    By mpankhurst in forum New To Java
    Replies: 5
    Last Post: 12-31-2011, 05:52 AM
  2. Remember the text in JTextField
    By seredi in forum New To Java
    Replies: 11
    Last Post: 09-21-2011, 06:55 PM
  3. Dynamically input text JTextField. Is it possible?
    By africanhacker in forum New To Java
    Replies: 2
    Last Post: 03-21-2011, 04:23 PM
  4. Reaplacing text in JTextField
    By PhQ in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 12:25 AM
  5. Change JTextField Font?
    By Krooger in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2010, 05:50 PM

Posting Permissions

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