Results 1 to 5 of 5
  1. #1
    cyentw is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default How to remove a JPanel then add another one in, properly?

    I'll make it quick. Here's my code:

    Java Code:
    public void actionPerformed(ActionEvent e) {
    		if(e.getSource()==button)
    		{
    					panel.remove(panel3);
    					panel.add(panel2);
    					panel.revalidate();
    	                                panel.repaint();
    	                                pack();
    		}
    But, when I click the button, panel3 stays and panel2 is nowhere to be found.

    I messed around with some JComponent methods, and found that panel2 did actually end up being added, but panel3 was still there. (I used panel.getComponentCount())
    Also, the code does run when the button is pressed. (Checked by having it print out "button" to the console when pressed.).


    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: How to remove a JPanel then add another one in, properly?

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: How to remove a JPanel then add another one in, properly?

    What type of layout does your 'panel' JPanel have? I would look into the CardLayout if you're going to be frequently switching back and forth between different options, it may work for you.

  4. #4
    cyentw is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Re: How to remove a JPanel then add another one in, properly?

    Ok, heres all of the necessary code.
    Java Code:
    package cyentw.utility.cutilities;
    
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Scanner;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Launch extends JFrame implements ActionListener{
    
    	private JFrame frame;
    	private JButton button;
    	private JPanel panel;
    	private JPanel panel2;
    	private JPanel panel3 = new Panel3();
    	private JPanel panel4 =  new Panel4();
    
    	public static void main(String[] args) {
    		new Launch();
    	}
    	public Launch()
    	{
    		
    		frame = new JFrame();
    		panel = new JPanel();
    		panel2 = new JPanel();
    		button = new JButton("button.");
    
    		frame.setSize(500,500);
    		frame.setLocationRelativeTo(null);
    		frame.setTitle("CUtilities");
    		frame.setName("CUtilities");
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    		panel.setSize(500,500);
    		panel.setLayout(null);
    		frame.add(panel);
    
    		panel2.setSize(350,500);
    		panel2.setLayout(null);
    		panel2.setLocation(160,0);
    		panel2.setBackground(Color.white);
    		panel.add(panel2);
    
    		button.setSize(150,50);
    		button.setLocation(5, 15);
    		button.addActionListener(this);
    		panel.add(button);
    		
    		panel2.add(panel4);
    
    		frame.setVisible(true);
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource()==give)
    		{
    					panel2.remove(panel4);
    					panel2.add(panel3);
    					panel2.revalidate();
    	                panel2.repaint();
    	                pack();
    		}
    	}
    }
    
    //CLASSES PANEL3 AND PANEL4 BOTH EXTEND JPANEL, HAVE A LAYOUTMANAGER OF NULL, HAVE LOCATION 0,0 AND SIZE 350,500, HAVE 2 COMPONENTS EACH, AND ARE SET TO VISIBLE.
    Hope this helps :)

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: How to remove a JPanel then add another one in, properly?

    What is supposed to happen when the code is executed and the button pressed?

    I'm looking at the code and don't see any comments describing what it is supposed to do.

    BTW The posted code does not compile without errors. Please fix the compiler errors.
    Last edited by Norm; 12-30-2013 at 02:01 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 12
    Last Post: 12-23-2012, 08:11 PM
  2. Remove A JPanel From A Separate Class
    By anthropamorphic in forum New To Java
    Replies: 5
    Last Post: 09-10-2011, 06:43 AM
  3. How do i remove images from a Jpanel?
    By rajkobie in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 02:44 PM
  4. JPanel add/remove in a JFrame
    By Aldane in forum AWT / Swing
    Replies: 2
    Last Post: 03-28-2011, 07:28 PM
  5. how to remove all component in container(Jpanel)
    By SaYuNaRa in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 04:22 AM

Posting Permissions

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