Member Avatar for san_fran_crisko

Hi,

I'm trying to get Java to remove a JLabel from a JPanel whenever a button is clicked. Unfortunately it won't do this. I've tested my code and the button click is working for sure, so its the code to remove the component that is acting up. If anyone could shed any light on this it would be much appreciated. I've been stuck for hours on it!

import javax.swing.*;
import java.awt.*;
import java.io.*; 
import java.util.Vector;
import java.awt.event.*;

class MainMenu extends JFrame implements ActionListener{
    
    JPanel westPanel = new JPanel();
    JPanel menuPanel = new JPanel();
    
    JLabel titleLabel = new JLabel("CD DATABASE", JLabel.CENTER);
    JLabel defaultLabel = new JLabel("Please select an option from the menu on the left to continue", JLabel.CENTER);
    
    JButton addButton = new JButton("Add New CD Details");
    JButton artistButton = new JButton("Search for an Artist");
    JButton genreButton = new JButton("Search for a Genre");
    JButton yearButton = new JButton("Search for a Year");
    JButton exitButton = new JButton("Exit");
    
    
    public MainMenu() {
        
        super("CD System ver 2.0");
        setSize(600,350);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        
        menuPanel.setLayout(new BorderLayout());
        
        
        westPanel.setLayout(new GridLayout(5,0));
        menuPanel.add("West", westPanel);
        
        
        
        defaultLabel.setFont(new Font("Serif", Font.BOLD, 14));
        menuPanel.add("Center", defaultLabel);
        
        
        titleLabel.setPreferredSize(new Dimension (200, 50));
        titleLabel.setFont(new Font("Serif", Font.BOLD, 24));
        menuPanel.add("North", titleLabel);
        
        
        addButton.addActionListener(this);
        westPanel.add(addButton);
        
        artistButton.addActionListener(this);
        westPanel.add(artistButton);
        
        genreButton.addActionListener(this);
        westPanel.add(genreButton);
        
        yearButton.addActionListener(this);
        westPanel.add(yearButton);
        
        exitButton.addActionListener(this);
        westPanel.add(exitButton);
        
        
        setContentPane(menuPanel);
    
        setVisible(true); }
            
    public void actionPerformed(ActionEvent event) {
        
        if (event.getSource() == exitButton) {
            System.exit(0);}
        if (event.getSource() == addButton) {
            menuPanel.remove(defaultLabel); }
        }
        
        
}
    

public class CDsystemv2 {

    static Vector vCDDatabase = new Vector();
        
    public static void main (String[] args) {

        MainMenu start = new MainMenu();
    }
}

Why don't you just set the label to setVisible(false) in your action performed.
Hope this helps

Member Avatar for san_fran_crisko

Thanks for replying fowler. I didn't realise you could set individual components to visible true/false. I'll give that a shot.

What I'm looking to do it when someone clicks on a button, it replaces the default JLabel when the program starts and will replace it with the appropriate components for the selected task.

You did not add the listeners to the button !

There is no problem with your code. You just need to call the this.repaint(); after removing the component.

Suppose the JFrame variable is frame. And there are two JPanel p1, p2. p1 is already added to frame. When button is clicked you want to remove p1 and add p2. Then you should do something like this:

frame.remove(p1);
frame.add(p2);
frame.validate();

Hope this might help.

- Mohammed Homam

next time check the timestamp before responding. This was handled 2+ years ago.

@hOmAm you just replied 3 years old thread. The problem is either already solved or long forgotten. Please do not reopen old threads in the future.

Thread closed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.