user3832184
user3832184

Reputation: 77

JFrame instance doesn't visualise added component

I created SelectMethodFrame that extends JFrame that can have only one instance:

public class SelectMethodFrame extends JFrame {

  private JSplitPane mainWindow = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

  public JSplitPane getMainWindow() {
    return mainWindow;
  }

  public void setMainWindow(JSplitPane mainWindow) {
    this.mainWindow = mainWindow;
  }

  private static SelectMethodFrame instance = null;



  public static SelectMethodFrame getInstance() {
    if (instance == null)
        instance = new SelectMethodFrame();
    return instance;
 }


  public void setTab(JComponent panelConsumption, JComponent panelCpu,
        JComponent panelMemo, JComponent panelScreen,
        JComponent panelMobile, JComponent panelWifi) {

    TabbedView tab = new TabbedView(panelConsumption, panelCpu, panelMemo,
            panelScreen, panelMobile, panelWifi);


    mainWindow.setRightComponent(tab);


}

  public void setTree(JTree tree) {

    mainWindow.setLeftComponent(new JScrollPane(tree));

    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);

    getContentPane().add(mainWindow, java.awt.BorderLayout.CENTER);
    setExtendedState(MAXIMIZED_BOTH);
    setMinimumSize(new Dimension(800, 500));
    this.setTitle(Constants.APP_CHECKBOXTREE);
  }

}

I create frame in one class in this way:

SelectMethodFrame frame = new SelectMethodFrame();

frame.setTree(treeSelection);

And everything works fine but when I want to add another component to my frame in another class:

SelectMethodFrame.getInstance().setTab(panelConsumption, panelCpu,
    panelMemo, panelScreen, panelMobile, panelWifi);

it doesn't show it. I tried with

SelectMethodFrame.getInstance().repaint();
SelectMethodFrame.getInstance().revalidate();

but it doesn't work. It shows component only if created in the constructor. Where is the problem?

I changed the code and put:

public SelectMethodFrame() {

    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);

    getContentPane().add(mainWindow, java.awt.BorderLayout.CENTER);
    setExtendedState(MAXIMIZED_BOTH);
    setMinimumSize(new Dimension(800, 500));
    this.setTitle(Constants.APP_CHECKBOXTREE);

}

instead in

public void setTree(JTree tree) {

    mainWindow.setLeftComponent(new JScrollPane(tree));

    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);

    getContentPane().add(mainWindow, java.awt.BorderLayout.CENTER);
    setExtendedState(MAXIMIZED_BOTH);
    setMinimumSize(new Dimension(800, 500));
    this.setTitle(Constants.APP_CHECKBOXTREE);
  }

now it opens it in another frame but shows only the last component added. It so strange..

Upvotes: 1

Views: 78

Answers (1)

elias
elias

Reputation: 15480

You're not implementing the singleton pattern correctly.

In the SelectMethodFrame class, set the constructor as private:

public class SelectMethodFrame extends JFrame {
    private SelectMethodFrame (){}
    ...
}

And in all references to the singleton inside the class, instead this, use the instance variable, like:

instance.pack();
instance.setLocationRelativeTo(null);
instance.setVisible(true);
//etc...

This way you can't call new SelectMethodFrame() externally. So, instead:

SelectMethodFrame frame = new SelectMethodFrame();

You should use:

SelectMethodFrame frame = SelectMethodFrame.getInstance();

It avoids the creation of more than one instance of the class.

Upvotes: 1

Related Questions