I'm trying to remove a JButton from a JPanel but my code does not work. Any help is appreciated.

This is the JButton. Problem is the line 25 I guess.

class SquareButton extends JButton implements MouseListener {
	
	String	text = null;
	GameArea ga = null;
	
	public SquareButton(GameArea ga) {
		this.ga = ga;
		setPreferredSize(new Dimension(30, 30));
		setOpaque(false);
		addMouseListener(this);
		setFont(new Font("Verdana", Font.BOLD, 15));
		setHorizontalAlignment(CENTER);
		setVerticalAlignment(CENTER);
	}
	
	public Insets getInsets() {
		return new Insets(0,0,0,0);
	}
	
	public void mouseClicked(MouseEvent e) {
		if(e.getButton() == 3) {
			changeIcon();
		}
		else if(e.getButton() == 1) {
			ga.remove(this);
			ga.add(new SquareLabel());
			revalidate();
		}
	}

	private void changeIcon() {
		if(text == null) {
			text = "!";
			setText(text);
		} else {
			text = null;
			setText("");
		}
	}
	
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mousePressed(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}
	
}

Part of the GameArea:

public GameArea(int gameWidth, int gameHeight, int mines) {
		squares = new SquareButton[gameWidth][gameHeight];
		mineField = new boolean[gameWidth][gameHeight];
		hintNumbers = new int[gameWidth][gameHeight];
		
		setLayout(new BorderLayout(0, 0));
		status = new JPanel();
		gameArea = new JPanel();
		gameArea.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
		add(status,BorderLayout.NORTH);
		add(gameArea,BorderLayout.CENTER);
		setSize(300, 330);
		setVisible(true);
		
		for(int i = 0; i < gameWidth; i++) {
			for(int j = 0; j < gameHeight; j++) {
				squares[i][j] = new SquareButton(this);
				gameArea.add(squares[i][j]);
			}
		}
		
		randMines(mines, gameWidth, gameHeight);
		calculateHints(mineField, gameWidth, gameHeight);
		
	}

Edited 5 Years Ago by matadori: n/a

you can use the


jpanel.remove(component);

Statement and it will remove the component from the JPanel.

Are you certain the statement on line 25 is being executed? (it looks OK to me). It may be a problem with your button numbers; you test for 3 and 1, but here's what the API Java|Doc has to say about getButton()...

The values for button range from zero to indicate the left button of the mouse, one to indicate the middle button if present, and two to indicate the right button.

if TopLayoutContainer http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html was already visible

1/ if you want/need to add JComponent(s) to the Container (JPanel, JFrame), then you have call

someContainersName.add(someJComponetsName); //EDIT corect method is add  
someContainersName.revalidate();
someContainersName.repaint(); // just in some cases

1/ if you want/need to remove JComponent(s) from the Container (JPanel, JFrame), then you have call

someContainersName.remove(someJComponetsName);
someContainersName.revalidate();
someContainersName.repaint();

my questions:

1/ why set opacities for Swing JButton.setOpaque(false);
2/ why you needed remove some JComponent, if there is possible to call
JButton.setVisible(false);, and in the moment when you'll need that, just set/change some properties for inVisible JButtons, thenafter set that to setVisible(true)
3/ are you sure that something in your line 25 has something to do with remove JButton

Edited 5 Years Ago by mKorbel: amend Remove mmthod to Add

This article has been dead for over six months. Start a new discussion instead.