2

I'm trying to figure it out for 2 days and nothing helps. I Tried over 100 combinations and nothing. This is my last chance. I'm writing a simple game. 1 JFrame, few JPanels. After some action I need "a play again button" will show up. Adding a button(default visibility) in JPanel constructor shows it all-time, adding with setVisible(false) and then call (true) in other method doesnt work. I've tried revalidate(), rapaint() etc.

public class Game extends JPanel implements ActionListener{

private JButton playAgain = new JButton();

public Game(){

    setFocusable(true);
    requestFocus();
    this.setPreferredSize(new Dimension(800,600));
    this.setLayout(null);
    addButton();
    this.setVisible(true);
}

private void addButton() {

    playAgain.setBounds(600, 550, 200, 50);
    playAgain.addActionListener(this);
    playAgain.setBorder(null);
    playAgain.setCursor(new Cursor(Cursor.HAND_CURSOR));
    playAgain.setContentAreaFilled(false);
    playAgain.setVisible(false);
    this.add(playAgain);
}

private void showButton() {

    playAgain.setVisible(true);
}

public void actionPerformed(ActionEvent e){

        if(king1.isKingStopped()){
            gameFinished = true;
            addButton();
            //showButton(); // doesnt work ;/
}

Function showButton() doesnt change visibility. This is only problematic part of code, not all. Thanks.

  • 2
    Just wondering, did you verify through debugger that the `showButton` method is being called? – Orin Aug 31 '16 at 18:28
  • 3
    1) **DON'T** use a `null layout`!!! See [Null Layout is Evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) 2) Don't use `setPreferredSize`, see [Should I avoid the use of setPreferred|Minimum|Maximum size?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – Frakcool Aug 31 '16 at 18:28
  • 3
    Well, a button with no text, no border and the content area not filled may be not visible in some LaFs. Also, please post a [MCVE], as the actionPerformed method you provided would not even compile, because a closing `}` is missing. When people can reproduce the problem you're describing, the cause will be found faster. – Lukas Rotter Aug 31 '16 at 18:29
  • 4
    Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Aug 31 '16 at 18:32
  • have you tried making a basic button with this code (removing most of the code in addButton()), then adding the code line by line? That way you could find out what lines of code is making the button not display. – Tyler Aug 31 '16 at 18:33
  • @Orin2005 ofc. yes it is. – Jakub Kralka Aug 31 '16 at 18:36
  • @Lukas Rotter button have custom icon. – Jakub Kralka Aug 31 '16 at 18:37
  • 1
    i don't get it ?`playAgain.setVisible(false);` how can you even click a button which is not visible ? – Madhawa Priyashantha Aug 31 '16 at 18:41
  • @FastSnail well.. :) I want to click it when it will be visible. – Jakub Kralka Aug 31 '16 at 18:44

2 Answers2

0

Try to put

playAgain = new JButton();

in side your constructor like :

public class Game extends JPanel implements ActionListener{

private JButton playAgain;

public Game(){
playAgain = new JButton();
}
}
Kaustubh
  • 64
  • 1
  • 7
0

I found it! Change JButton to "static" and showButton() to static void. And the button is now visible. Can anyone tell why ? :) Maybe it help someone else :) thanks guys.

private static JButton playAgain();

private static showButton();