Click here to Skip to main content
15,356,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there is a point in apps that after user clicks the "ok" button or something, there will be no need to the text fields, text areas and the buttons themselves; how to remove or change them after a button is clicked in a jframe?

they should probably all use same method for removing and same for chaging.

public static void main(String[] args) {
    	

    	
    	JFrame f = new JFrame("text");
    	
       f.getContentPane().setBackground(Color.BLACK);
 	   f.setSize(290,220);
 	   f.setLocation(735,550);
 	   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 	   JTextArea area=new JTextArea("some text");
       area.setBounds(0,0, 280,100);
       JLabel l1,l2;  
       l1=new JLabel("text");  
       l1.setBounds(0,107, 55,25);  
       l2=new JLabel("text");  
       l2.setBounds(140,107, 55,25); 
       JTextField t1,t2;  
       t1=new JTextField();  
       t1.setBounds(55,107, 75,25);
       t2=new JTextField();  
       t2.setBounds(195,107, 75,25); 
       JButton b = new JButton("text");
       b.setBounds(82, 142, 100, 33);

       f.add(b);
       f.add(t1); f.add(t2);  
       f.add(area);
       f.add(l1); f.add(l2);  
 	   f.setLayout(null);
 	   f.setVisible(true);

       b.addActionListener(new ActionListener(){  
    	   public void actionPerformed(ActionEvent e){  
    	               
    	       String UN = t1. getText();
    	       String PW = t2. getText();
    		   // here before going further, jlable, jbutton, jtextfield are going
               //to get deleted and jtextarea is going to be updated

........................
    		        
    	       });  


What I have tried:

there was nothing on the net or at least wasnt easy to find.
Posted
Updated 23-Mar-22 7:49am
v3
Comments
Richard MacCutchan 23-Mar-22 11:54am
   
You just need to create a method that does what is needed. But we have no idea what your code is doing so it is impossible to give a specific answer.
T1xT 23-Mar-22 12:19pm
   
question updated
Richard MacCutchan 23-Mar-22 12:55pm
   
The structure of your code does not make it easy since everything is local to main. You need to look at some of the examples at Java Tutorials Learning Paths[^].
T1xT 23-Mar-22 13:49pm
   
well there's an easy way; check the solution below -

1 solution

well found the solution finally:

to eliminate jcomponents:

Container parentobject = object.getParent();
    		   parentobject.remove(object);
    		   parentobject.validate();
    		   parentobject.repaint();


and to update the jtextarea:

areaobject.setText("new text");



so the code will be:


public static void main(String[] args) {
    	

    	
    	JFrame f = new JFrame("text");
    	
       f.getContentPane().setBackground(Color.BLACK);
 	   f.setSize(290,220);
 	   f.setLocation(735,550);
 	   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 	   JTextArea area=new JTextArea("some text");
       area.setBounds(0,0, 280,100);
       JLabel l1,l2;  
       l1=new JLabel("text");  
       l1.setBounds(0,107, 55,25);  
       l2=new JLabel("text");  
       l2.setBounds(140,107, 55,25); 
       JTextField t1,t2;  
       t1=new JTextField();  
       t1.setBounds(55,107, 75,25);
       t2=new JTextField();  
       t2.setBounds(195,107, 75,25); 
       JButton b = new JButton("text");
       b.setBounds(82, 142, 100, 33);

       f.add(b);
       f.add(t1); f.add(t2);  
       f.add(area);
       f.add(l1); f.add(l2);  
 	   f.setLayout(null);
 	   f.setVisible(true);

       b.addActionListener(new ActionListener(){  
    	   public void actionPerformed(ActionEvent e){  
    	               
    	       String UN = t1. getText();
    	       String PW = t2. getText();

               Container parentl1 = l1.getParent();
    		   parentl1.remove(l1);
    		   parentl1.validate();
    		   parentl1.repaint();
    		   
    		   Container parentl2 = l2.getParent();
    		   parentl2.remove(l2);
    		   parentl2.validate();
    		   parentl2.repaint();
    		   
    		   Container parentt1 = t1.getParent();
    		   parentt1.remove(t1);
    		   parentt1.validate();
    		   parentt1.repaint();
    		   
    		   Container parentt2 = t2.getParent();
    		   parentt2.remove(t2);
    		   parentt2.validate();
    		   parentt2.repaint();
    		   
    		   Container parentb = b.getParent();
    		   parentb.remove(b);
    		   parentb.validate();
    		   parentb.repaint();
    		   
    		   area.setText("new text"); ................
    	       });
   
v2
Comments
Richard MacCutchan 24-Mar-22 3:46am
   
You do not need all those calls to getParent, since you already know that the controls are in JFrame f. And you only need to call validate and repaint once, when they have all been removed.
T1xT 24-Mar-22 7:00am
   
yes, this works out too:

f.remove(l1);
f.remove(l2);
f.remove(t1);
f.remove(t2);
f.remove(b);
f.validate();
f.repaint();<pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900