0
votes

I want to make other class frame setvisible(false) when button is pressed.

There is a class called DisplayScore and this class gets getContentPane().add(new ScoreInfo()); from ScoreInfo class. Reason for spliting into two class is because DisplayScore extends JFrame and ScoreInfo extends JPanel. Anyway

In ScoreInfo when button is pressed I want to call up other frame and close the DisplayScore frame. If I do this.setVisible(false);, only the Panel disappear, and the frame which is made from DisplayScore(), remains.

So..my question is how handle that from ScoreInfo class? Check out the buttom part. actionPerformed method.

public class ScoreInfo extends JPanel implements ActionListener
{
        JButton btnBack;

public ScoreInfo()
{


    JTextArea ta = new JTextArea();
    ta.setForeground(Color.white);
    ta.setFont(new Font("Serif", Font.ITALIC+Font.BOLD, 16));
    ta.setBackground(Color.darkGray);

    try
    {
        ta.read(new FileReader("data/scores4.txt"),null);
    }
    catch(IOException ioe)
    {
        System.out.print("ERROR: Could not read file \"");
    }


    btnBack = new JButton("MAIN");
    btnBack.setSize(10,10);
    btnBack.setBackground(Color.darkGray);
    btnBack.setFont(new Font("Courier New", Font.ITALIC+Font.BOLD, 15));
    btnBack.setForeground(Color.black);

    JPanel score = new JPanel();
    score.setLayout(new BorderLayout());
    JButton btnScore = new JButton("H I G H  S C O R E");
    btnScore.setBackground(Color.BLACK);
    btnScore.setFont(new Font("Courier New", Font.ITALIC+Font.BOLD, 48));
    btnScore.setForeground(Color.white);

    JLabel score_tab = new JLabel("   N A M E  " + "  S C O R E  " + "  C O M B O  ");
    score_tab.setFont(new Font("Courier New", Font.ITALIC+Font.BOLD, 35));


    score.add(btnBack, BorderLayout.WEST);
    score.add(btnScore, BorderLayout.CENTER);
    score.add(score_tab, BorderLayout.SOUTH);

    JPanel panel = new JPanel();
    panel.setBackground(Color.darkGray);
    panel.setPreferredSize(new Dimension(800, 380));
    panel.setLayout(new BorderLayout());
    panel.add(score, BorderLayout.NORTH);
    //panel.add(backpnl, BorderLayout.WEST);
    panel.add(ta, BorderLayout.CENTER);

    add(panel, score);

    btnBack.addActionListener(this);

    }

public void actionPerformed(ActionEvent evt){
    if(evt.getSource()==btnBack){

        //want to make DisplayScore.setVisible(false); 
        //this.setVisible(false);
        MainFrame mf = new MainFrame();
        mf.pack();
        mf.setSize(640,480);
        mf.setLocation(400,0);
        mf.setResizable(false);
        mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mf.setVisible(true);

    }
}

}

2
1) See The Use of Multiple JFrames, Good/Bad Practice? 2) The pop-up GUI might be best done as a JDialog or JOptionPane. 3) "because DisplayScore extends JFrame.." Don't extend frame. "..and ScoreInfo extends JPanel" ..or panel. Instead simply create one and add things to it.Andrew Thompson

2 Answers

1
votes

You could:

  • Pass a reference of the parent frame to the panel. I think this is a little dirty, but that's just me.
  • Use SwingUtilities.getWindowAncestor(Component) to find the parent window of the current component. You can then call setVisible on it.
1
votes

This should work:

if(evt.getSource()==btnBack){

    getParent.setVisible(false);
    MainFrame mf = new MainFrame();

But is not too robust, since you can't know if the parent is actually the JFrame ( and as MadProgrammer commented, this would be the contentPane, so this won't work). A better approach would be to ask for the JFrame in the constructor, and hide it necessarily.