-1

I have a class named BoardGUI extended from JFrame, in a constructor I have made a JPanel with two buttons in it. I have added this panel into my frame. Whenever I run this program, buttons get invisible. As I bring my mouse cursor over the buttons they get visible. Code is as follows:

public class BoardGUI extends JFrame {
Play pieces;
JButton a=new JButton("Undo");
JButton r=new JButton("replay");
JPanel jp=new JPanel();

public BoardGUI() {
    pieces = new Play();
    setTitle("Checkers Game");
    setSize(645, 700);
    setVisible(true);

    jp.setLayout(new FlowLayout());
    jp.setPreferredSize(new Dimension(645,35));
    a.setVisible(true);
    r.setVisible(true);
    jp.add(a);
    jp.add(r);
    add(jp,BorderLayout.SOUTH);

I am also using repaint method in my program. Can anybody point out my mistake and suggest any solution for this?

  • Can you provide a simple test through public static void main method? – PKopachevsky Jan 14 '14 at 11:42
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) of your closest attempt (as opposed to code snippets). 2) Provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Jan 14 '14 at 11:46
  • As to the layout, you might get some ideas from [this chess board](http://stackoverflow.com/a/21096455/418556). This [short example](http://stackoverflow.com/a/16058759/418556) shows how to combine components with a BG image. – Andrew Thompson Jan 14 '14 at 11:54
  • Actually its a checkers game. Whenever a piece is moved. a 2D array is being updated a frame is being repainted. so main is just a single line code. make an object of BoardGUI. –  Jan 14 '14 at 12:00
  • For me your code works... can you tell us what look&feel are you using, JVM, etc.? – Alberto Jan 14 '14 at 12:02
  • 1
    I didn't see there MCVE, where is Play (searched by CTRL + F) and please why there is MouseWhatever, voids for piece, MCVE isn't copy - paste (just in case that there is only part of) – mKorbel Jan 14 '14 at 12:17
  • btw I'm take that as joke – mKorbel Jan 14 '14 at 12:18

1 Answers1

5

I have a class named BoardGUI extended from JFrame, in a constructor i have made a JPanel with two buttons in it. I have added this panel into my frame. Whenever i run this program, buttons get invisible. As i bring my mouse cursor over the buttons they get visible.

  • setVisible(true); should be last code line in constructor, because you added JComponents to the already visible JFrame,

  • or to call revalidate() and repaint() in the case that JComponents are added to visible Swing GUI

  • there no reason to call a.setVisible(true); or r.setVisible(true); for standard JComponents, because JComponents are visible(true) by default in comparing with Top Level Containers, there you need to call JFrame/JDialog/JWindow.setVisible(true);

EDIT

(i used the very first suggestion you gave. problem remains the same.) - for example

enter image description here

from code

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyGridLayout {

    private JFrame frame = new JFrame("GridLayout, JButtons, etc... ");
    private JPanel panel = new JPanel(new GridLayout(8, 8));

    public MyGridLayout() {
        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                JButton button = new JButton("(" + (row + 1) + " / " + (col + 1) + ")");
                button.putClientProperty("column", col);
                button.putClientProperty("row", row);
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();
                        System.out.println(
                                "clicked column : "
                                + btn.getClientProperty("column")
                                + ", row : " + btn.getClientProperty("row"));
                    }
                });
                panel.add(button);
            }
        }
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyGridLayout myGridLayout = new MyGridLayout();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • i used the very first suggestion you gave. problem remains the same. –  Jan 14 '14 at 11:44
  • 1
    then there are another ***, for better help sooner post an [MCVE](http://stackoverflow.com/help/mcve), short, runnable, compilable with hardcoded value in local variables, see [Initial Thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) also, is very important – mKorbel Jan 14 '14 at 11:46
  • 1
    *"then there are another ***"* (Chuckle) ;) – Andrew Thompson Jan 14 '14 at 11:47
  • 1
    @Andrew Thompson - :-) *** == in my head ran only insults – mKorbel Jan 14 '14 at 11:55
  • 1
    Oh I'm right there with you. How many times a *day* do we need to repeat these things? – Andrew Thompson Jan 14 '14 at 11:58
  • @Andrew Thompson each of edited question, every day, too hard to be clean-up squad here – mKorbel Jan 14 '14 at 12:03
  • 1
    (shrugs) I go through 'spurts of activity'. Then I get sick of it for a while and just vote them down and move on.. – Andrew Thompson Jan 14 '14 at 12:05
  • 1
    @AndrewThompson I feel bad when I see you _"semi-flashing"_ on someone. I can tell you get irritated by the repetitiveness. :) – Paul Samsotha Jan 14 '14 at 12:37
  • 1
    @peeskillet I am trying to be calmer about it, since I was hauled to Meta and hugely criticized for it. Who knows how long the (slightly) calmer me will last... ;) – Andrew Thompson Jan 14 '14 at 12:43
  • @peeskille did you read comments by Kewin Workman, and on another forums too, AndrewThompson is domesticated and peace_full creatures (biblically speaking) in compare with him – mKorbel Jan 14 '14 at 12:48
  • @mKorbel no, I don't roam other forums too much. I could imagine though. I'm still a newb here, and I often times want to go bizzerk, but I'm trying to keep clean rep :) – Paul Samsotha Jan 14 '14 at 12:51