0

I'm adding and removing a JButton on MouseEnter and MouseExit respectively. This is working ok but when the button is added to the panel it appears in the top-right corner instead of the BorderLayout.SOUTH position specified.

The Frame only has a JPanel and the only line I have added is

 jPanel1.addMouseListener(new myMouseListener(jPanel1));

The Mouse Listener

package example;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
public class myMouseListener extends MouseAdapter{
    JButton btn;
    JPanel panel;

    public myMouseListener(JPanel panel)
    {
        this.panel = panel;
        this.btn = new JButton("Test");
        this.btn.setSize(40, 40);
    }

    public void mouseEntered(MouseEvent e) {
        panel.setBackground(Color.red);
        panel.add(btn, BorderLayout.SOUTH);
    }

    public void mouseExited(MouseEvent e) {
       panel.setBackground(Color.blue);
       panel.remove(btn);
    }
}

You can download a sscce here
http://www.filehosting.org/file/details/302851/Example.zip

Can anyone shed some light on the issue?

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
  • 1
    Are you sure that you're using `BorderLayout` in your `JPanel`? – KARASZI István Jan 19 '12 at 12:29
  • 2
    don't paste link of source code. Post your code so anybody can suggest. – KV Prajapati Jan 19 '12 at 12:30
  • 2
    please learn java naming conventions and stick to them – kleopatra Jan 19 '12 at 12:38
  • 1
    unrelated: remove the btn.setSize(..), it's unneeded clutter (has no effect whatever) – kleopatra Jan 19 '12 at 12:42
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 19 '12 at 12:54
  • @AndrewThompson, i'll update the question to make it clear but the link points to an sscce. – Ash Burlaczenko Jan 19 '12 at 13:09
  • @kleopatra, this is a small test case, the full application follows a naming convention. – Ash Burlaczenko Jan 19 '12 at 13:15
  • @kleopatra, the `setSize()` does make I difference. Try the sscce i've posted and change the values and you will see. – Ash Burlaczenko Jan 19 '12 at 13:18
  • a) you didn't show a SSCCE b) if it makes a difference, you are doing something completely wrong c) test case or not, small or not: you want to start a communication with us, its language is java - why would you hinder that communication by not following its conventions? – kleopatra Jan 19 '12 at 13:21
  • @kleopatra, what do you want? the sscce link isnt helpful. I've posted a link to some code that is short, self contained, compilable and an example. I don't know what else to show. – Ash Burlaczenko Jan 19 '12 at 21:47
  • 1
    @AshBurlaczenko That zip is listed as `122.0 kB`. An SSCCE should be less than 200 lines of code (some would say shorter). As such, I can conclude that source that (when compressed) comes to 122 kilobytes is ***either not S or not SC!*** Please read the links people provide, in future. -1 – Andrew Thompson Jan 20 '12 at 02:11
  • This is, in essence, a duplicate of: http://stackoverflow.com/questions/4279435/java-how-would-i-dynamically-add-swing-component-to-gui-on-click – eternaln00b Jan 20 '12 at 04:39

2 Answers2

5

Call

revalidate();
repaint(); 

after adding/removing.

May be it's better to make it visible/invisible instead?

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

The following works for me with no issues i.e. a button does show up at the south. Issuing revalidate on the component, as suggested by stanislavL, seems to work just fine. Please do a little debugging / research before dismissing answers outright!

public class LayoutTest
{
    public LayoutTest()
    {
        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        JPanel p = new JPanel(new BorderLayout());
        p.addMouseListener(new MyMouseListener(p));
        f.add(p, BorderLayout.CENTER);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String args[])
    {
        new LayoutTest();
    }
}
class MyMouseListener extends MouseAdapter{
    JButton btn;
    JPanel panel;

    public MyMouseListener(JPanel panel)
    {
        this.panel = panel;
        this.btn = new JButton("Test");
        this.btn.setSize(40, 40);
    }

    public void mouseEntered(MouseEvent e) {
        panel.setBackground(Color.red);
        panel.add(btn, BorderLayout.SOUTH);
        panel.revalidate();
    }

    public void mouseExited(MouseEvent e) {
       panel.setBackground(Color.blue);
       panel.remove(btn);
       panel.revalidate();
    }
}  
eternaln00b
  • 1,043
  • 9
  • 18