0

Let's say I have a JFrame that is created through a singleton pattern like this :

private static Dist2PuncteFrame instance = null;

public static Dist2PuncteFrame getInstance() {

    if (instance == null)
        instance = new Dist2PuncteFrame();
    return instance;
}

I call it through a JMenuItem like this:

dist2PcteItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            dist2PcteFrame = Dist2PuncteFrame.getInstance();
            dist2PcteFrame.setSomeParameters(blablabla);
            dist2PcteFrame.init();
        }
    });

Nothing complicated. The problem occurs when I open the window the second time.
My close button and the 2 input labels seem to move (labels left/right, button up/down).

I have frames that are called the exact same way and they don't have this problem.
Anyone knows what seems to be the problem? I am using NetBeans 7.1.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
Pantaziu Cristian
  • 882
  • 2
  • 14
  • 26
  • 1
    Possiby an EDT Violation? Check http://stackoverflow.com/questions/2727841/why-a-edt-violation-happens – Rekin May 21 '12 at 09:10
  • 2
    For better help sooner, post an [SSCCE](http://sscce.org). Without seeing your code, it's unlikely that someone can tell what's going on. – Guillaume Polet May 21 '12 at 09:10
  • 1
    Yes, post your code - looks like you're building the GUI every time so you end up with multiple instances of the GUI controls inside the single Frame. Try temporarily removing the singleton check (comment out: "if (instance == null)" ) to verify this. – davidfrancis May 21 '12 at 09:18
  • What does init() do? Does it add the controls? It must be doing something that affects the rendering – maress May 21 '12 at 09:36
  • I've got it. My `init()` method initializes the components (`result = new JLabel()`, etc.) and calls the addListeners() and createLayout() methods. The solution is to add `getContentPane().removeAll()` before everything else in `init()`. – Pantaziu Cristian May 21 '12 at 10:13

0 Answers0