I want to program something that if I click the button it will remove the JTextfield
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.
I want to program something that if I click the button it will remove the 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!
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!
I'm not using a container my setLayout is null so that I could use the setBounds
the remove method only disable the textfield
...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!
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.
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!
A4Andy (August 31st, 2011)
Consider this as solved as I used hide() method instead. Thanks for your help anyways.
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!