I want to program something that if I click the button it will remove the JTextfield
Printable View
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?
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?
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.
here is my codes
Code :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
I can't run that because it's not an SSCCE. But where do you validate your Container after removing the component?
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.