Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: how to delete a JTextField?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default how to delete a JTextField?

    I want to program something that if I click the button it will remove the JTextfield


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: how to delete a JTextField?

    There's a remove method in the Container class. Didn't you google this or look at the API first?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to delete a JTextField?

    I did try

    remove(txtfieldname);

    but it didn't work, it was not able to remove the textfield

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: how to delete a JTextField?

    Quote Originally Posted by A4Andy View Post
    I did try

    remove(txtfieldname);

    but it didn't work, it was not able to remove the textfield
    Sigh. What about it didn't work? Where's your SSCCE? Did you validate the Container after making the change?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to delete a JTextField?

    I'm not using a container my setLayout is null so that I could use the setBounds

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to delete a JTextField?

    the remove method only disable the textfield

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: how to delete a JTextField?

    ...what? What are you adding/removing from if not a Container? What do you mean by "remove method only disable the textfield"?

    If you want help, you'll have to provide an SSCCE, as I don't really understand anything you're saying.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to delete a JTextField?

    here is my codes

    import java.awt.event.*;
    import javax.swing.*;
     
    public class test extends JFrame {	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JLabel lblCount,lblCount2;
    	public JTextField txtQTY[] = new JTextField[11];
    	private JTextField txtCName; 
    	private JButton addButton, remButton;
     
    	public test()
    	{
    		super("Test");
    		setLayout (null);
     
    		lblCount= new JLabel("80");
    		lblCount.setBounds(700, 80,100, 20);
    		lblCount.setVisible(false);
    		add(lblCount);
    		lblCount2= new JLabel("0");
    		lblCount2.setBounds(700, 100,100, 20);
    		lblCount2.setVisible(false);
    		add(lblCount2);
     
    		txtCName = new JTextField("");
    		txtCName.setBounds(150, 10,100, 20);
    		txtCName.setHorizontalAlignment(JTextField.RIGHT);
    		add(txtCName);
     
    		addButton = new JButton("+");
    		addButton.setBounds(50, 80, 45, 20);
    		add(addButton);
     
    		HandlerClass1 handler1 = new HandlerClass1();
    		addButton.addActionListener(handler1);
     
    		remButton = new JButton("-");
    		remButton.setBounds(50, 105, 45, 20);
    		add(remButton);
    		HandlerClass2 handler2 = new HandlerClass2();
    		remButton.addActionListener(handler2);
     
     
    	}
     
    		 public class HandlerClass1 implements ActionListener
    		{
    		 	public void actionPerformed (ActionEvent event)
    			{	
    		 		String a = lblCount2.getText();
    		 		int b = Integer.parseInt(a);
    		 		int c = b+1;
    		 		String x = lblCount.getText();
    		 		int y = Integer.parseInt(x);
    		 		if(b!=10)
    		 		{
    		 		//
    		 		//Quantity
    		 		//
    				txtQTY[c] = new JTextField(" ");
    				txtQTY[c].setBounds(100, y,50, 20);
    				txtQTY[c].setHorizontalAlignment(JTextField.RIGHT);
    				add(txtQTY[c]);								
     
    				y = y+30;
    				b=b+1;
    				String z=Integer.toString(y);
    				String d=Integer.toString(b);
    				lblCount.setText(z);
    				lblCount2.setText(d);
    		 		}
    		 		else
    		 		{
    		 			JOptionPane.showMessageDialog(null, "max");
    		 		}
    			}
     
    		}	 
    	 public class HandlerClass2 implements ActionListener
    		{			
    			public void actionPerformed (ActionEvent event)
    			{
    				remove(txtCName);
    			}
    		}
    }

    if a user click the "+" button it would add 10 JTextField

    and the "-" JTextField (txtCName)

    the "+" button works fine now but the "-" is my problem
    Last edited by A4Andy; August 31st, 2011 at 10:46 AM.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: how to delete a JTextField?

    I can't run that because it's not an SSCCE. But where do you validate your Container after removing the component?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. The Following User Says Thank You to KevinWorkman For This Useful Post:

    A4Andy (August 31st, 2011)

  11. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to delete a JTextField?

    Consider this as solved as I used hide() method instead. Thanks for your help anyways.

  12. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: how to delete a JTextField?

    That method is deprecated- you're supposed to use setVisible() instead. Keep in mind that simply making the component invisible doesn't actually remove it, so what you're doing is causing a possible memory leak, especially if you plan on doing this with a bunch of components.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Jar self delete on exit?
    By KiwiProg in forum Java Theory & Questions
    Replies: 1
    Last Post: December 19th, 2010, 01:54 AM
  2. Can I delete one of these two?
    By ice in forum Java IDEs
    Replies: 2
    Last Post: November 14th, 2010, 03:02 AM
  3. How to delete data from php through GUI in Java?
    By BluXit in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2010, 09:41 AM
  4. How to move an image (or how to delete one)
    By User in forum AWT / Java Swing
    Replies: 3
    Last Post: December 17th, 2009, 10:25 AM
  5. [SOLVED] how to delete an item from a form
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 30th, 2009, 01:06 PM