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();
}
}