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");

        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);
        setVisible(true); }
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == exitButton) {
        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();

Recommended Answers

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

Jump to Post

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

Jump to Post

All 7 Replies

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

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:


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 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.